Saturday, May 12, 2012

Quick VIM configuration and tutorial

VIM (Vi IMproved) is a very powerful ubiquitous console-mode text editor, but unfortunately the standard configuration is not that great (being mostly compatible with older VI versions and still supporting very slow terminals, which is never the case for a local terminal). People really do not have time to set to read a 1000 page manual to take advantage of some neat VIM features.

What most need is just a quick guide to set up VIM so that it can rival most heavy GUI editors in features. And here it is. There's also GVim, a GUI version of VIM, but IMHO the main advantage of VI is that it can run in a terminal and is very quick on remote connections, where GVim would be too slow or not installed at all.

Firstly, avoid using ancient VI version adding this to your .bashrc:

: alias vi='vim'


Some useful keystrokes

Esc: return to normal mode
i: insert at current location
I: insert at start of current line
a: insert after current location (append)
A: insert after end of current line (shortcut for $a)
o: insert line below current line (open)
O: insert line above current line
s: delete character under cursor and start inserting in its place (substitute text, shortcut for xi)
S or cc: delete all text on line and start inserting in its place (substitute or change line)
.: repeats last operation
*: search forward for the next occurrence of the word under the cursor
ci": replace what’s inside the next set of " and go into insert mode. You can also use ' instead of ", etc (any character, you choose)

^: go to the beginning of line
f: move the cursor to the next  location
$: go to the end of line


q: start recording following commands into a register named .
q: stop recording and save a macro. Make sure you end up in a state that allows running next iteration, e.g. same position as initial but on the next line.
@: run the macro saved in  register  times, by default 1.


Ctrl^N: complete current word with first match in text (press again to select the next match). INSERT mode only.
Ctrl^P: complete current word with last match in text (press again to select the previous match). INSERT mode only.

How to use a dictionary for word completion. E.g. I am bilingual Italian-English and I have in my .vimrc:

set dictionary=/usr/share/dict/british-english,/usr/share/dict/italian
set complete-=k complete+=k
 

Useful colon commands

:n edit next file in argument list
:N edit previous file in argument list
 

Useful .vimrc commands

To use your mouse to move the text cursor or select, in all modes:

set mouse=a

You can still copy and paste the X primary selection by holding the shift key before selecting or middle-clicking, respectively. Your mouse wheel, if any, will work even when mouse="". If your mouse does not have a middle button, you may find the keyboard short-cut to paste (shift-ins) more convenient.

Turn on the Highlight Search option:

set hlsearch

Enable syntax highlighting:

syntax on

If you are using a dark background for your terminal you may want to:

set background=dark

Display commands or file name completion in a one-line menu at the bottom, so that you can more easily see what the options are when cycling through alternatives with the Tab key:

set wildmenu

Set the currently edited file name and path (directory) in terminal title. On exit, the old terminal title is restored.


set title


Show partial commands in the last line of the screen:

set showcmd

Features you may want to toggle manually

Enable syntax highlighting if your file has the wrong extension, e.g. not .php for a PHP file:

:set syn=php


:nohlsearch
Switch off search highlighting for the current search. It can be shortened up to :noh without ambiguity.

:set list
Makes vim show hidden characters: '$' indicates EOL, '^I' tabspace
Toggles back to normal mode: :set nolist
Toggles: :set invlist

:set number

When creating documentation rather than source code, you may want VIM to automatically break lines for you, e.g. to have a textwidth of 70 characters:

:set tw=70


Scripts and add-ons

vimfortune - displays a VIM usage tip every time you start vim with an empty buffer. The only drawback: some tips are quite long and take time to read, but you can skip it (press q if the tip is more than one page long)


TODO
Describe:
set ts=4
set sw=4
set spell
set spl=en
syntax on
set wrap
set ai
set cindent ” for c indentation(correct spacing)

See other basic stuff here:

http://vim.wikia.com/wiki/Tutorial

1 comment:

Unknown said...

Vim tutorial Cheat Sheet , Learn VIM in 5 miunute
https://www.youtube.com/watch?v=7BDh4bDqG8Y