Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@sirodoht
Last active December 5, 2016 15:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sirodoht/0fa000ca169f2417ff34b4bdadcf2fdf to your computer and use it in GitHub Desktop.
Save sirodoht/0fa000ca169f2417ff34b4bdadcf2fdf to your computer and use it in GitHub Desktop.
Vim provides many ways to move the cursor. Becoming familiar with them leads to more effective text editing.
h move one character left
j move one row down
k move one row up
l move one character right
w move to beginning of next word
b move to previous beginning of word
e move to end of word
W move to beginning of next word after a whitespace
B move to beginning of previous word before a whitespace
E move to end of word before a whitespace
All the above movements can be preceded by a count; e.g. 4j moves down 4 lines.
0 move to beginning of line
$ move to end of line
_ move to first non-blank character of the line
g_ move to last non-blank character of the line
gg move to first line
G move to last line
nG move to n'th line of file (n is a number; 12G moves to line 12)
H move to top of screen
M move to middle of screen
L move to bottom of screen
z. scroll the line with the cursor to the center of the screen
zt scroll the line with the cursor to the top
zb scroll the line with the cursor to the bottom
Ctrl-D move half-page down
Ctrl-U move half-page up
Ctrl-B page up
Ctrl-F page down
Ctrl-O jump to last (older) cursor position
Ctrl-I jump to next cursor position (after Ctrl-O)
Ctrl-Y move view pane up
Ctrl-E move view pane down
n next matching search pattern
N previous matching search pattern
* next whole word under cursor
# previous whole word under cursor
g* next matching search (not whole word) pattern under cursor
g# previous matching search (not whole word) pattern under cursor
% jump to matching bracket { } [ ] ( )
fX to next 'X' after cursor, in the same line (X is any character)
FX to previous 'X' before cursor (f and F put the cursor on X)
tX til next 'X' (similar to above, but cursor is before X)
TX til previous 'X'
; repeat above, in same direction
, repeat above, in reverse direction
de Duyuuuelete everything till the end of the word by pressing . at your heart's desire.
ci(xyz[Esc] -- This is a weird one. Here, the 'i' does not mean insert mode. Instead it means inside the parenthesis. So this sequence cuts the text inside parenthesis you're standing in and replaces it with "xyz". It also works inside square and figure brackets -- just do ci[ or ci{ correspondingly. Naturally, you can do di (if you just want to delete all text without typing anything. You can also do a instead of i if you want to delete the parentheses as well and not just text inside them.
ci" - cuts the text in current quotes
ciw - cuts the current word. This works just like the previous one except that ( is replaced with w.
C - cut the rest of the line and switch to insert mode.
ZZ -- save and close current file (WAY faster than Ctrl-F4 to close the current tab!)
ddp - move current line one row down
xp -- move current character one position to the right
U - uppercase, so viwU upercases the word
~ - switches case, so viw~ will reverse casing of entire word
Ctrl+u / Ctrl+d scroll the page half-a-screen up or down. This seems to be more useful than the usual full-screen paging as it makes it easier to see how the two screens relate. For those who still want to scroll entire screen at a time there's Ctrl+f for Forward and Ctrl+b for Backward. Ctrl+Y and Ctrl+E scroll down or up one line at a time.
Crazy but very useful command is zz -- it scrolls the screen to make this line appear in the middle. This is excellent for putting the piece of code you're working on in the center of your attention. Sibling commands -- zt and zb -- make this line the top or the bottom one on the sreen which is not quite as useful.
% finds and jumps to the matching parenthesis.
de -- delete from cursor to the end of the word (you can also do dE to delete until the next space)
bde -- delete the current word, from left to right delimiter
df[space] -- delete up until and including the next space
dt. -- delete until next dot
dd -- delete this entire line
ye (or yE) -- yanks text from here to the end of the word
ce - cuts through the end of the word
bye -- copies current word (makes me wonder what "hi" does!)
yy -- copies the current line
cc -- cuts the current line, you can also do S instead. There's also lower cap s which cuts current character and switches to insert mode.
viwy or viwc. Yank or change current word. Hit w multiple times to keep selecting each subsequent word, use b to move backwards
vi{ - select all text in figure brackets. va{ - select all text including {}s
vi(p - highlight everything inside the ()s and replace with the pasted text
b and e move the cursor word-by-word, similarly to how Ctrl+Arrows normally do. The definition of word is a little different though, as several consecutive delmiters are treated as one word. If you start at the middle of a word, pressing b will always get you to the beginning of the current word, and each consecutive b will jump to the beginning of the next word. Similarly, and easy to remember, e gets the cursor to the end of the current, and each subsequent, word.
similar to b/e, capital B and E move the cursor word-by-word using only whitespaces as delimiters.
capital D (take a deep breath) Deletes the rest of the line to the right of the cursor, same as Shift+End/Del in normal editors (notice 2 keypresses -- Shift+D -- instead of 3)
select v
select row(s) SHIFT + v
select blocks (columns) CTRL + v
indent selected text >
unindent selected text <
list buffers :ls
open buffer :bN (N = buffer number)
print :hardcopy
open a file :e /path/to/file.txt
:e C:\Path\To\File.txt
sort selected rows :sort
search for word under cursor *
open file under cursor gf
(absolute path or relative)
format selected code =
select contents of entire file ggVG
convert selected text to uppercase U
convert selected text to lowercase u
invert case of selected text ~
convert tabs to spaces :retab
start recording a macro qX (X = key to assign macro to)
stop recording a macro q
playback macro @X (X = key macro was assigned to)
replay previously played macro * @@
auto-complete a word you are typing ** CTRL + n
bookmark current place in file mX (X = key to assign bookmark to)
jump to bookmark `X (X = key bookmark was assigned to
` = back tick/tilde key)
show all bookmarks :marks
delete a bookmark :delm X (X = key bookmark to delete)
delete all bookmarks :delm!
split screen horizontally :split
split screen vertically :vsplit
navigating split screens CTRL + w + j = move down a screen
CTRL + w + k = move up a screen
CTRL + w + h = move left a screen
CTRL + w + l = move right a screen
close all other split screens :only
* - As with other commands in vi, you can playback a macro any number of times.
The following command would playback the macro assigned to the key `w' 100
times: 100@w
** - Vim uses words that exist in your current buffer and any other buffer you
may have open for auto-complete suggestions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment