Mastering Vim Basic

Why Use Vim?

Vim is a powerful, highly configurable text editor designed for efficient text editing. While it has a steep learning curve, mastering Vim significantly boosts productivity for developers, system administrators, and power users.

If you’re new, start with Vim extensions in Visual Studio Code (VSC) to smoothly transition:

  • Install the Vim extension in VSC.
  • Open the command palette (cmd + shift + p) and find Preferences: Open Keyboard Shortcuts (JSON).
  • Add this shortcut for toggling Vim mode:
{
  "key": "ctrl+shift+[IntlBackslash]", // macOS: "ctrl+shift+[Backquote]"
  "command": "toggleVim"
}

Essential Navigation

Horizontal Movement

  • 0: Beginning of line
  • ^: First non-blank character
  • $: End of line

Vertical Movement

  • j / k: Move down/up one line
  • { / }: Move up/down by paragraph
  • CTRL-D / CTRL-U: Scroll half-page down/up

Word Navigation

  • w: Next word start
  • b: Previous word start
  • e: Next word end

Combine commands with numbers:

  • 2w: Move forward two words

Fast Searching

  • /pattern: Search pattern forward (n: next, N: previous)
  • ?pattern: Search pattern backward
  • f<char>: Jump to next occurrence of <char>
  • t<char>: Jump just before <char>
  • ; / ,: Repeat or reverse the last inline search

Advanced Movements

  • gd: Go to definition
  • gf: Open file under cursor
  • G: End of file
  • gg: Beginning of file
  • %: Match brackets {[()]}
  • Ctrl + O / Ctrl + I: Navigate cursor position history

Basic Editing

  • yy: Copy line
  • dd: Delete line
  • p: Paste below
  • P: Paste above
  • u: Undo
  • CTRL-R: Redo
  • xp: Swap two characters
  • ddp: Swap two lines
  • J: Join lines
  • r<char>: Replace character under cursor
  • ci(: Change inside parentheses
  • cs'": Change single quotes to double quotes

Indentation and Comments

  • Visual select (Ctrl+v), press I to insert comments (//), then Esc.
  • <n>>> / <n><<: Indent or dedent multiple lines.

File Operations

  • :e <file>: Edit file
  • :r <file>: Read (insert) file content
  • Save partial file:
    :100,$w <file>       # save from line 100 to end
    :100,$w >> <file>    # append to file

Window Management

  • vim -o <file1> <file2>: Open files horizontally split
  • vim -O <file1> <file2>: Open files vertically split
  • Splitting inside Vim:
    • Horizontal: :split <file> (CTRL + w s)
    • Vertical: :vsplit <file> (CTRL + w v)
  • Window navigation: <n>CTRL + w + h/j/k/l
  • Resize: CTRL + w + < / > / - / + / =
  • Close window: CTRL + w q
  • Rotate: CTRL + w r
  • Exchange windows: CTRL + w x
  • Close others: CTRL + w o

Tabs

  • :tabnew: New tab
  • :tabn: Next tab
  • :tabp: Previous tab
  • :tabclose: Close current tab
  • :tabonly: Close other tabs

Marks

  • Mark line: m<char> (e.g., ma)
  • Jump to mark: 'a
  • Exact position: `a

Registers

  • Store text: "a7yy (store 7 lines in register ‘a’)
  • Paste from register: "ap
  • View registers: :reg

Folding

  • Create fold: zf4j (fold 4 lines down)
  • Open/close folds: zO / zc
  • Delete folds: zd
  • Open/close all folds: zR / zM

Line Numbers Configuration

  • Enable numbers: :set number
  • Relative numbers (useful for jumps like 2j): :set relativenumber
  • Toggle dynamically (recommended .vimrc config):
syntax on
set number
augroup numbertoggle
  autocmd!
  autocmd BufEnter,FocusGained,InsertLeave,WinEnter * if &nu && mode() != "i" | set rnu | endif
  autocmd BufLeave,FocusLost,InsertEnter,WinLeave   * if &nu | set nornu | endif
augroup END
set incsearch
set mouse=a

Powerful Substitution

  • Replace globally: :%s/old/new/g
  • Replace with confirmation: :%s/old/new/gc
  • HTML formatting: :%s/HEY/<b>&<\/b>/g
  • Case manipulation: :%s/[Hh]elp/\U&/g

Integration with Shell

Format selected text with external commands:

:!jq

Advanced File Handling

  • Binary files: vim -b <file>
  • Diff files: vimdiff <oldfile> <newfile>
  • Read-only mode: vim -R <file>
  • Execute commands at start: vim -c /pattern <file>

Browser Integration

Enhance browsing with Vim-style shortcuts:

  • Chrome: Vimium
  • Safari: Vimari

Useful Resources

Conclusion

Vim is incredibly efficient once mastered. Consistent practice and gradual incorporation into your workflow can vastly improve your productivity.