logo
eng-flag

Vim Cheatsheet

Table of Contents

  1. Modes
  2. Basic Movement
  3. Editing
  4. Searching and Replacing
  5. Cut, Copy, and Paste
  6. Working with Multiple Files
  7. Visual Mode
  8. Macros
  9. Folding
  10. Marks
  11. Registers
  12. Customization
  13. Advanced Commands

Modes

Vim has several modes:

  • Normal Mode: Default mode for navigation and manipulation
  • Insert Mode: For inserting text
  • Visual Mode: For selecting text
  • Command-line Mode: For entering commands

Switching Modes

  • Normal to Insert: i (insert before cursor), a (append after cursor)
  • Insert to Normal: Esc or Ctrl+[
  • Normal to Visual: v (character-wise), V (line-wise), Ctrl+v (block-wise)
  • Normal to Command-line: : (ex commands), / (forward search), ? (backward search)

Basic Movement

Character Movement

  • Left: h
  • Down: j
  • Up: k
  • Right: l

Word Movement

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

Line Movement

  • Line start: 0
  • First non-blank character: ^
  • Line end: $

Screen Movement

  • Top of screen: H
  • Middle of screen: M
  • Bottom of screen: L

File Movement

  • Go to line number: :n (e.g., :5 goes to line 5)
  • File start: gg
  • File end: G

Scrolling

  • Scroll down half-screen: Ctrl+d
  • Scroll up half-screen: Ctrl+u

Example:

(In Normal mode)
5j     # Move down 5 lines
3w     # Move forward 3 words
$      # Move to end of line
gg     # Go to start of file

Editing

Inserting Text

  • Insert before cursor: i
  • Insert at beginning of line: I
  • Append after cursor: a
  • Append at end of line: A
  • Open new line below: o
  • Open new line above: O

Deleting Text

  • Delete character: x
  • Delete line: dd
  • Delete to end of line: D
  • Delete word: dw

Changing Text

  • Change character: r
  • Change word: cw
  • Change to end of line: C
  • Change entire line: cc

Undo and Redo

  • Undo: u
  • Redo: Ctrl+r

Example:

(In Normal mode)
i Hello, World!  # Insert "Hello, World!"
Esc              # Return to Normal mode
cw Vim           # Change word to "Vim"

Searching and Replacing

Searching

  • Search forward: /pattern
  • Search backward: ?pattern
  • Next occurrence: n
  • Previous occurrence: N

Replacing

  • Replace on current line: :s/old/new/
  • Replace all occurrences in file: :%s/old/new/g
  • Replace with confirmation: :%s/old/new/gc

Example:

/TODO         # Search for "TODO"
:%s/foo/bar/g # Replace all "foo" with "bar" in the file

Cut, Copy, and Paste

Yanking (Copying)

  • Yank line: yy
  • Yank word: yw

Deleting (Cutting)

  • Delete (cut) line: dd
  • Delete (cut) word: dw

Pasting

  • Paste after cursor: p
  • Paste before cursor: P

Example:

yy    # Yank current line
3j    # Move down 3 lines
p     # Paste the yanked line

Working with Multiple Files

Buffers

  • List buffers: :ls
  • Next buffer: :bn
  • Previous buffer: :bp
  • Specific buffer: :b<number>

Windows

  • Split horizontally: :sp or :split
  • Split vertically: :vsp or :vsplit
  • Move between windows: Ctrl+w followed by h, j, k, or l
  • Close current window: :q

Tabs

  • Open new tab: :tabnew
  • Next tab: gt
  • Previous tab: gT
  • Close tab: :tabclose

Example:

:vsp newfile.txt  # Open newfile.txt in vertical split
Ctrl+w l          # Move to right window
:tabnew           # Open new tab
gt                # Go to next tab

Visual Mode

  • Start visual mode: v
  • Start line-wise visual mode: V
  • Start block-wise visual mode: Ctrl+v

Operations in Visual Mode

  • Yank selection: y
  • Delete selection: d
  • Change selection: c

Example:

v3w    # Select three words
y      # Yank selection
p      # Paste selection

Macros

  • Record macro: q<letter> to start, q to stop
  • Play macro: @<letter>
  • Repeat last macro: @@

Example:

qa        # Start recording macro 'a'
i(  )     # Insert parentheses
Escf)     # Move cursor to closing parenthesis
@a        # Play macro 'a'

Folding

  • Toggle fold: za
  • Open fold: zo
  • Close fold: zc
  • Open all folds: zR
  • Close all folds: zM

Marks

  • Set mark: m<letter>
  • Jump to mark: '<letter>
  • Jump to exact position of mark: ```

Example:

ma        # Set mark 'a'
'a        # Jump to line of mark 'a'
`a        # Jump to exact position of mark 'a'

Registers

  • Yank to register: "<letter>y
  • Paste from register: "<letter>p
  • View registers: :reg

Example:

"ayy      # Yank current line into register 'a'
"ap       # Paste content of register 'a'

Customization

Vim configuration file: ~/.vimrc

Common settings:

set number         " Show line numbers
set syntax=on      " Enable syntax highlighting
set autoindent     " Enable auto-indenting
set tabstop=4      " Set tab width to 4 spaces
set expandtab      " Use spaces instead of tabs

Advanced Commands

  • Repeat last command: .
  • Execute shell command: :!command
  • Filter text through external command: !!
  • Open file under cursor: gf
  • Jump to definition: Ctrl+]
  • Return from jump: Ctrl+t
  • Complete word: Ctrl+n (in Insert mode)

Example:

:!ls        # Run 'ls' command
!!sort      # Sort current line using external 'sort' command

2024 © All rights reserved - buraxta.com