vi - VIsual editor

↑ Up

My shell-based visual editor of choice is vi. It is a very powerful and ubiquitous tool, but involves an initial learning curve. The content of this entire website was written/edited inside of vi. Note that you can save your preferred configuration in the user config file ~/.vimrc.

For a good primer, enter the command vimtutor into the shell, and you will be given an interactive tutorial in vi. From there, I recommend practice, and referencing this keybinding cheatsheet:

Cursor movement

h - move left
j - move down
k - move up
l - move right
w - jump by start of words (punctuation considered words)
W - jump by words (spaces separate words)
e - jump to end of words (punctuation considered words)
E - jump to end of words (no punctuation)
b - jump backward by words (punctuation considered words)
B - jump backward by words (no punctuation)
0 - (zero) start of line
^ - first non-blank character of line
$ - end of line
a - another test line
G - Go To command (prefix with number - 5G goes to line 5)
n - Move to nth line of file
H - Move to top of screen
M - Move to middle of screen
L - Move to bottom of screen
% - Move to associated (), {}, [] i.e. find matching parenthesis
fc - Move forward to next occurence of char c in line
Fc - Move backward to next occurence of char c in line

Note: Prefix a cursor movement command with a number to repeat it. For example, 4j moves down 4 lines.

Insert Mode - Inserting/Appending text

i - start insert mode at cursor
I - insert at the beginning of the line
a - append after the cursor
A - append at the end of the line
o - open a blank line below
O - open a blank line above
ea - append at end of word
Esc - exit insert mode

Editing

r - replace a single character (does not use insert mode)
J - join line below to the current one
cc - change (replace) an entire line
cw - change (replace) to the end of word
ce - change (replace) to the end of word
c$ - change (replace) to the end of line
s - delete character at cursor and subsitute text
S - delete line at cursor and substitute text (same as cc)
xp - transpose two letters (delete and paste, technically)
u - undo
U - undo changes to the whole line
. - repeat last command

Marking text (visual mode)

v - start visual mode, mark lines, do commands
V - start Linewise visual mode
o - move to other end of marked area
Ctrl+v - start visual block mode
O - move to Other corner of block
aw - mark a word
ab - mark a () block (with braces)
aB - mark a {} block (with brackets)
ib - mark an inner () block
iB - mark an inner {} block
Esc - exit visual mode

Highlighting

:noh - turn off highlighting

Visual commands

> - shift right
< - shift left
y - yank (copy) marked text
d - delete marked text
~ - switch case

Cut and Paste

yy - yank (copy) a lin
2yy - yank 2 line
yw - yank wor
y$ - yank to end of lin
p - put (paste) the clipboard after curso
P - put (paste) before curso
dd - delete (cut) a lin
dw - delete (cut) the current wor
D - delete after curser to end of lin
x - delete (cut) current characte

Exiting

:w - write (save) the file, but don’t exit
:wq - write (save) and quit
:q - quit (fails if anything has changed
:q! - quit and throw away change
:x - same as w
:'<,'> w filename - save the highlighted text into the filename, only need to type : with highlighted text

Search/Replace

/pattern - search for pattern
?pattern - search backward for pattern
n - repeat search in same direction
N - repeat search in opposite direction
:%s/old/new/g - replace all old with new throughout file (%) and multiple instances per line (g)
:%s/old/new/gc - replace all old with new throughout file with confirmation
:s/old/new - replace old with new, first instance on the line
:n,m s/old/new/g - replace old with new everywhere on lines n to m

Opening files (aka “buffers”)

:e filename - Edit a file in a new buffer
:e \**/*foo<Tab> - ** stands for any subdirectory, <Tab> denotes tab-completion
:next \**/*.dat - open multiple files (with some extension)
:b4 - switch to buffer number
:bn - switch to next buffer in the buffer list
:bp - switch to previous buffer in the buffer list
:bf - switch to first buffer in the buffer list
:bl - switch to last buffer in the buffer list
:b foo<Tab> - switch by buffer name with tab-completion
:b# - switch to the alternate file
:bnext (or :bn) - go to next buffer
:bprev (of :bp) - go to previous buffer
:bd - delete a buffer (close a file)
:sp filename - Open a file in a new buffer and split window
ctrl+ws - Split window
ctrl+ww - switch between window
ctrl+wq - Quit a window
ctrl+wv - Split windows vertically
:ls - show list of loaded buffer

Running external prompt (bash) commands in vim

:!command - show results of command executed outside of vi (temporarily) :read !command - puts results of command into buffer at the cursor line
Note: This means that you can effectively make vi into a powerful “modern” IDE!