Wednesday, July 09, 2014

VIM commands


  • :help  -  vim help
  • :help commandname - help on a particular command
  • CTRL+] - jump to a highlighted topic
  • CTRL+T - jump backwards


  • :help left-right-motion
  • j,k move up down
  • h,l move left right
  • b,w move previous or next word
  • ctrl+b, ctrl+d move page up or page down

Undo redo

  • u: undo last change (can be repeated to undo preceding commands)
  • Ctrl-R: Redo changes which were undone (undo the undos). 
  • Compare to '.' to repeat a previous change, at the current cursor position. Ctrl-R will redo a previously undone change, wherever the change occurred. 

Switch between navigation and editing mode

  • A - move to the end of the line and switch to editing mode 
  • I - switch to editing mode at the current place
  • Escape - switch to navigation mode
  • alt+h alt+j alt+k alt+l - switch to navigation mode and move
  • alt+: - switch to navigation mode and send a command

Search and replace characters

Vim wiki on search and replace 
  • :s/foo/bar/g Find each occurrence of 'foo' (in the current line only), and replace it with 'bar'. 
  • :%s/foo/bar/g Find each occurrence of 'foo' (in all lines), and replace it with 'bar'.
  • %s/option value=".*"//g remove all beginnings of line. :%s/\option\n/, /g replace all end of line by comma + space. This cleans an html list of species for inclusion in a text.


Display a list of first level header in a markdown document (found in quick markdown navigation/TOC)
:g/^# /#
Then enter the line number to jump to that line.

Line numbers

 Display line numbers
:set nu
Disable line numbers
:set nonu

Editing a whole line

  • dd to delete a whole line
  • yy to copy a whole line
  • p to paste the copied or deleted text after the current line or 
  • P to paste the copied or deleted text before the current line 

Copy, cut and paste

  • Position the cursor where you want to begin cutting.
  • Press v (or upper case V if you want to cut whole lines).
  • Move the cursor to the end of what you want to cut.
  • Press d to cut or y to copy.
  • Move to where you would like to paste.
  • Press P to paste before the cursor, or p to paste after. 


Indentation replaced by spaces, add this to the ~/.vimrc file
set tabstop=4
set expandtab
set softtabstop=4
set shiftwidth=4
filetype indent on 
More details on vim indentation in the python wiki.

Multiple files and windows

  • :e filename - edit another file 
  • :ls         - show current buffers
  • :b 2        - open buffer #2 in this window
  • :b filename - open buffer #filename in this window
  • :bd         - close the current buffer (! to forget changes)
  • :bd filename -close a buffer by name 


  • :sp[lit] filename  - split window and load another file
  • :vs[plit] - same but split vertically  
  • ctrl-w up arrow - move cursor up a window
  • ctrl-w ctrl-w   - move cursor to another window (cycle)
  • ctrl-w_         - maximize current window
  • ctrl-w=         - make all equal size
  • CTRL+z - suspend the process and get back to the shell
  • fg - get back to vim


View differences between file1 and file2 (vim documentation)
vimdiff file1 file2

spell check

Set spell check only in the local buffer:
:setlocal spell spelllang=en_gb  
 Turn spell check off
:set nospell

Mark word as correct, this creates a spell file under /home/user/.vim/spell:
Mark word as incorrect

Plugins for programming languages


Text colour.
Add syntax highlight to your .vimrc
syntax enable
How to add a file extension to vim syntax highlight
au BufNewFile,BufRead *.dump set filetype=sql
I used it to display markdown files as text files:
au BufNewFile,BufRead *.md set filetype=txt

No comments: