Vim's efficiency and flexibility make it a top choice for developers. Here are 15 advanced Vim commands, complete with examples, to elevate your coding experience:
1. Macros - Record and Execute
Automate repetitive tasks.
- Record:
q<letter>
- Execute:
@<letter>
qa...q (record to 'a'), @a (execute 'a')
2. Global Replace with RegEx
Complex pattern-based replacements.
:%s/old/new/gc
3. Tabs and Buffers
Efficient multiple file handling.
- New tab:
:tabnew <filename>
- Switch tabs:
gt
,gT
:tabnew file.txt, gt, :ls, :b2
4. Window Splits
View files side-by-side.
- Vertical:
:vsp <filename>
- Horizontal:
:sp <filename>
:vsp file.txt
5. Folding - Code Organization
Hide/reveal code sections.
- Create fold:
zf#j
zf3j, zo, zc
6. :%
- Command Range
Apply commands to the entire file.
:%norm A;
7. c
- Change Command
Replace text efficiently.
cw
8. Visual Block Mode
Columnar text editing.
- Enter:
Ctrl+v
Ctrl+v, I#<Esc>
9. :g
- Global Command
Execute on lines matching a pattern.
:g/pattern/d
10. Advanced Search and Navigation
Navigate through files.
- Marks:
m<letter>
,'a
ma, 'a
11. u
and Ctrl+r
- Undo/Redo
Manage changes.
u, Ctrl+r
12. :r !<command>
- Insert Command Output
Insert external command output.
:r !date
13. :!
- Execute External Command
Run commands without leaving Vim.
:!ls
14. :diffthis
- Compare Files
Compare files in split view.
:vsp file.txt, :diffthis
15. :mksession
- Save Session
Save your current Vim session.
:mksession /path/to/session.vim