Vim¶
Vim is one of the most powerful text editors. It is at the same time light and highly customizable and exists on virtually every platform. Unfortunately it comes with a steeper learning curve than other text editors. One reason is that vim is a modal editor which distinguishes :
- Insert mode to type or edit text
- Command mode to run commands
- Visual mode to select text with the cursor.
Most important shortcuts to switch between the modes¶
ESCswitch to command modeiswitch to insert modevswitch to visual mode character-wiseVswitch to visual mode line-wise
Essential commands¶
:qquit when no changes where made:q!quit discarding any changes made:wwrite/save current document:w!write/save current document even though it's read-only:wqwrite/save current document and quit (same asZZ):e fileopen file:42jump to line number 42/patternsearch forward for pattern?patternsearch backward for patternnshow next search resultNshow previous search resultuundo last changectrl-rredo last change
Movement commands¶
h,j,k,lbehave just like arrow keys left, down, up, right0go to first column^go to first non-blank character$go to end of linewgo to start of following wordego to end of word under cursorgggo to first lineGgo to last lineg,go to last modification%jump to matching counterpart (parenthesis, if else, ...)*search forward for word under cursor#search backward for word under cursor
Editing commands¶
onew line below cursor, and switch to insert modeOnew line above cursor, and switch to insert modedddelete line under cursor (and copy it to register)yyyank line under cursor into register = copypput contents of register = pastexdelete character under cursor (and copy it to register)dwdelete word under cursor (and copy it to register)cwchange word = delete word under cursor, and switch to insert modeAappend at end of lineIinsert at beginning of line~toggle case of letter under cursor (lowercase <-> uppercase)Jjoin the current line with the next one>>add level of indentation<<remove level of indentation
Advanced commands¶
:set invnutoggle line numbersctrl-x ctrl-fauto-complete filename (when in insert mode):%s/old/new/greplace old by new in current document:g/pattern/ddelete all lines matching pattern:g!/pattern/ddelete all lines not matching pattern (same as:v/pattern/d):bn,:bpswitch to next/previous open buffer
.vimrc¶
Vim can be configured through the $HOME/.vimrc file. Below we list some entries that can be useful to have in the .vimrc
"General
syntax on "syntax highlighting
set nocompatible "enable all vim features
set autoindent "automatic indentation
set showmatch "show matching parenthesis
set ruler "show current position at the bottom
set number "show line numbers
"Tabs
set expandtab "convert all tabs to spaces
set tabstop=4 "use tabs of 4 spaces
set shiftwidth=4 "indents are 4 spaces wide
"Search
set incsearch "incremental search
set wrapscan "continue search from top when reaching the end
set ignorecase "search is not case-sensitive
set smartcase "search case-sensitive for uppercase patterns
set hlsearch "search pattern highlighting