DEV Community

Wakeup Flower
Wakeup Flower

Posted on

Ultimate Vim mannual

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 line n
  • 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
Enter fullscreen mode Exit fullscreen mode

Regex Tips

  • \v โ€” Very magic mode (simpler regex syntax)
:%s/\v\d+/NUMBER/g   " Replace all numbers with "NUMBER"
Enter fullscreen mode Exit fullscreen mode

๐ŸŽจ 5. Highlighting

  • Search highlights automatically (:set hlsearch)
  • Remove highlights manually:
:noh
Enter fullscreen mode Exit fullscreen mode
  • Use :match to manually highlight patterns:
:match ErrorMsg /TODO/
Enter fullscreen mode Exit fullscreen mode

๐Ÿงน 6. Formatting and Indentation

Auto-indent Whole File

gg=G
Enter fullscreen mode Exit fullscreen mode
  • 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 like ciw, 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
Enter fullscreen mode Exit fullscreen mode

Undo and Redo

u                   " Undo
Ctrl-r              " Redo
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Tabs (optional to explore)

:tabnew filename    " Open file in new tab
:tabn               " Next tab
:tabp               " Previous tab
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

File Explorer (NetRW)

:e .                " Open file explorer
Enter fullscreen mode Exit fullscreen mode

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, then Esc
  • 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 register a
  • "ap โ€” Paste from register a
  • "_d โ€” Delete without affecting default register (black hole)
  • :reg โ€” View contents of all registers

6. Marks and Navigation

  • ma โ€” Set mark a
  • 'a โ€” Jump to start of line of mark a
  • `a โ€” Jump to exact position of mark a

7. Macros

  • qa โ€” Start recording macro into register a
  • Perform actions
  • q โ€” Stop recording
  • @a โ€” Play macro a
  • @@ โ€” 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 and exit 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
Enter fullscreen mode Exit fullscreen mode
  • Search forward: /foo
  • Search backward: ?foo
  • Repeat search: n (forward), N (backward)
  • Clear highlights: :noh

11. Sessions

  • Save session:
:mksession ~/session.vim
Enter fullscreen mode Exit fullscreen mode
  • Load session:
vim -S ~/session.vim
Enter fullscreen mode Exit fullscreen mode

12. Custom Key Mappings

  • Normal mode mapping:
nnoremap <leader>r :source %<CR>   " Reload current vim config
Enter fullscreen mode Exit fullscreen mode
  • Visual mode mapping:
vnoremap <leader>c "+y             " Copy selection to system clipboard
Enter fullscreen mode Exit fullscreen mode
  • <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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • Go forward in undo history:
:later 5        " 5 changes forward
Enter fullscreen mode Exit fullscreen mode

Bonus Tips

Running Vim in Background

  • Suspend Vim: Ctrl-Z
  • List jobs: jobs
  • Resume Vim: fg

Drop to Shell Temporarily

  • :sh to open shell, type exit 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 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)