Skip to content

Instantly share code, notes, and snippets.

@abdulhalim-cu
Last active October 21, 2017 06:04
Show Gist options
  • Save abdulhalim-cu/e0b5a326a5ef453ce436032a23f3ac54 to your computer and use it in GitHub Desktop.
Save abdulhalim-cu/e0b5a326a5ef453ce436032a23f3ac54 to your computer and use it in GitHub Desktop.
Mastering Vim
1st Level
Normal Mode:
i → Insert mode. Type ESC to return to Normal Mode
x → Delete the char under the cursor
:wq → Save & quit (:w save, :q quit)
dd → Delete (and Copy) the current line
P → Paste
hjkl → Basic cursor mode (←↓↑→)
:help <command> to get general help
2nd Level
Insert Mode Variations:
a → insert after the cursor
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
Basic Moves:
Scroll Up & down:
<C-E> → Scroll the window down
<C-Y> → Scroll the window up
<C-F> → Scroll down one page
<C-B> → Scroll up one page
H → Move cursor to the top of the window
M → Move cursor to the middle of the window
L → Move cursor to the bottom of the window
gg → go to the top of file
G → go to bottom of file
0 → go to the first column
^ → go to the first non-blank character of the line
$ → go to the end of the line
g_ → go the the last non-blank character of line
/pattern → search for pattern
Motions
a → all
i → in
t → till
f → find forward
F → find backward
Commands
d → delete (also cut)
c → change (delete, then place in insert mode)
y → yank (copy)
v → visually select
Copy/ Paste:
P → Paste before position
p → paste after current position
yy → Copy the current line, easier but equivalent to ddP
Undo /Redo:
u → Undo
Ctrl-r or <C-r> → Redo
Load/ Save/ Quit/ Change File (Buffer):
:e <path/to/file> → Open
:w → Save
:saveas <path/to/file> → to save
:x, ZZ, or :wq → save and quit (:x only save if necessary)
:q! → quit without saving, also :qa! To quit even if there are modified hidden buffer
:bn (:bp) → Show next (previous) file (buffer)
Fun Words:
diw → delete in word
di), di}, di] → delele inside (), {}, [] respectively
caw → change all word
yi), yi}, yi] → yank all text inside (), {}, [] respectively
3rd Level
Better:
. → (dot) will repeat the last command
N. → (N=1,2,3,4,5,6…) → Will repeat the command N times
2dd → will delete 2 lines
3p → will paste the text 3 times
100isudo [ESC] → will write “sudo sudo sudo … 100 times
100 → 100 times, i → insert mode, sudo → write sudo word
. → Just after the last command will write again the 100 sudo
3. → will write sudo 3 times
Stronger:
NG → Go to line N
gg → Go to start of the file (equivalent to 1G)
G → Go to last line
Word Moves:
w → Go to the start of the following word
e → go to end of the following word
W → go to start of the following word
E → go to end of the following word
Efficient Moves:
% → Go to corresponding (, {, [
* (#) → go to next(previous) occurrence of the word under the cursor
Faster:
<start position><command><end position>
For example: 0y$ means
0 → go to beginning of this line
y → yank from here
$ → up to the end of this line
Also
ye → yank from here to the end of the word
But also
y2/foo yank up to the second occurrence of “foo”
4th Level
Move on current line:
0 → go to column 0
^ → go to first character on the line
$ → go to the last column
g_ → go to last character on the line
fa → go to next occurrence of the letter `a` on the line
, (;) will find next (previous) occurrence
t, → go to just before the character ,
t; → go to just before the character ;
3fa → find the 3rd occurrence of `a` on this line
F & T → Like f and t but backward
dt” → remove everything until the “
dt; → remove everything until the ;
Zone Selection
<action>a<object> or <action>i<object>
<action> → d (delete), y (yank), v (visual mode)
<object> → w (a word), W (a WORD )
S ( a sentence)
P ( a paragraph)
Also “, ‘, ), }, ]
Select Rectangular Blocks: <C-v>
Typically: 0<C-v><C-d>I-- [ESC]
^ → go to the first non-blank character of the line
<C-v> → start block selection
<C-d> → Move down (could also be jjj or % )
I-- [ESC] → Write -- to the comment each line
Completion: <C-n> and <C-p>
In insert mode just type the start of a word, then type <C-p> or <C-n> magic…
Macros: qa do something q, @a, @@
Qa record your action in the register a. Then @a will replay the macro saved into the register a as if you typed it. @@ is a shortcut to replay the last executed macro.
Example:
On a line containing only the number 1, type this
qaYp<C-a>q
qa → Start recording
Yp → duplicate this line
<C-a> → increment the number
q → stop recording
@a → write 2 under the 1
@@ → write 3 under the 2
Now do 100@@ will create a list of increasing numbers until 103
Visual Selection: v,V,<C-v>
<C-v> and <C-V> for visual selection
J → Join all the lines together
< (>) → indent to the left (right)
= → auto indent
<C-v>
Go to the line (jjj or <C-d> or /pattern or % etc..)
$ → go to the end of the line
A → write text [ESC].
Splits: :split and vsplit
:split → create a split (:vsplit create a vertical split)
<C-w><dir> → where dir is any of hjkl or ←↓↑→ to change the split.
<C-w>_ ( resp. <C-w>| ) → maximize the size of the split (resp. Vertical split)
<C-w>+ ( resp. <C-w>- ) → Grow (resp. shrink) split.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment