Vim – Automatically Make Scripts Executable

Add this to your vimrc:

au BufWritePost * if getline(1) =~ “^#!” | if getline(1) =~ “/bin/” | silent !chmod a+x | endif | endif

Now, whenever you save a file which has its first line starting with #! (e.g. #! /bin/python), it will become executable.

Enjoy :-)

Rerun a Bash Command With Different Parameters (Advanced Bang Bang)

You probably know that you can use “bang bang” (that is: !!) to rerun your last command.
Sometimes you use it if you forgot to prefix a privileged command with sudo.
example:

$ touch /etc/test
touch: cannot touch ‘/etc/test’: Permission denied
$ sudo !! #rerun the last command with sudo before it.

Now assume you want to rerun the last command, but want to change a parameter you passed to it.
For example

echo 1.2 && echo 2.3 && echo 4.2

You want to run the same command but with each instance of the number 2 replaced with 5.
This is how you do it

!! :gs/2/5

That’s it. Enjoy

Bash Tip: Separate a Variable Name From a String

Assume you have this variable in bash:


singular=egg

Now you want to define a new variable which takes $singular and appends an “s” to it.

This is how to do it


singular=egg

plural=${singular}s

The trick is to use {} in order to separate the variable name from the rest of the string.

Enjoy

Show current git branch and its status

We’re going to make our bash prompt show the current git branch, with a color indicating its status (green = clean, red = dirty, yellow = clean but not pushed to remote)

The result should be something like this:

current git branch in prompt

Add the following code to your ~/.bashrc

if [ -x /usr/bin/tput ] && tput setaf 1 >& /dev/null; then
	c_git_clean=$(tput setaf 2)
	c_git_dirty=$(tput setaf 1)
	c_git_semi_dirty=$(tput setaf 3)
	c_reset=$(tput sgr0)
else
	c_git_clean=
	c_git_dirty=
	c_reset=
	c_git_semi_dirty=
fi

git_prompt ()
{
	if ! git rev-parse --git-dir > /dev/null 2>&1; then
		return 0
	fi

	git_branch=$(git branch 2> /dev/null | sed -n '/^\*/s/^\* //p')
	if git diff HEAD --quiet 2> /dev/null >&2; then
		git_color="${c_git_clean}"
		dirty=0
	else
		git_color="${c_git_dirty}"
		dirty=1
	fi

	if git diff $git_branch origin/$git_branch --quiet 2> /dev/null >&2; then
		git_color="$git_color"
	else
		if [ dirty=0 ]; then
			git_color="${c_git_semi_dirty}"
		fi
	fi

	echo " ($git_color$git_branch${c_reset})"

}
#the relevant part is $(git_prompt). the following ps will give you a prompt like:
#[username]@hostname [current directory name] (current git branch) $
export PS1='\[\033[0;33m\]\u\[\033[0m\]@\h [\[\033[1;34m\]\W\[\033[0m\]]$(git_prompt) \[\033[0m\] \$ '

My gitconfig

I’ll put it here without explanations, as it is self-explanatory.
Enjoy ;)

[user]
	name = Your Username
	email = Your Mail
	token = Your Github token if relevant

[core]
	quotepath = false
	excludesfile = /Your/Home/Directory/.gitignore
	editor = mvim -f #your favorite editor

[push]
	default = matching

#I use Kaleidoscope for diff. A great tool if you happen to work on a Mac by the way
[difftool "Kaleidoscope"]
	cmd = ksdiff-wrapper git \"$LOCAL\" \"$REMOTE\"

[difftool]
	prompt = false


	tool = Kaleidoscope

[color]
	ui = auto

[color "branch"]
	current = yellow reverse
	local = yellow
	remote = green

[color "diff"]
	meta = yellow bold
	frag = magenta bold
	old = red bold
	new = green bold

[color "status"]
	added = yellow
	changed = green
	untracked = cyan

[alias]
	st = status # 'git st' is now the same as 'git status'. same for the others
	ci = commit
	br = branch
	co = checkout
	df = diff
	dc = diff --cached
	lg = log -p
	lol = log --graph --decorate --pretty=oneline --abbrev-commit
	lola = log --graph --decorate --pretty=oneline --abbrev-commit --all
	ls = ls-files
	ign = ls-files -o -i --exclude-standard #show files ignored by git

Python – Modifying a List of Dictionaries Using Map

Assume we have a list of python dictionaries, and all of the dictionaries have a common key, let’s call it ‘numeric_value’

list = [{'numeric_value' : 0}, {'numeric_value' : 3}, {'numeric_value' : 5}, {'numeric_value' : 2}]

Now, we want to add a field to each of the dictionaries, let’s call it ‘prime’. We want it to be True if ‘numeric_value’ is a prime number, and False otherwise.

Assume that we have already implemented a simple function ‘is_prime(number)’ which checks whether a number is prime or not.

The simple approach:

for idx,item in enumerate(list):
    list[idx]['Prime'] = is_prime(item['numeric_value'])

Which works, but it’s not attractive enough :D

This is how I implement it when I’m bored:

list = map(lambda x: dict(x.items() + [('prime', is_prime(x['numeric_value'])]), list)

Note: I’m NOT saying that you should do it this way :)

vim – Convert Code to HTML

You can very easily create a HTML version of your code (including the style and colorscheme you are using) with this simple vim command (in command mode):

:TOhtml

(That is, hit escape, then type  “:TOhtml” without the quotes, and hit enter)

If you’re working on a file named  myfile.c, vim will create a HTML version of it in the same directory, named myfile.c.html.