A curated collection of essential and advanced Vim tips, commands, and techniques to boost your productivity and mastery of Vim.
0. Editing Techniques
🧭 1. Cursor Movement (Navigation)
Jumping Around the File
-
gg
— Go to top of file -
G
— Go to bottom of file -
nG
— Go to linen
-
H
/M
/L
— Top / Middle / Bottom of screen
Word-wise Navigation
-
w
— Start of next word -
e
— End of current/next word -
b
— Beginning of current/previous word -
ge
— End of previous word
Paragraph & Sentence
-
{
/}
— Previous / next paragraph -
(
/)
— Previous / next sentence
Matching Pairs
-
%
— Jump between matching()
,{}
,[]
, etc.
Search-Based Movement
-
/pattern
— Search forward -
?pattern
— Search backward -
n
/N
— Repeat last search forward / backward
📋 2. Copy (Yank) and Paste
Basic Copy
-
yy
— Yank (copy) entire line -
yw
— Yank word -
y$
— Yank to end of line -
y0
— Yank to start of line -
yG
— Yank to end of file -
v
+ movement +y
— Yank visually selected text
Paste
-
p
— Paste after cursor -
P
— Paste before cursor
System Clipboard (if enabled)
-
"+y
— Yank to system clipboard -
"+p
— Paste from system clipboard
❌ 3. Deletion (Cutting)
-
dd
— Delete current line -
dw
— Delete word -
d$
— Delete to end of line -
d0
— Delete to start of line -
dG
— Delete to end of file -
v
+ movement +d
— Delete visually selected text
Use "_d
to delete without affecting yank register (black hole).
🔍 4. Search and Replace
Basic Search
-
/pattern
— Search forward -
?pattern
— Search backward -
n
/N
— Repeat search
Replace Examples
:%s/foo/bar/g " Replace all 'foo' with 'bar'
:%s/foo/bar/gc " Confirm each replacement
:'<,'>s/foo/bar/g " Replace in visual selection
Regex Tips
-
\v
— Very magic mode (simpler regex syntax)
:%s/\v\d+/NUMBER/g " Replace all numbers with "NUMBER"
🎨 5. Highlighting
- Search highlights automatically (
:set hlsearch
) - Remove highlights manually:
:noh
- Use
:match
to manually highlight patterns:
:match ErrorMsg /TODO/
🧹 6. Formatting and Indentation
Auto-indent Whole File
gg=G
-
gg
— Go to top -
=G
— Indent everything to the bottom
Indent Selections
-
>
— Indent one level -
<
— Un-indent one level -
=
— Reindent (smart based on syntax)
Use in visual mode:
-
V
+ select lines +=
— Format selected lines
⌨️ Bonus: Repeat and Dot Command
-
.
— Repeat the last change (powerful for repeating deletions, replacements, etc.) - Use
.
after actions likeciw
,dw
, etc., to repeat efficiently.
1. Basic Editing and Navigation
Search and Replace
:%s/foo/bar/gc " Find 'foo', replace with 'bar', confirm each
Save and Quit
:w " Save
:w filename " Save as
:q " Quit
:q! " Force quit (discard changes)
:wq or :x " Save and quit
Undo and Redo
u " Undo
Ctrl-r " Redo
2. Window Management
Splits and Windows
Command | Abbreviation | Description |
---|---|---|
:split |
:sp |
Horizontal split |
:vsplit |
:vsp |
Vertical split |
:new |
:new |
New empty buffer (horizontal) |
:vnew |
:vne |
New empty buffer (vertical) |
:close |
:clo |
Close current split |
:only |
:on |
Close all other splits |
Navigation between splits:
Ctrl-w + h/j/k/l " Move left/down/up/right between splits
Tabs (optional to explore)
:tabnew filename " Open file in new tab
:tabn " Next tab
:tabp " Previous tab
3. Buffers and Files
Buffers
:e filename " Open file (edit buffer)
:bn / :bp " Next / Previous buffer
:bd " Delete (close) buffer
:ls or :buffers " List open buffers
File Explorer (NetRW)
:e . " Open file explorer
4. Visual Mode
Enter Visual Mode
-
v
— character-wise selection -
V
— line-wise selection -
Ctrl-v
— block-wise (column) selection
Visual Mode Operations
Command | Effect |
---|---|
d |
Delete selection |
y |
Yank (copy) selection |
c |
Change selection (delete + insert) |
> / <
|
Indent / un-indent selection |
~ |
Toggle case |
u / U
|
Make lowercase / uppercase |
:s/foo/bar/g |
Search & replace in selection |
Visual Block Mode Special
- Insert text in multiple lines: select block, press
I
, type text, thenEsc
- Useful for column edits
Other Visual Tips
-
gv
— reselect previous visual selection -
o
— move cursor to other end of selection -
Ctrl-g
— show selection info
5. Registers and Clipboard
-
"ayy
— Yank current line into registera
-
"ap
— Paste from registera
-
"_d
— Delete without affecting default register (black hole) -
:reg
— View contents of all registers
6. Marks and Navigation
-
ma
— Set marka
-
'a
— Jump to start of line of marka
-
`a
— Jump to exact position of marka
7. Macros
-
qa
— Start recording macro into registera
- Perform actions
-
q
— Stop recording -
@a
— Play macroa
-
@@
— Repeat last macro -
10@a
— Play macro 10 times
8. Command-Line Mode and Shell Integration
- Run shell command:
:!ls
- Insert shell output into file:
:r !ls
- Filter lines through shell command:
:'<,'>!sort
- Use
:sh
to drop to a shell temporarily andexit
to return
9. Folding
-
zf{motion}
— Create fold -
zo
/zc
— Open / close fold -
za
— Toggle fold -
:set foldmethod=syntax
— Syntax-based folding -
:set foldmethod=manual
— Manual folding mode
10. Search with Regular Expressions
- Use
\v
for very magic mode (simplified regex) - Example:
:%s/\v(foo|bar)/baz/g
- Search forward:
/foo
- Search backward:
?foo
- Repeat search:
n
(forward),N
(backward) - Clear highlights:
:noh
11. Sessions
- Save session:
:mksession ~/session.vim
- Load session:
vim -S ~/session.vim
12. Custom Key Mappings
- Normal mode mapping:
nnoremap <leader>r :source %<CR> " Reload current vim config
- Visual mode mapping:
vnoremap <leader>c "+y " Copy selection to system clipboard
-
<leader>
key is usually\
by default
13. Integration with External Tools
- Use
grep
,ag
(The Silver Searcher),rg
(ripgrep),fzf
for fast search/navigation - Example:
:grep keyword **/*.txt
:copen " Open quickfix list to see results
14. Text Objects
Command | Description |
---|---|
ciw |
Change inside word |
ci" |
Change inside quotes |
di( |
Delete inside parentheses |
va{ |
Visual select a block including braces |
Combine text objects with operators for powerful editing.
15. Undo Branching and Time Travel
- Go back in undo history:
:earlier 10m " 10 minutes ago
- Go forward in undo history:
:later 5 " 5 changes forward
Bonus Tips
Running Vim in Background
- Suspend Vim:
Ctrl-Z
- List jobs:
jobs
- Resume Vim:
fg
Drop to Shell Temporarily
-
:sh
to open shell, typeexit
to return
Vim customization in .vimrc
" Appearance and UI
set number " Show line numbers
set relativenumber " Show relative line numbers
set cursorline " Highlight current line
set cursorcolumn " Highlight current column
set colorcolumn=80 " Show a vertical ruler at column 80
set showmatch " Highlight matching parentheses
set matchtime=2 " Delay (tenths of a second) for match highlight
set laststatus=2 " Always show the status line
set scrolloff=5 " Keep 5 lines visible above/below cursor
set signcolumn=yes " Always show the sign column (useful with git)
set termguicolors " Enable true color support
" Searching
set ignorecase " Ignore case in search
set smartcase " Override ignorecase if uppercase used
set incsearch " Show matches as you type
set hlsearch " Highlight all search results
set wrapscan " Wrap around end of file when searching
" Indentation and Tabs
set tabstop=4 " Number of spaces per tab
set shiftwidth=4 " Spaces per indent level
set softtabstop=4 " Backspace/delete over 4 spaces
set expandtab " Use spaces instead of tabs
set autoindent " Copy indent from current line
set smartindent " Smart indenting on new lines
set cindent " C-style indentation
set breakindent " Keep indentation on wrapped lines
" Editing and Behavior
set backspace=indent,eol,start " Make backspace behave normally
set whichwrap+=<,>,[,] " Allow left/right arrow to wrap to next/prev line
set hidden " Allow buffer switching without saving
set undofile " Persistent undo
set autoread " Auto-reload file if changed externally
set confirm " Ask before closing unsaved changes
set splitbelow " Open horizontal splits below
set splitright " Open vertical splits to the right
" File and Clipboard
set clipboard=unnamedplus " Use system clipboard
set fileencoding=utf-8 " Default file encoding
set noswapfile " Disable swap files
set nobackup " Disable backups
set nowritebackup " Avoid write backup
" Mouse and Navigation
set mouse=a " Enable mouse support
set scrolljump=5 " Minimal scroll when moving cursor
set sidescroll=1 " Minimal horizontal scroll
set wildmenu " Show command completion menu
set wildmode=longest:full,full " Completion behavior
" Display Commands
set ruler " Show cursor position in bottom right
set showcmd " Show partial command in status line
set showmode " Show insert/visual/replace mode
" Performance
set lazyredraw " Don't redraw while executing macros
set ttyfast " Faster terminal scrolling
" Set paste
set paste
set paste temporarily disables auto-indentation and other formatting behaviors that can mess up pasted text, especially in insert mode (e.g., when copying from another source or terminal).
:set nopaste
To disable set paste
syntax on
set number
set paste
set tabstop=4
set shiftwidth=4
set expandtab
set autoindent
set smartindent
set cindent
set ruler
set showcmd
set mouse=a
set clipboard=unnamedplus
// for 42 school
autocmd BufNewFile *.c :Stdheader
Top comments (0)