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

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.


Search For The Word Under The Cursor In VIM

If you want to search a word in vim, you’ll probably use “/” (forward) and “?” (backward) followed by the word you want to search.

What if you want to look for the word currently under the cursor?
Obviously, you can use the above method. But a shorter one is: just hit “*” to search forwards, and “#” to search backwards.
So short, so efficient.

Replace Text Enclosed in Quotes or Brackets in VIM

This command, to change text enclosed between quotes/double quotes/any type of brackets, can be really time saving:
Say you have this code:

void f() {

Place the vim cursor anywhere inside the function, and (in command mode) type ci{ (meaning: Change Inner block enclosed between curly brackets).
The code within the brackets (In this case, the whole body of the function) will be deleted, and you’ll find yourself in insert mode where you can type the new code.

Try it, you’ll love it ;-)