vim tips and tricks

=====vimrc=====

Below I have listed some settings in vim which make your editing life easier.

  * Disabling file backups: Vim makes backups of files which are edited. Disable this with

<code>

set nobackup

</code>

  * Disable displaying line numbers with

<code>

set nonumber

</code>

  * If you enable the showmatch option, then whenever you start and close a bracket, then it automatically shows the matching brackets. This is useful for some programming languages like C.

<code>

set showmatch

</code>

  * You can set the number of spaces for each tab

<code>

set tabstop=6

</code>

  * Some languages like fortran 77 require seven blank spaces at the beginning of each line. You can set tabstop to seven and convert the tab into ordinary spaces by((Sometimes you may need a tab, like in a makefile))

<code>

set expandtab

</code>

  * Indenting of a code makes it easier to read it. Set auto indenting with

<code>

set ai

</code>

  * When you are typing a text document like in LaTex, long lines may not properly flow to next line and a word can appear on two lines. Use the following setting to shift to new line without putting a new line character

<code>

set linebreak

</code>

  * If you would rather put a new line character at the end of each physical line, use

<code>

set wrap wrapmargin=4

</code>

  * When you have a line that spans more than one physical line, then the cursor simple jumps to the next line. You can make the cursor move by one physical line each time with

<code>

nmap  j gj

nmap  k gk

</code>

  * While editing a paragraph, its formatting can get spoiled because you may make some changes in the middle of the paragraph. Then define the following key binding and use it whenever you want to reformat a paragraph

<code>

nmap gqap <F8>

</code>

=====Comment selected lines=====

Visually select the lines you want to comment using CTRL + v. Then press semi-colon ":" which will give you

<code>

:'<,'>

</code>

Then type

<code>

:'<,'> s/^/#

</code>

=====Links=====

  * [[http://rayninfo.co.uk/vimtips.html|Best of VIM Tips]]