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.

$ 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:


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

This is how to do it



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


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)

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

	git_branch=$(git branch 2> /dev/null | sed -n '/^\*/s/^\* //p')
	if git diff HEAD --quiet 2> /dev/null >&2; then

	if git diff $git_branch origin/$git_branch --quiet 2> /dev/null >&2; then
		if [ dirty=0 ]; then

	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 ;)

	name = Your Username
	email = Your Mail
	token = Your Github token if relevant

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

	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\"

	prompt = false

	tool = Kaleidoscope

	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

	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):


(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.