Freebie

vim

vim is a modal text editor available on virtually every Linux system. It operates in distinct modes: normal mode for navigation and commands, and insert mode for typing text. Press Esc to return to normal mode at any time.

vim.nox

vim.nox is a minimal vim build for server environments. It includes scripting language support but omits the GUI components of full vim.

.vimrc

.vimrc is your personal vim configuration file, stored in your home directory. Settings here apply every time you open vim. It is part of the broader dot file system for managing shell and tool configuration. A system-wide configuration file also exists at /etc/vimrc.

For example, enable line numbers on startup:

# ~/.vimrc
set number

Start vim without loading .vimrc to test settings in isolation:

vim -u NONE

Basic Navigation

Navigate in normal mode without entering insert mode:

CommandDescription
hMove left
jMove down
kMove up
lMove right
wMove to the beginning of the next word
3wMove to the 3rd word to the right
bMove to the beginning of the previous word
3bMove to the 3rd word to the left
eMove to the end of the current word
0Move to the beginning of the line
$Move to the end of the line
ggGo to the first line of the file
GGo to the last line of the file
Ctrl + uMove up half a screen
Ctrl + dMove down half a screen
(Move to the beginning of the previous sentence
)Move to the beginning of the next sentence

Editing

Commands for entering insert mode, deleting, yanking, and pasting text:

CommandDescription
iInsert mode at the cursor
IInsert mode at the beginning of the line
aAppend mode after the cursor
AAppend mode at the end of the line
oOpen a new line below the current line
OOpen a new line above the current line
rReplace the character under the cursor
xDelete the character under the cursor
XDelete the character before the cursor
ddDelete the current line
99ddDelete 99 lines
dwDelete from the cursor to the end of the word
3wdDelete the 3 words to the right
dbDelete the word to the left
3dbDelete the 3 words to the left
d$Delete from the cursor to the end of the line
d0Delete from the cursor to the beginning of the line
dasDelete the current sentence
yyCopy (yank) the current line
3yyCopy three lines starting from the cursor
ywCopy (yank) the current word
yiwCopy the word under the cursor
y$Copy (yank) to the end of the line
y^Copy from the cursor to the start of the line
y%Copy to the matching bracket ((), {}, [])
pPaste after the cursor
5pPaste 5 times
PPaste before the cursor
uUndo
Ctrl + rRedo

Visual Mode

Visual mode lets you select text before applying a command. Three selection types are available:

CommandDescription
vStart visual mode (character selection)
VStart visual line mode
vjjjHighlight the current line and 3 lines below
vjjjyyHighlight and copy three lines
Ctrl + vStart visual block mode
yYank (copy) the selected text
dDelete the selected text
>Indent the selected text
<Un-indent the selected text

Search and Replace

Search and replace commands work in normal mode:

CommandDescription
/patternSearch forward for pattern
?patternSearch backward for pattern
nRepeat the search in the same direction
NRepeat the search in the opposite direction
:%s/old/new/gReplace all occurrences of old with new in the entire file
:%s/old/new/gcReplace all occurrences with confirmation

Working with Multiple Files

vim loads each file into a buffer. Switch between buffers without closing the editor:

CommandDescription
:e filenameOpen a file
:wSave the current file
:w filenameSave as filename
:qQuit vim
:wqSave and quit
:q!Quit without saving
:bnGo to the next buffer
:bpGo to the previous buffer
:bdDelete a buffer

Useful Commands

Runtime settings and shell integration commands:

CommandDescription
:set nuShow line numbers
:set nonuHide line numbers
:syntax onEnable syntax highlighting
:syntax offDisable syntax highlighting
:set pasteEnable paste mode
:set nopasteDisable paste mode
:set tabstop=4Set tab width to 4 spaces
:set expandtabConvert tabs to spaces
:set shiftwidth=4Set indentation width to 4 spaces
:! <command>Run a shell command without leaving vim

Split windows

Split the editor to view multiple files at once. Use Ctrl+w twice to cycle between panes:

CommandDescription
:sp /path/to/fileSplit horizontally and open a file
:vs /path/to/fileSplit vertically and open a file
Ctrl+w Ctrl+wSwitch between open panes

Exiting Vim

Exit commands work from normal mode. Press Esc first if you are in insert mode:

CommandDescription
:wSave the file
:qQuit vim
:wq or ZZSave and quit
:q!Quit without saving