Programming Adventures: Vim
Guide to vim for programmers.
Vim
Vim is a text editor.
It’s handy when working on most remote machines.
Many programmers use it as their default code editor.
I created this guide to act as a cheat-sheet for my personal use.
People who use vim regularly often create one for reference.
More detailed vim guides for various use cases can be found on the internet.
Overview
Linting is not available in plain vim.
A prowess in typing will make vim enjoyable:
- High typing speed; code completion is not available in plain vim.
- High typing accuracy; error correction is not available in plain vim.
Resources
vimtutor
Command-line program.:help _
Visit vim’s documentation. Replace_
with command.
Operations
Commands and actions are used to operate vim.
Ideally vim is supposed to be operated using only the keyboard.
Most commands can be called multiple times by prepending them with a number or range. A range is defined as _,_
where _
are numbers or marks or certain special characters:
.
Current line.$
Last line.%
Entire file. Equivalent to1,$
.*
Last selection in visual mode.
Range can also be augmented with basic arithmetic.
Configuration
Vim configuration is stored in .vimrc file.
Vimscript is used to write the configuration.
Look for ~/.vimrc
.
General Commands
Commands are made up of set
followed by options. Options may hold value.
Boolean options can be toggled by appending !
to the option.
Boolean options can be unset by prepending no
to the option.
Value of an option can be checked by appending ?
to the option.
Value can be assigned to an option by appending =
followed by the value to the option.
Value can be added to an option by appending +=
followed by the value to the option.
Value can be subtracted from an option by appending -=
followed by the value to the option.
An option can be reset to default value by appending &
to the option.
:set hlsearch
Set highlight search results.:set noh
Unset highlight search results.:set number
Set line numbers.:set nonumber
Unset line numbers.:set ignorecase
Set to ignore case in patterns and words.:set smartcase
Set to consider case in patterns and words. Requires:set ignorecase
.:set undofile
Set to track changes to a file.:set undodir=~/example/path
Set file location to save track of change. Replace~/example/path
with path and name of file. Requires:set undofile
.:set undolevels=_
Set number of changes to track for undo. Replace_
with a number.:set foldenable
Set to enable fold.:set foldmethod=_
Set method to collapse blocks of code. Replace_
with one ofmanual
,indent
,expr
,syntax
,diff
,marker
. Requires:set foldenable
.:set foldcolumn=_
Set number of folds to display on the sidebar. Replace_
with a number.:set incsearch
Set highlight pattern during input.:set statusline=example.expression
Set status line. Replaceexample.definition
with format.:set background=_
Set background scheme. Replace_
with scheme name.:set encoding=_
Set encoding. Replace_
with name of encoding.:set expandtab
Set to use spaces in place of tabs.:set tabstop=_
Set number of spaces to place for each tab. Replace_
with a number.:set shiftwidth=_
Set number of spaces for each indentation. Replace_
with a number.:set smarttab
Set to use number of spaces depending upon the location of cursor.:set autoindent
Set auto-indent. Equivalent to:set ai
.:set smartindent
Set to use auto-indent depending upon the location of cursor. Equivalent to:set si
.:set cursorline
Set to show line underneath cursor.:set nocursorline
Unset cursorline.:set cursorcolumn
Set to highlight the column of cursor.:set nocursorcolumn
Unset cursorcolumn.:set warp
Set line warp.:set nowarp
Unset line warp.:set autoread
Set to re-read files modified on disk.:set noautoread
Unset autoread.:set title
Set to display file title.:set notile
Unset title display.:set wildmenu
Display tab complete options as menu.:set spell
Enable spellchecking.:set list
Set to display whitespaces.:set nolist
Unset whitespaces display.:retab
Replace all tabs with spaces.:syntax on
Enable syntax highlight.:syntax off
Disable syntax highlight.:colorscheme _
Choose colorscheme for highlight. Replace_
with colorscheme. Requires:syntax on
.:filetype on
Enable file type detection.:filetype plugin on
Enable plugins for the detected filetype.:filetype plugin off
Disable plugins for filetype.:filetype indent on
Enable filetype specific indentation.:filetype indent off
Disable filetype specific indentation.
Commands in .vimrc must be added without :
.
Save Options and Sessions
:mk example.file
Save operations and mappings to file. Replaceexample.file
with name of file. Use:mk! example.file
to overwrite file.:mkexrc example/file
Save operations and mappings to file. Replaceexample/file
with path and name of file. Use:mkexrc! example/file
to overwrite file.:mks example/file
Save session to file. Equivalent to:mksession example/file
. Replaceexample/file
with path and name of file. Use:mks! example/file
to overwrite file.:source example/file
Restore operations and mapping or session. Replaceexample/file
with path and name of file.
Buffers, Windows and Tabs
A buffer is an open file.
A window displays a buffer.
A tab contains one or more windows.
General Commands
:buffers
List buffers.:buffer _
Move to buffer. Replace_
with id or name of the buffer.:ls
List buffers. Equivalent to:buffers
.:bn
Move to next buffer. Equivalent to:bnext
.:bp
Move to previous buffer. Equivalent to:bprevious
.:bf
Move to first buffer. Equivalent to:bfirst
.:bl
Move to last buffer. Equivalent to:blast
.:badd example.file
Add file to buffer. Replaceexample.file
with file path.:bdelete _
Delete buffer. Replace_
with id or name of the buffer.:_,_bdelete
Delete buffers. Replace_,_
with start id and end id respectively.:bd
Delete buffer. Equivalent to:bdelete
.:Bdelete
Delete buffer but do not close window.:jumps
Display jump list.:history :
Command history.:history /
Search command history.:history ?
Search command history.:qall
Quit all buffers.
Operations And Navigation
ctrl + ^
Switch to alternate buffer._ctrl + ^
Switch to specific buffer. Replace_
with buffer id.ctrl + w
followed bys
Split the current window horizontally.ctrl + w
followed byv
Split the current window vertically.ctrl + w
followed byn
Split the current window horizontally and edit a new file.ctrl + w
followed by^
Split the current window with alternate file._
followed byctrl + w
followed by^
Split current window with specific buffer. Replace_
with buffer id.ctrl + w
followed byj
Move to lower window.ctrl + w
followed byk
Move to upper window.ctrl + w
followed byh
Move to left window.ctrl + w
followed byl
Move to right window.ctrl + w
followed byr
Rotate the windows.ctrl + w
followed byx
Exchange current window with next window.ctrl + w
followed by=
Resize current window to fit.ctrl + w
followed by-
Decrease the height of current window.ctrl + w
followed by+
Increase the height of current window.ctrl + w
followed by<
Decrease the width of current window.ctrl + w
followed by>
Increase the width of current window.ctrl + o
Jump to previous cursor position.ctrl + i
Jump to next cursor position.
History
q:
Open command history. Equivalent to:history :
.q/
Search command history. Equivalent to:history /
.q?
Search command history. Equivalent to:history ?
.@:
Repeat last command.
Normal Mode
Mode defines the purpose for which vim is being used.
The default mode in vim is NORMAL mode.
All general commands are accessible in this mode.
Use ctrl + c
or ctrl + [
to activate NORMAL mode.
Use :
to switch to COMMAND mode:
ctrl + r
followed byctrl + f
Copy filename under cursor.ctrl + r
followed byctrl + w
Copy word under cursor.ctrl + r
followed byctrl + l
Copy line under cursor.
General Commands
:help
Vim’s help.:q
Quit vim. Use:q!
to Force quit vim.:w
Write file to disk.:w example.file
Write fileexample.file
to disk. Replaceexample.file
with name of file. Use:w! example.file
to overwrite fileexample.file
to disk.:wq
Write file to disk and quit.:x
Write file to disk and quit. Same as:wq
.:e example.file
Edit existing file. Replaceexample.file
with name of file.:pwd
Display present working directory.:cd example/directory
Change directory. Replaceexample/directory
with directory path.:reg
Display register. Equivalent to:register
.:s/example.pattern/replace.pattern/_
Substituteexample.pattern
withreplace.pattern
. Replaceexample.pattern
with search pattern.replace.pattern
can be left empty. Replace_
with&
,c
,g
,i
,I
,n
or leave empty.:g/example.pattern/_
Execute command globally. Replaceexample.pattern
with search pattern. Replace_
with command.:marks
Display list of marks.:marks _
Display specific mark. Replace_
with names of marks.:delm _
Delete mark. Replace_
with name of mark. Equivalent todelmarks _
.:delm!
Delete all marks. Equivalent todelmarks!
.:dig
Display digraphs. Equivalent to:digraphs
.:! example.command
Execute shell command. Replaceexample.command
with shell command.:!!
Execute last shell command.:r! example.command
Execute shell command and insert output. Equivalent to:read! example.command
. Replaceexample.command
with shell command.:r!!
Execute last shell command and insert output. Equivalent to:read!! example.command
.:foldo
Open all folds. Equivalent to:foldopen
.:foldo!
Open all folds recursively.:foldc
Close all folds. Equivalent to:foldclose
.:foldc!
Close all folds recursively.:foldd example.command
Execute command on all open folds. Equivalent to:folddoopen example.command
. Replaceexample.command
with command to execute.:folddoc example.command
Execute command on all closed folds. Equivalent to:folddoclosed example.command
. Replaceexample.command
with command to execute.:ab
List abbreviations. Equivalent to:abbreviate
. Use:iab
for INSERT mode abbreviations. Use:cab
for COMMAND mode abbreviations.:ab eg
List abbreviations beginning witheg
. Equivalent to:abbreviate eg
. Replaceeg
with any string. Use:iab eg
for INSERT mode abbreviations. Use:cab eg
for COMMAND mode abbreviations.:ab eg example given
Abbreviateexample given
witheg
. Equivalent to:abbreviate eg example given
. Replaceeg
with any string. Replaceexample given
with replacement strings. Use:iab eg example given
for INSERT mode abbreviations. Use:cab eg example given
for COMMAND mode abbreviations.:una eg
Remove abbreviationeg
. Equivalent to:unabbreviate eg
. Replaceeg
with abbreviation string. Use:iuna eg
for INSERT mode abbreviations. Use:cuna eg
for COMMAND mode abbreviations.:abc
Remove all abbreviations. Equivalent to:abclear
. Use:iabc
for INSERT mode abbreviations. Use:cabc
for COMMAND mode abbreviations.:split
Split file horizontally.:vsplit
Split file vertically.
Search
/example.pattern
Search pattern forward in current file. Replaceexample.pattern
with search pattern.?example.pattern
Search pattern backward in current file. Replaceexample.pattern
with search pattern./Example.Pattern\C
Search case sensitive pattern forward in current file. ReplaceExample.Pattern
with cased search pattern.?Example.Pattern\C
Search case sensitive pattern backward in current file. ReplaceExample.Pattern
with cased search pattern./example.pattern\c
Search case insensitive pattern forward in current file. Replaceexample.pattern
with search pattern.?example.pattern\c
Search case insensitive pattern backward in current file. Replaceexample.pattern
with search pattern.//
Search previous pattern.*
Search word under cursor forward.#
Search word under cursor backward.g*
Search pattern under cursor forward.g#
Search pattern under cursor backward.gn
Select last search result.n
Next search result.N
Previous search result.ctrl + g
Next search result from COMMAND mode. Requires:set incsearch
.ctrl + t
Previous search result from COMMAND mode. Requires:set incsearch
.
Navigation
h
Move one character left.j
Move one line down.k
Move one line up.l
Move one character right.w
Move one word forward.b
Move one word backward.0
Move to start of line.^
Move to first non whitespace character of line.$
Move to end of line.%
Move to the other bracket of the pair.f_
Find character forward on line. Replace_
with character.F_
Find character backward on line. Replace_
with character.m_
Move to character forward on line. Replace_
with character.M_
Move to character backward on line. Replace_
with character.;
Go forward on line. Requiresf_
orF_
orm_
orM_
.,
Go backward on line. Requiresf_
orF_
orm_
orM_
._G
Move to start of line. Replace_
with line number.G
Move to start of last line of file.gg
Move to start of first line of file. Equivalent to1G
.ctrl + u
Move half screen up.ctrl + d
Move half screen down.ctrl + y
Scroll upwards.ctrl + e
Scroll downwards.g;
Move to next position of change.g,
Move to previous position of change.gi
Move to last position of change and switch to INSERT mode.gI
Move to start of line and switch to INSERT mode.[m
Move to start of method.m]
Move to end of method.'_
Move to first non whitespace character on marked line. Replace_
with name of mark.`_
Move to position of mark. Replace_
with name of mark.[z
Move to start of fold.]z
Move to end of fold.zj
Move to start of next fold.zk
Move to start of previous fold.
Operations
Operators may be used in conjuction to navigation commands.
u
Undo.ctrl + r
Redo..
Repeat last command.dd
Copy and delete line.d$
Delete to end of line.dgg
Delete to beginning of file.dG
Delete to end of file.ggdG
Delete entire content of file.diw
Delete inside word.dip
Delete inside paragraph.ciw
Change inside word.p
Paste.&
Repeat last substitute.zi
Toggle foldenable.zf
Create fold. Requires selection in VISUAL mode.zd
Delete fold under cursor.zD
Delete fold under cursor recursively.zE
Eliminate all folds.zo
Open fold under cursor.zO
Open fold under cursor recursively.zc
Close fold under cursor.zC
Close fold under cursor recursively.za
Toggle fold under cursor.zA
Toggle fold under cursor recursively.zx
Open fold under cursor and close all other folds.zm
Close all folds.zM
Close all folds recursively.zr
Open all folds.zR
Open all folds recursively.ctrl + a
Increment next number on line.ctrl + x
Decrement next number on line.
Registers
"_
Set active register. Replace_
with name of register (a - z).q_
Start recording. Replace_
with name of register (a - z).q
Stop recording. Requiresq_
.@_
Execute register. Replace_
with name of register (a - z).
Marks
m_
Mark the current line. Replace_
with name of mark (a - z for local, A - Z for global).
Miscellaneous
gf
Open and edit file located at path under cursor.ctrl + w
followed byctrl + f
Open and edit file located at path under cursor in separate window.gx
Open file located at path under cursor.ga
Get code of the character under cursor.
Insert Mode
Insert content.
Use i
in NORMAL mode to INSERT before the current character.
Use a
in NORMAL mode to INSERT after the current character.
Use A
in NORMAL mode to INSERT after the end of line.
Use o
in NORMAL mode to INSERT below the current line.
Use O
in NORMAL mode to INSERT above the current line.
Operations and Navigation
ctrl + r
followed by_
Insert content of register. Replace_
with name of register.ctrl + r
followed by=
Expression register. Enter expression to evaluate and insert.ctrl + k
followed by__
Insert digraph. Replace__
with digraph code.ctrl + a
Insert last content that was inserted.ctrl + @
Insert last content that was inserted.ctrl + h
Delete character before cursor.ctrl + w
Delete word under cursor.ctrl + u
Delete everything before cursor.ctrl + t
Add indentation.ctrl + d
Remove indentation.ctrl + v
followed by_
Insert character. Replace_
with character literal. Equivalent toctrl + q
followed by_
.ctrl + g
followed byu
Split undo sequence.ctrl + x
followed byctrl + y
Scroll up.ctrl + x
followed byctrl + e
Scroll down.ctrl + x
followed byctrl + l
Whole line completion.ctrl + x
followed byctrl + f
File name completion.ctrl + x
followed bys
Spelling suggestion.ctrl + x
followed byctrl + v
Command-line completion.ctrl + x
followed byctrl + i
Path pattern completion.
Visual Mode
Modify, edit or copy selection.
Use v
in NORMAL mode.
Use shift + v
in NORMAL mode to activate VISUAL LINE mode.
Use ctrl + v
in NORMAL mode to activate VISUAL BLOCK mode.
Use ctrl + q
in NORMAL mode to activate VISUAL BLOCK mode.
Use gv
in NORMAL mode to select the last selection made using VISUAL mode.
Operations
o
Move to other end of selection. In VISUAL BLOCK mode.O
Move to other side of selection. In VISUAL BLOCK mode.R
Rewrite selection. Equivalent toS
Substitute selection.U
Uppercase selection.I
Insert at start of selected line.A
Append at end of selected line.c
Substitute selected lines.