In: computer, text-editor. Also: emacs-cheatsheet.
Backlinks: emacs-cheatsheet.
Basic movement
-
h = left 1 character
-
l = right 1 character
-
j = down 1 character
-
k = up 1 character
-
w = jump forward to the next word (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 to the previous word (punctuation considered words)
-
B = jump backward by words (no punctuation)
-
H = move to top of screen
-
M = move to middle of screen
-
L = move to bottom of screen
-
} = go forward by paragraph (the next blank line)
-
{ = go backward by paragraph (the next blank line)
-
[m = move to the start of a method
-
]m = move to the end of a method
-
0 = (zero) jump to the start of line
-
^ = jump to first non-blank character of line (same as 0w)
-
$ = jump to the end of line
-
Ctrl+u = move cursor up half a screen
-
Ctrl+d = move cursor down half a screen
-
Ctrl+f = move cursor up (page up)
-
Ctrl+b = move cursor down (page down)
-
gg = jump to the first line of the document
-
G = jump to the last line of the document
-
[linenum]G = move the cursor to the beginning of an arbitrary line
-
: [linenum] [enter] = jump to that line in the document
-
[num]| - go to column “num” on current line (e.g. 10| goes to col 10)
-
Ctrl+o = retrace your movements in file backwards (from jump list)
-
Ctrl+i = retrace your movements in file forwards
-
g; = jump to a newer change (from change list)
-
g, = jump to a older change (from change list)
-
zt = screen to top
-
zb = screen to bottom
-
zz = screen center
Search character (move horizontally)
-
f [character] = =f=ind a character after the cursor
-
F [character] = =f=ind a character before the cursor
-
t [character] = move =t=ill a character after the cursor
-
T [character] = move =t=ill a character before the cursor
-
; (semicolon) = go to the next searched item
-
, (comma) = go the previous searched item
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 (gn is better though)
-
:%s/old/new/gc = replace all old with new throughout file with confirmations
Insert, Append, Edit
-
i = start =i=nsert mode at cursor
-
I = start insert mode at the start of line (same as ^, then i)
-
a = =a=ppend after the cursor
-
A = append at the end of the line (same as $, then a)
-
o = =o=pen (append) blank line after current line (no need to press “enter”)
-
O = open blank line above current line (same as A
) -
ea = insert (append) at the end of the word
-
cc = change (replace) an entire line
-
c [movement command] = change (replace) from the cursor to the move-to point
-
C = change (replace) to the end of the line
-
example: “ce” = changes from the cursor to the end of the cursor word
-
r [character] = replace a single character with the specified char (doesn’t use Insert mode)
-
d [movement command] = delete from the cursor to the move-to point
-
example: “de” deletes from the cursor to the end of the current word
-
example: “d$” will delete from the cursor, to the end of line. You can also use the alias “D”.
-
dd = delete the current line
-
J = join line below to the current one
-
Ctrl-t, Ctrl-d = Indent/un-indent in insert mode
-
Esc or Ctrl+c or Ctrl+[ = exit edit mode
Reference: https://neovim.io/doc/user/usr_04.html
Some operator-motion commands are used so often that they have been given a single letter command:
-
x stands for
dl
(delete character under the cursor) -
X stands for
dh
(delete character left of the cursor) -
D stands for
d$
(delete to end of the line) -
C stands for
c$
(change to end of the line) -
s stands for
cl
(change one character) -
S stands for
cc
(change a whole line)
Cut and Paste
-
yy - yank (copy) a line
-
y$ - yank (copy) to end of line
-
p - put (paste) the clipboard after cursor (like
a
) -
P - put (paste) before cursor (like
i
) -
dd - delete (cut) a line
-
d$ - delete (cut) to the end of the line
-
D - delete (cut) to the end of the line
-
x - delete (cut) current character (like delete key)
-
X - delete (cut) previous character (like backspace key)
Note: To make it consistent with D
, it’s common to add a mapping for Y
in vimrc, like: nnoremap Y y$
Visual mode
-
v - start visual mode
-
V - start linewise visual mode
-
Ctrl+v - start visual block mode
-
from here you can move around as in normal mode (h, j, k, l, etc) and can then do a command (such as y, d, or c)
-
o - move to other end of marked area
-
O - move to other corner of block
-
Esc or Ctrl+c - exit visual mode
Visual commands
-
y - yank (copy) marked text
-
d - delete marked text
-
c - delete the marked text and go into insert mode (like c does)
Change case
-
~ = toggle the case of this character
-
g~[m] = toggle case with motion “m”
-
gU[m] = uppercase
-
gu[m] = lowercase
General
-
u - undo
-
U - restore (undo) last changed line
-
Ctrl+r - redo
-
. - repeat last change
-
@: - repeat the last command executed
-
:e filename - Edit a file
-
ZZ - save! & quit
-
ZQ - quit! without save
Working with multiple files
VIM has 4 layers of abstraction:
-
the buffers
-
the windows
-
the tabs
-
the argument list
Buffers
A buffer directly matches an open file in memory. If you close a window containing a buffer, the buffer is still there, hidden.
-
:buffers - buffer list
-
:buffer [IDorname] - move to the buffer using its ID or its name
-
:bnext or :bn - move to the next buffer
-
:bprevious or :bp - move to the previous buffer
-
:bfirst or :bf - move to the first buffer
-
:blast or :bl - move to the last buffer
-
Ctrl+^ - switch to the alternative buffer. It’s indicated in your buffer list with the symbol #.
-
[ID]Ctrl+^ - switch to a specific buffer with ID . For example, 75 Ctrl-^ switch to the buffer ID 75.
-
:bufdo [command] - apply a command to all buffers
-
:buffers! or ls! - list all buffers (including unlisted)
Windows
A window is just a space you can use to display the content of a buffer. Don’t forget: when you close the window, the buffer stays open.
-
:new - create a new window
-
:vsp - vertically split windows
-
Ctrl+w s - split the current window horizontally
-
Ctrl+w v - split the current window vertically
-
Ctrl+w n - split the current windows horizontally and edit a new file
-
Ctrl+w ^ - split the current with the alternate file (buffer with the # indicator in your buffer list)
-
[ID]Ctrl+W ^ - split windows with the buffer of ID . For example, 75 Ctrl-W ^ will open a window with the buffer ID 75.
To move the cursor from one window to another:
- Ctrl+w j / Ctrl+w k / Ctrl+w h / Ctrl+w l
To move the windows:
-
Ctrl+w r - rotate the windows
-
Ctrl+w x - exchange with the next window
Resize the windows:
-
Ctrl+W = - Resize windows for them to fit on the screen with the same size
-
Ctrl+W - - Decrease window’s height
-
Ctrl+W + - Increase window’s height
-
Ctrl+W < - Decrease window’s width
-
Ctrl+W > - Increase window’s width
Tabs
So a buffer is an open file, and a window is the container for an active buffer.
The tabs are a container for a bunch of windows. It’s very different than the concept of tabs in a standard IDE!
-
:tabnew or :tabe - Open a new tab
-
:tabclose or :tabc - Close the current tab
-
:tabonly or :tabo - Close every other tab except the current one
To move from tab to tab:
-
gt - go to the next tab
-
gT - go to the previous tab
-
you can also add a count before the last two keystrokes. For example, 1gT go to the 1st tab (tabs are indexed from 1)
Argument List (arglist)
The argument list (also called arglist) is the fourth and last container allowing you to organize your open files.
It’s useful to see it as a stable subset of the buffer list.
Every file in the arglist will be in the buffer list.
Some buffers in the buffer list won’t be in the arglist.
The files you want to open when you run Vim - such as executingvim file1 file2 file3
- will be automatically added to the arglist and to the buffer list.
-
:args - display the arglist
-
:argadd - add file to the arglist
-
:argdo - execute a command on every file in the arglist
Mapping Keystrokes
You can add mapping commands for every Vim mode:
-
:nmap = create new mapping for NORMAL mode
-
:imap = create new mapping for INSERT mode
-
:xmap = create new mapping for VISUAL mode
-
:nnoremap = create mapping for NORMAL mode (non recursive)
-
:inoremap = create mapping for INSERT mode (non recursive)
-
:vnoremap = create mapping for VISUAL mode (non recursive)
Marks and positions
-
'. = jump to last modification line (from change list)
-
`. = jump to the position of the last change in this file
-
`“ - jump to the position when last editing this file
-
`` = jump to the position before the last jump
-
`0 = jump to the position where Vim was previously exited
Combos
-
diw = delete inside the word. It will delete the current word under the cursor.
-
ciw = change inside the word. It will delete the current word under the cursor and switch to INSERT mode.
-
dip = delete inside the paragraph
-
Vu = lowercase line
-
VU = UPPERCASE line
-
g~~ = invert case
-
vEU = switch word to uppercase
-
vE~ = modify word case
-
ddp = move line down
-
ddkP = move line up (swap one line with the line above I usually do the following)