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
:matchto 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
:shto drop to a shell temporarily andexitto 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
\vfor 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),fzffor 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
-
:shto open shell, typeexitto 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)