This document contains 50+ Vim tips and tricks to help you become a more efficient Vim user. These tips cover everything from basic navigation to advanced editing techniques.
Opening Files
Open a file to a specific line number
vim +34 filename
Opens
filename
and jumps to line 34.Open a file in Vim from the command line
vim filename
Cursor Positioning
Move cursor to the top of the screen
PressH
(capital H).Move cursor to the middle of the screen
PressM
(capital M).Move cursor to the bottom of the screen
PressL
(capital L).
Deleting and Changing Text
Delete a line
Pressdd
.Delete a word
Pressdw
(deletes from cursor to end of word).Delete a single character
Pressdl
orx
.Delete multiple lines
Press5dd
to delete 5 lines.Change a word
Presscw
(deletes the word and enters insert mode).Change a single character
Presscl
ors
.Change text inside parentheses
Pressci(
orci)
.Change text inside quotes
Pressci"
orci'
.Change text inside brackets
Pressci[
orci]
.
Quitting and Saving
Quit without saving
PressZQ
(capital Z followed by Q).Save and quit
PressZZ
(capital Z twice).Save a file
Press:w
.Quit without saving changes
Press:q!
.
Navigating Lines
Jump to a specific line number
Press:100
to jump to line 100.Jump down 10 lines
Press10j
.Jump up 5 lines
Press5k
.Jump to matching bracket
Press%
.
Search and Replace
Search for a word
Press/word
and hit Enter.Search and replace globally
:%s/old/new/g
Replaces
old
withnew
throughout the file.Search for two words
/word1\_.*word2
Clear search highlights
Press:noh
.Search for the word under the cursor
Press*
to search forward,#
to search backward.
Copying and Pasting
Yank (copy) a line
Pressyy
.Yank a word
Pressyw
.Yank a single character
Pressyl
.Paste after the cursor
Pressp
.Paste before the cursor
PressP
.
Working with Buffers
Switch between buffers
PressCtrl + o
to go back,Ctrl + i
to go forward.Open a file in a new buffer
:e filename
Split window horizontally
:sp filename
Split window vertically
:vs filename
Close the current buffer
Press:q
.
File Management
Rename a file
:saveas newfilename
Move a file to a different directory
:w /path/to/newlocation/filename
Open a file manager in Vim
:Ex
Advanced Editing
Insert output of a command into the file
:r !command
Example:
:r !ls
inserts the output ofls
.Sort lines
In visual mode, select lines and press:!sort
.Repeat the last command
Press.
(period).Auto-complete in insert mode
PressCtrl + n
.Delete blank lines
:g/^$/d
Visual Mode
Select character by character
Pressv
.Select line by line
PressV
(capital V).Select block (column-wise)
PressCtrl + v
.Select inside parentheses
Pressvi(
orvi)
.Select inside quotes
Pressvi"
orvi'
.Select inside brackets
Pressvi[
orvi]
.Select a paragraph
Pressvip
.
Miscellaneous Tips
Undo changes
Pressu
.Redo changes
PressCtrl + r
.Reload Vim configuration
:source ~/.vimrc
Search for lines containing a word
:g/word
Search for lines containing two words
:g/word1.*word2
Insert current date and time
:r !date
Run the current line as a shell command
:!!sh
Switch two characters
Pressxp
.
Conclusion
These tips and tricks will help you become more proficient in Vim. Practice them regularly to build muscle memory and improve your workflow.