The idea about this document is to take each section one at a time to learn and memorize the key combinations. Stay on the section until each command becomes muscle memory (1 day, 1 week, ...), then move onto the next section.
Key(s) | Action |
---|---|
i | Switch to INSERT mode, in which it does what you expect it to do when you type |
Esc | Switch back to NORMAL mode |
:, then q! | Exit (forced), which means it's okay to lose changes (:q! ) |
:, then wq | Write buffer to disk and exit (:wq ) |
Don't attempt to use well known shortcuts
- Ctrl+c
- Ctrl+x
- Ctrl+v
- Ctrl+s
- Ctrl+u
- Ctrl+y
They're just programmed to either do nothing or do something else.
Note: This is NOT how text is usually placed into the clipboard in Vim, but learning about VISUAL mode might help to keep the frustration level low during the first few days.
First thing to do is switch to VISUAL mode by pressing v, then move around to select text and do one of the following:
Key(s) | Action |
---|---|
y | Yank (copy) the selected text |
d | Delete (and copy) the selected text |
Pressing Esc switches back to NORMAL mode and finally:
Key(s) | Action |
---|---|
p | Paste what's is in the clipboard after where the cursor is. |
In addition to pasting with p, these are other quick edits that can be done without having to go into INSERT mode.
Key(s) | Action |
---|---|
x | Delete (and copy) the char under the cursor |
dd | Delete (and copy) the current line |
yy | Yank (copy) the current line |
h, j, k or l | For navigating instead of using the arrow keys |
r<char> |
Replace the character under the cursor with <char> |
~ | Toggle case of character under the cursor |
Applying combinations of the above:
Key(s) | Action |
---|---|
xp | Swap two characters |
ddp | Swap two lines |
And this is just the tip of how fast it can be to edit text in NORMAL mode in Vim.
Key(s) | Action |
---|---|
u | Undo |
Ctrl+r | Redo |
. | Repeat the last change |
Besides i that was already mentioned.
Key(s) | Action |
---|---|
a | Append after the cursor |
A | Append after the end of the current line |
I | Insert before the first non-blank character of current line |
o | Insert a new line after the current one |
O | Insert a new line before the current one |
cw | Replace from the cursor to the end of the word. This is composed of c (change) and w (word) |
In addition to h, j, k and l that were introduced in section 2, these are other basic motion keys:
Key(s) | Action |
---|---|
0 | Go to the first column (beginning of line) |
^ | Go to the first non-blank character of the line |
$ | Go to the end of line |
g_ | Go to the last non-blank character of line |
Key(s) | Action |
---|---|
/<pattern> |
Search forward for <pattern> |
?<pattern> |
Search backward for <pattern> |
n | Repeat the last / or ? |
N | Repeat the last / or ? in opposite direction |
It is important to point out that whenever a delete action is performed ( d, dd or x), those contents are placed in the clipboard. So, Vim does not have a pure delete action, there only exists cut.
Key(s) | Action |
---|---|
P | p is for pasting, but P (capitalized) is for pasting before. In the same fashion that o inserts a line after and O inserts one before |
X | You guessed right, it removes (and copies) the char before the cursor |
A Buffer is how Vim calls a file that is open and loaded into memory. As any modern editor, Vim can have more than one file open at the same time.
Note: Vim has tabs and some people use them in the same way they use tabs in other editors (one per open file), however this is a mistake. Tabs in Vim have different purpose: viewports, layouts or workspaces.
Key(s) | Action |
---|---|
:e <path> |
Edit (open for editing) file at <path> |
:w |
Write (save) |
:w <path> |
Write (save) a copy to <path> |
:saveas <path> |
Is self-explanatory. The difference with w <path> is this one updates the name of the buffer |
:wq , :x , ZZ |
Save and quit. :x only saves if necessary. Notice that ZZ does not start with colon : |
:q! |
Quit without saving |
:qa! |
Quit and close all buffers without saving. In case there are modified hidden buffers |
:ls |
List open buffers (open files) |
:bn |
Next buffer |
:bp |
Previous buffer |
:bd |
Delete (close) buffer (fails if isn't saved first) |
:bd! |
Delete (close) buffer without saving (forced) |
:bd <number> |
Delete (close) a specific buffer (fails if isn't saved first) |
:bd! <number> |
Delete (close) a specific buffer without saving (forced) |
Key(s) | Action |
---|---|
<N> G or :<N> |
Go to line N |
gg | Go to first line |
G | Go to last line |
w | Go to the start of the following word (letters and underscore) |
W | Go to the start of the following WORD (characters separated by blank) |
e | Go to the end of the current or next word |
E | Go to the end of the current or next WORD |
b | Go to the start of the current or previous word (analogous to e) |
B | Go to the start of the current or previous WORD (analogous to E) |
% | Go to the matching bracket: ( , { , [ , < , > , ] , } , ) |
* | Go to the next occurrence of the word under the cursor |
# | Go to the previous occurrence of the word under the cursor |
Key(s) | Action |
---|---|
<N><command> |
Repeat the command N times |
Key(s) | Action |
---|---|
5j | Move the cursor down 2 lines |
2l | Move the cursor 2 chars to the right |
2dd | Delete (and copy) 2 lines |
3p | Paste the text 3 times |
100 i, then type hi space, then Esc |
Write hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi |
. | Just after the last command writes "hi " 100 times again |
3. | Just after the last command writes "hi " 3 times (and not 300) |
Most commands can be used in the following general format:
<command><motion>
Key(s) | Action |
---|---|
0y$ | 0 moves to the beginning of the line, then y$ yanks from that position to the end of the line. |
ye | yank from the current cursor position to the end of the word |
y2/foo |
yank from the current cursor position to right before the second occurrence of "foo" |
What worked for y also works for d (delete), v (visual select), gU (uppercase), gu (lowercase), c (change).
cw was already introduced in a previous section as if it was a command by itself, but it is actually a composition of the change command and a movement.
0, ^, $ and g_ were already covered. But there are another 2 sets of useful commands for moving within the current line:
Key(s) | Action |
---|---|
f<char> |
Go to the next occurrence of <char> in the line |
F<char> |
Same as f<char> but backwards |
t<char> |
Go to just before the next occurrence of <char> in the line |
T<char> |
Same as t<char> but backwards |
And also...
Key(s) | Action |
---|---|
, | Go to the next occurrence of the last <char> found with f, F, t or T. |
; | Go to the previous occurrence of the last <char> found with f, F, t or T. |
Key(s) | Action |
---|---|
3fa | Go to the 3rd occurrence of a on this line |
dt" | Remove everything until the first " |
In section 11 <command><motion>
was introduced as the general format of edit
commands, but in reality there is a concept in Vim called text objects which
extends this to:
<command><motion or text object>
There are plain-text text objects:
- Words:
w
- Sentences:
s
- Parragraphs:
p
There are also common programming language text objects:
Type | Examples | Key |
---|---|---|
Strings | '...' , "..." , `...` |
', " or ` |
Parentheses | (...) |
( or ) |
Square brackets | [...] |
[ or ] |
Curly braces | {...} |
{ or } |
Markup language tags | <...> |
< or > |
Markup language tag blocks | <div>...</div> |
t |
Plus they can also be extended (create custom ones) by using Vimscript.
By combining the text object key with either
- i: Inner; do not include surrounding chars
- Or a: All; include surrounding chars
Key(s) | Action |
---|---|
daw | Delete the word (plus surrounding spaces) at which the cursor is. In contrast with dw, in this case it doesn't matter if the cursor is at the middle of the word, it will remove the whole word. |
diw | Delete just the word (not the surrounding spaces) |
das, dis | Same behavior for sentences |
dap, dip | Same behavior for paragraphs |
Key(s) | Action |
---|---|
ci" | Change the next quoted text in the line (do not remove the quotes) |
ca' | Change the next single quoted text in the line (remove also the single quotes) |
ca`, ca), ca} and ca> | Same behavior for different enclosing chars |
cit | Given the text <h2>Sample Title</h2> , the result is <h2></h2> |
When having the following text and the cursor positioned on the first o
:
(map (+) ("foo"))
^
Key(s) | Action |
---|---|
vi" | Select foo |
va" | Select "foo" |
vi) | Select "foo" |
va) | Select map (+) ("foo") |
v2i) | Select (map (+) ("foo")) |
- Registers
- Rectangular Blocks
- Marks
- Macros
- Plugins
- Basic Configuration
- Advanced Configuration
http://yannesposito.com/Scratch/en/blog/Learn-Vim-Progressively/