Vim - 101 hacks
Tips & Tricks from Vim 101 Hacks
Navigation
Scroll line
Ctrl + E
: Scroll down one lineCtrl + Y
: Scroll up one line
word vs WORD
word
consists of a sequence of letters, digits and underscores.WORD
consists of a sequence of non-blank characters, separated with white space.
For example: 192.168.1.3
contains 7 words. But the whole 192.168.1.3
is
considered as one WORD.
Position cursor at specific location within a line
0 ^ line_content g_ $
0
: Go to the starting of current line.$
: Go to the end of current line.^
: To to the first non-blank char of current line.g_
: Go to the last non blank character of current line.
Screen Navigation
H
: First line of current screen. (H
is “home”)M
: Middle line of current screen.L
: Last line of current screen.
Redraw screen with current line on Top/Bottom/Middle
Navigation Key | Description |
---|---|
z |
Redraw the screen with current line under the cursor at the top of the screen |
z- | Redraw the screen with current line under the cursor at the bottom of the screen |
z. | Redraw the screen with current line under the cursor at the middle of the screen |
Go to Top/Bottom of the File
- Go to the top:
:0
,gg
,1G
. - Go to the bottom:
:$
,G
Vim command line navigation
Command line | Description |
---|---|
vim +143 |
Go to the 143rd line of file |
vim +/search-term |
Go to the first match of the specified search term from top |
vim +?search-term | Go to the first match of the specified search term from |
vim -t TAG | Go to the specific tag |
Basic Text Manipulation
Insert or Append Text
Key | Description |
---|---|
:r FILENAME | Insert another file content into current file after the current line |
:r! COMMAND | Insert output of a command into current file after the current line |
Paste before or affter the copied Line/Word/Others
:reg
=> Show all recent register
Recent deleted content will appear in 0 - 9 register. If you would like to paste the word from the register number 3, execute the following:
"3p
Or paste clipboard content to editor in normal mode:
"*p
Expert Text Manipulation
Sort File Content
:sort
# sort visual block
:'<,'>!sort
:sort! # Sort in descending order
:sort i # Ignore case while sorting
:sort u # Remove duplicate lines. U stands for unique
Vim Command Line Hacks
# Open file in Readonly mode
vim -R filename.txt
# Skip loading Plugins Temporarily
vim --noplugin filename.txt
# Edit multiple files in Tabs
vim -p file1 file2 file3
Encrypt File in Vim
:X
Enter encryption key. Next time you open that file, Vim will prompt for the encryption key.
Review the Differences between Files using Vimdiff
vimdiff file1 file2
Default registers and their users
Key | Description |
---|---|
% | Name of the current file |
# | Name of the alternate file |
: | Most recent executed command line |
/ | Last search pattern |
“ | Last used registers |
Search across Multiple Files using vimgrep
:vimgrep jason *.txt
# Use :cn to jump to the next match
# Use :clist to view all the files that matched the vimgrep search keyword