Vim is a powerful and highly customizable text editor, known for its efficiency and wide array of commands. Whether you're a beginner looking to get started or an advanced user wanting to master more complex commands, this cheat sheet covers everything you need to know.
Table of Contents
- Basic Commands
- Navigation
- Editing Text
- Searching and Replacing
- Working with Multiple Files
- Advanced Commands
- Command Combinations and Shortcuts
- Customization
- Conclusion
Basic Commands
These are essential commands to get started with Vim.
- Opening Vim:
vim <filename>
-
Insert Mode:
-
i
: Insert at cursor -
I
: Insert at the beginning of the line -
a
: Append after cursor -
A
: Append at the end of the line -
o
: Open a new line below -
O
: Open a new line above
-
-
Saving and Exiting:
-
:w
: Save changes -
:q
: Quit -
:wq
orZZ
: Save and quit -
:q!
: Quit without saving
-
Navigation
Efficient navigation is key to mastering Vim.
-
Basic Movement:
-
h
: Move left -
j
: Move down -
k
: Move up -
l
: Move right
-
-
Word Navigation:
-
w
: Next word -
b
: Previous word -
e
: End of current/next word
-
-
Line Navigation:
-
0
: Beginning of line -
^
: First non-blank character of line -
$
: End of line
-
-
File Navigation:
-
gg
: Go to the top of the file -
G
: Go to the bottom of the file -
:n
: Go to line numbern
-
Editing Text
These commands help you modify and edit text in various ways.
-
Deleting Text:
-
x
: Delete character under cursor -
dw
: Delete word -
dd
: Delete current line
-
-
Copying and Pasting:
-
yy
: Copy (yank) current line -
p
: Paste after cursor -
P
: Paste before cursor
-
-
Undo and Redo:
-
u
: Undo -
Ctrl + r
: Redo
-
-
Replacing Text:
-
r<char>
: Replace character under cursor with<char>
-
R
: Enter replace mode (overwrites characters)
-
Searching and Replacing
Vim’s search and replace functionality allows you to locate and modify text efficiently.
-
Search:
-
/pattern
: Search forward forpattern
-
?pattern
: Search backward forpattern
-
n
: Repeat search in the same direction -
N
: Repeat search in the opposite direction
-
-
Replace:
-
:s/old/new
: Replace first instance ofold
withnew
on the current line -
:s/old/new/g
: Replace all instances on the current line -
:%s/old/new/g
: Replace all instances in the file -
:%s/old/new/gc
: Replace all with confirmation
-
Working with Multiple Files
Vim can manage multiple files in the same session, ideal for complex projects.
- Opening Multiple Files:
vim <file1> <file2>
-
Switching Between Files:
-
:n
: Go to the next file -
:prev
: Go to the previous file -
:e filename
: Edit a specific file
-
-
Working with Buffers:
-
:ls
: List open buffers -
:b<number>
: Switch to buffer<number>
-
:bd
: Close (delete) the current buffer
-
-
Using Split Windows:
-
:split filename
: Open file in horizontal split -
:vsplit filename
: Open file in vertical split -
Ctrl + w, w
: Switch between splits -
Ctrl + w, q
: Quit split
-
Advanced Commands
These commands take you to the next level of Vim mastery.
-
Macros:
-
q<register>
: Start recording a macro in<register>
-
<commands>
: Execute the desired commands -
q
: Stop recording -
@<register>
: Run the macro stored in<register>
-
@@
: Repeat last macro
-
-
Registers:
-
"<register>y
: Yank to a specific register -
"<register>p
: Paste from a specific register
-
-
Marks:
-
m<letter>
: Set mark<letter>
at the cursor position -
'<letter>
: Jump to the mark<letter>
-
'<
: Jump to the beginning of the line with mark<letter>
-
-
Command-Line Mode:
-
:!command
: Run an external command (e.g.,:!ls
to list files) -
:sh
: Temporarily open a shell from Vim -
Ctrl + z
: Suspend Vim (resume withfg
in the shell)
-
Command Combinations and Shortcuts
These combinations allow you to execute more complex tasks with simple commands.
-
Select All and Copy:
-
ggVGy
: Selects all text in the file and copies it. -
gg
: Go to the beginning of the file. -
V
: Start visual line mode. -
G
: Extend the selection to the end of the file. -
y
: Yank (copy) the selected text.
-
-
Delete All Text:
-
ggVGd
: Selects and deletes all text in the file.
-
-
Change All Text to Uppercase:
-
ggVGgU
: Selects all text and changes it to uppercase.
-
-
Indent All Text:
-
gg=G
: Auto-indents all lines in the file.
-
-
Join All Lines:
-
ggVGJ
: Joins all lines in the file into a single line.
-
Customization
Vim is highly customizable through configuration settings.
-
Basic Configuration:
-
:set number
: Show line numbers -
:set relativenumber
: Show relative line numbers -
:set autoindent
: Enable automatic indentation -
:set ignorecase
: Ignore case in searches -
:set smartcase
: Overrideignorecase
if uppercase letters are used in search
-
-
Saving Configurations:
- To make these settings permanent, add them to your
.vimrc
file:
set number set relativenumber set autoindent set ignorecase set smartcase
- To make these settings permanent, add them to your
Conclusion
This Vim cheat sheet is designed to help users from beginner to advanced levels. By mastering these commands, you’ll be able to navigate, edit, and customize Vim to improve productivity and efficiency. Remember, Vim has a steep learning curve, so practice regularly to become proficient.
For more in-depth customization and usage, refer to the Vim documentation.
Happy editing!
Top comments (0)