Few vim tricks

1. Ctags

Ctags lets you tag your current code and header files. It supports many languages. With tagging, when you are writing code, you can quickly traverse to the declarations and definitions of functions and variables. Once you install ctags in your distribution issue following command to tag source code (after you know where the source code for the library or program you want to tag exists).

$ ctags -R /path/to/source .

If for example you want to tag standard headers or other headers found in Linux, the “/path/to/source” would be “/usr/include“. Above command creates a tags file. To let vim know about the file, you can add following command in ~/.vimrc file.

set tags=./tags,~/.tags/tags,../tags,../../tags

This commands sets vim to search for tags file in current directory, parent directory, parent to parent directory or .tags directory in your home folder. If you want other files to be included while searching for references, add them to above line, after you have created the tags file.

With this set, now when you are in a function, if you want to see its definition or declaration press “Ctrl + ]“. To go deeper you can keep on pressing this key. To return back press “Ctrl + t“.

If you want to search which files reference current tag name, use following command in vim.

:ts <tag-name>

You can use :tn or :tp to move among the list.

2. Movement

Besides the j, k, l, h movement

:ls = lists currently opened files
:bn, :bp = lets you move between opened files. You can also press Ctrl + I or Ctrl + O, if you are editing across different files
:bd = When you are done, kill a buffer with :bd, so that you don’t clutter your opened file list.
` = Move to your last edit position. Very helpful, when you want to reference a part of your code and edit at other. For example few variables you might have declared at the top section, and initialize them at some later portion in the code.
g;, g. = These commands lets you move in your edit history. Unlike `, if you have a sparse edit positions, this can be very helpful

3. Folding

If you don’t want to see certain portions of source code, or structure code based on functions for better visibility, you can fold portions of the code. Before you do so, add following code to ~/.vimrc

" Fold
" v(visual) zf = create fold, za = open, zc = close, zd = delete fold
augroup vimrc
 au BufReadPre * setlocal foldmethod=indent
 au BufWinEnter * if &fdm == 'indent' | setlocal foldmethod=manual | endif
augroup END

As I have listed in the comments section above in vim highlight the code you want to fold using “v“, and fold it using “zf“. To open the fold press “za” and to close back press “zc“. If you close your document, the last fold option remains intact. I.e If you have a fold opened it remains opened, and if you have it closed it remains closed. However an opened fold does not mean the fold mark is deleted. Therefore you have to use “zd” to delete the fold marks. Note that there are other fold methods too, that changes the nature of fold.

In vim press “:help fold” to learn more.

4. Fonts

You should prefer good fonts while writing your code or editing documents. Download fonts like Inconsolata, Consolas or Bitstream Vera Sans and use them in your editor. For gvim I prefer Inconsolata and in terminal I prefer Consolas. To set the font in a terminal, you have to set it in your preferred terminal emulators like urxvt, lxterminal or gnome-terminal using their respective preferences option or respective config files. For gvim you can set it in ~/.vimrc by adding following code.

set guifont=Inconsolata\ Medium\ 15

5. Current file name

If you write code you also might need to compile it. In vim you don’t have to remember the name or location of the file to compile it. Just use following commands (e.g in case of java or gcc).

:!javac %
:!gcc -o %< %
:!java %<
:!gcc -o %< % && ./%<

As you might have guess from above command %< is for file name without the suffix and % is the file name.

If you need to change to current directory in vim you can type following command.

: cd %:p:h

However, this might not be easy to remember. You can also write an alias for this in ~/.vimrc.

" CDC = Change to Directory of Current file
command CDC cd %:p:h

After this if you type :CDC you can change to the directory that hosts current file.

6. Spell Checking

Vim is not only be used to write source code for programs, but also used to write your normal texts. One of the major concerns while editing texts is spell checking. Add following to your ~/.vimrc (add the language accordingly)

imap <F3> <C-o>:setlocal spell! spelllang=en_us<CR>
nmap <F3> :setlocal spell! spelllang=en_us<CR>

After this, you can edit your text document are ready to see all your typing error press F3 (if you don’t mind errors while editing, you can use this key earlier too). Here are few commands, you can use to spell check.

z = suggest words
]s, [s = jump to next, previous misspelled word
zg = Add a word to the dictionary
zw = Mark a word as incorrect

7. Copy/paste text from/to external sources.

"+y = Yes it is double quotes, plus and y. To copy a text to clipboard (just like pressing Ctrl + c in most GUI based text editors).

Linux also has another clipboard (where a text can be highlighted and pasted with middle mouse click). To copy text to this clipboard replace + with * in this command. After that you can click middle mouse button to paste the text.

"+p = To paste text copied in other GUI programs, press this key in vim.

Another very useful vim command is inserting part or whole document text inside vim. For that you have to use “:r” command.

:r !ls | head -3 # Insert only three top text from ls command.
:r ~/.vimrc # Insert the text of a file at your current mouse position.

Use u or Ctrl + r to undo or redo these changes.

Conclusion

I hope some of you have found this useful.There are many many commands in vim. Some of them stick closely with the users and even though popular some find it hard to adapt them. I would like to know your favorite vim commands and tricks (the one that is top on your list, or is super cool).  If you would like to copy some of these settings, you can also do that from this ~/.vimrc file.

About these ads

Leave a comment

Filed under Uncategorized

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s