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¶
ESC
switch to command modei
switch to insert modev
switch to visual mode character-wiseV
switch to visual mode line-wise
Essential commands¶
:q
quit when no changes where made:q!
quit discarding any changes made:w
write/save current document:w!
write/save current document even though it's read-only:wq
write/save current document and quit (same asZZ
):e file
open file:42
jump to line number 42/pattern
search forward for pattern?pattern
search backward for patternn
show next search resultN
show previous search resultu
undo last changectrl-r
redo last change
Movement commands¶
h,j,k,l
behave just like arrow keys left, down, up, right0
go to first column^
go to first non-blank character$
go to end of linew
go to start of following worde
go to end of word under cursorgg
go to first lineG
go 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¶
o
new line below cursor, and switch to insert modeO
new line above cursor, and switch to insert modedd
delete line under cursor (and copy it to register)yy
yank line under cursor into register = copyp
put contents of register = pastex
delete character under cursor (and copy it to register)dw
delete word under cursor (and copy it to register)cw
change word = delete word under cursor, and switch to insert modeA
append at end of lineI
insert at beginning of line~
toggle case of letter under cursor (lowercase <-> uppercase)J
join the current line with the next one>>
add level of indentation<<
remove level of indentation
Advanced commands¶
:set invnu
toggle line numbersctrl-x ctrl-f
autocomplete filename (when in insert mode):%s/old/new/g
replace old by new in current document:g/pattern/d
delete all lines matching pattern:g!/pattern/d
delete all lines not matching pattern (same as:v/pattern/d
):bn
,:bp
switch 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