Tuesday, 2 June 2015

Some of the cool vim tricks and tips

I use vim everyday. So much that even to write notes I use vim!
Here are some of the awesome tricks I have come across:

1.  Suppose you have opened a file for which you don't have permission. While saving it will say "E212: Can't open file for writing". Now instead of saving this temporarily in another file and then saving in this file, you can do this:

:w !sudo tee %

2.  You can edit binary files like in hexedit 


3. Easy diff between two files. Say there are two files 'a' and 'b'. To find the diff of 'a' with respect to 'b'

:vert diffs b

4. See command history


5. Execute command while still in vim.

:! <command>

Here I am compiling and running cpp file without closing it.

6. Autocomplete whatever you are typing in insert mode based on the words you have typed before:


7. Directory Listing without NERDTree

:vsp %:h

8. Indenting code

=<next position>
For example, 
To indent the current line, press ==
To indent the code till next closing flower brace, press =}

Here is an example of indenting the entire code block. Place the cursor on { and then press =%



9. Make searching as you type instead of after completing typing the entire thing

:set incsearch

10. Download and open source code for a given url

vim http://google.com

I usually use this to quickly view responses of my web pages.

vim http://localhost/path

I will add more in the future.

What are the dark corners of Vim your mom never told you about?