Vim¶
My cheatsheet for Vim
Logic¶
-
A|a
- Append / Arounda
ppend text after the cursorA
ppend text after the line
-
B|b
- Back / Beginning- move
b
ackwards one word - move
B
ackwards "big" word
- move
-
C|c
- Changec
hange inC
hange from here
-
D|d
- Deleted
elete code enclosed inD
elete from here |DD
delete line
-
E|e
- Ende
nd of a wordE
nd of a "big" word |E
nd of line2
-
F|f
- Findf
ind a characterF
ind backwards
-
G|g
- Findg
o to ||gg g
o to end of documentG
o to |GG G
o to beginning
-
I|i
- Inserti
nsert text before cursorI
nsert text before line
-
m
- Markm
ark position to register
-
N|n
- Nextn
ext result forwardN
ext result backward
-
O|o
- Open Lineo
pen line befo
reO
pen line abO
ve
-
P|p
- Past / Paragraphp
aste after cursor |in p
aragraphP
aste before cursor
-
q
- Re'q'ord Ma'q'ros- re
q
ord a macro to
- re
-
R|e
- Replace | Redor
eplace under the cursorR
eplace until stoppedR
edo (CTRL+R)
-
S|s
- Switchs
witch character (and keep on)S
witch line
-
T|t
- To- (do something)
t
o character T
o character (backwards)
- (do something)
-
U|u
-
Moving Inside a File¶
You can move the cursor within a file by single characters, words, tokens, or lines.
According to Vim, a word can be a group of letters, numbers, and underscores. On the other hand, a token is anything separated by whitespace and can include punctuation.
Additionally, you can move to different parts of a text by screen view.
Moving by Characters, Words and Tokens¶
The basic keys for moving the cursor by one character are:
- h – move the cursor left
- j – move the cursor down
- k – move the cursor up
- l – move the cursor right
You can also use these keys with a number as a prefix to move in a specified direction multiple times. For example, if you run 5j the cursor moves down 5 lines.
- b – move to the start of a word
- B – move to the start of a token
- w – move to the start of the next word
- W – move to the start of the next token
- e – move to the end of a word
- E – move to the end of a token
For instance, you have the noun phrase “step-by-step” as part of a text and the cursor is placed at the end of it. The first time you press b, the cursor moves back to “step-by-step”. However, if you use B, the cursor moves all the way back to: “step-by-step” since there is no whitespace between these characters.
Moving by Lines¶
- 0 (zero) – jump to the beginning of the line
- $ – jump to the end of the line
- ^ – jump to the first (non-blank) character of the line
-
G / #gg / :# – move to a specified line number (replace # with the line number)¶
Moving by Screens¶
The following commands are used as a quick way to move within the text without scrolling.
- Ctrl + b – move back one full screen
- Ctrl + f – move forward one full screen
- Ctrl + d – move forward ½ a screen
- Ctrl + u – move back ½ a screen
- Ctrl + e – move screen down one line (without moving the cursor)
- Ctrl + y – move screen up one line (without moving the cursor)
- Ctrl + o – move backward through the jump history
- Ctrl + i – move forward through the jump history
- H – move to the top of the screen (H=high)
- M – move to the middle of the screen (M=middle)
- L – move to the bottom of the screen (L=low)
Inserting Text¶
- i – switch to insert mode before the cursor
- I – insert text at the beginning of the line
- a – switch to insert mode after the cursor
- A – insert text at the end of the line
- o – open a new line below the current one
- O – open a new line above the current one
- ea – insert text at the end of the word
- Esc – exit insert mode; switch to command mode
Some of these commands switch between command and insert mode. By default, Vim launches in command mode, allowing you to move around and edit the file. To switch to command mode, use the Esc key.
Editing Text¶
r
– replace a single character (and return to command mode)cc
– replace an entire line (deletes the line and moves into insert mode)C / c$
– replace from the cursor to the end of a linecw
– replace from the cursor to the end of a words
– delete a character (and move into insert mode)J
– merge the line below to the current one with a space in between themgJ
– merge the line below to the current one with no space in between themu
– undoCtrl + r
– redo.
– repeat last command
Cutting, Copying And Pasting¶
yy
– copy (yank) entire line#yy
– copy the specified number of linesdd
– cut (delete) entire line#dd
– cut the specified number of linesp
– paste after the cursorP
– paste before the cursor
Marking Text (Visual Mode)¶
Apart from command mode and insert mode, Vim also includes visual mode. This mode is mainly used for marking text.
Based on the chunk of text you want to select, you can choose between three versions of visual mode: character mode, line mode, and block mode.
v
– select text using character modeV
– select lines using line modeCtrl+v
– select text using block modeo
– move from one end of the selected text to the otheraw
– select a wordab
– select a block with ()aB
– select a block with {}at
– select a block with <>ib
– select inner block with ()iB
– select inner block with {}it
– select inner block with <>
Visual Commands¶
Once you have selected the desired text in visual mode, you can use one of the visual commands to manipulate it. Some of them include:
y
– yank (copy) the marked textd
– delete (cut) the marked textp
– paste the text after the cursoru
– change the market text to lowercaseU
– change the market text to uppercase
Search in File¶
*
– jump to the next instance of the current word#
– jump to previous instance of the current word/pattern
– search forward for the specified pattern?pattern
– search backward for the specified patternn
– repeat the search in the same directionN
– repeat the search in the opposite direction
Saving and Exiting File¶
:w
– save the file:wq
/ :x / ZZ – save and close the file:q
– quit:q!/ ZQ
– quit without saving changes:w new_file_name
– save the file under a new name and continue editing the original:sav
– save the file under a new name and continue editing the new copy:w !sudo tee %
– write out the file using sudo and tee command
Working with Multiple Files¶
:e file_name
– open a file in a new buffer:bn
– move to the next buffer:bp
– go back to previous buffer:bd
– close buffer:b#
– move to the specified buffer (by number):b
file_name – move to a buffer (by name):ls
– list all open buffers:sp file_name
– open a file in a new buffer and split viewport horizontally:vs file_name
– open a file in a new buffer and split viewport vertically:vert ba
– edit all files as vertical viewports:tab ba
– edit all buffers as tabsgt
– move to next tabgT
– move to previous tabCtrl+ws
– split viewportCtrl+wv
– split viewport verticallyCtrl+ww
– switch viewportsCtrl+wq
– quit a viewportCtrl+wx
– exchange current viewport with next oneCtrl+=
– make all viewports equal in height and width
Marks and Jumps¶
- m[a-z] – mark text using character mode (from a to z)
- M[a-z] – mark lines using line mode (from a to z)
- `a - jump to position marked a
y
a – yank text to position marked >a>- `. – jump to last change in file
- `0 – jump to position where Vim was last exited
- `` – jump to last jump
- :marks – list all marks
- :jumps – list all jumps
:changes
– list all changesCtrl+i
– move to next instance in jump listCtrl+o
– move to previous instance in jump listg,
– move to next instance in change listg;
– move to previous instance in change list
Macros¶
qa
– record macro aq
– stop recording macro@a
– run macro a@@
– run last macro again
Enabling Vim Color Schemes¶
:colorscheme [colorscheme_name]
– change to specified scheme:colorscheme [space]+Ctrl+d
– list available Vim color scheme