Mastering Vim: Essential Commands and Tips

Vim, a powerful text editor, allows for efficient text manipulation using various commands and modes. Below is a comprehensive cheat sheet covering the most commonly used functionalities in Vim.

Basics

  • :help keyword: Display help for keyword.
  • :sav[eas] file: Save the current session to a file.
  • :clo[se]: Close current pane.
  • :ter[minal]: Open an embedded terminal within Vim.
  • K: Opens the man page for the word under the cursor.

Tip: Run vimtutor for a hands-on introduction to Vim basics.

Navigation Commands

  • h/j/k/l: Move cursor left, down, up, and right.
  • H/M/L: Move to the top, middle, or bottom of the screen.
  • w/b/e: Jump forwards/backwards to the start/end of a word.
  • W/E/B: Same as w/b/e, but words include punctuation.
  • 0/^/$/g_: Move to the beginning, first non-blank character, end, and last non-blank character of the line.

Advanced Movement

  • gg/G: Go to the start/end of the file.
  • %: Move to matching character (parentheses, braces).
  • f/t/F/T: Jump to the next/previous occurrence of a character.
  • {}: Jump between paragraphs or functions/blocks.

Editing

  • i/a/o/O: Insert mode: insert text before cursor, append after cursor, open a line below or above.
  • r/R: Replace one character or enter replace mode.
  • cc/cw/ciw: Change (replace) entire line, to the end of the word, or the entire word.
  • dd/dw/diw: Delete line, word from cursor, or entire word.
  • x/p/P: Delete character under cursor, paste text after/before cursor.

Visual Mode (Select Text)

  • v/V/<C-v>: Start visual mode, expand selection line-wise or block-wise.
  • y/d/>/<: Yank (copy), delete, or indent selected text.

Registers

  • :reg: Show content of all registers.
  • "xy: Yank into register x.
  • "xp: Paste contents of register x.

Macros

  • qa/q: Start and stop recording a macro in register ‘a’.
  • @a: Execute macro stored in register ‘a’.

Undo/Redo

  • u: Undo.
  • Ctrl + r: Redo.

Marks and Jumps

  • m + letter: Set a mark with the specified letter.
  • “` letter**“: Jump to the specified mark.

Files and Buffers

  • :e file: Edit a file.
  • :bn/:bp: Switch to the next/previous buffer.
  • :bd: Delete (close) a buffer.

Tabs and Windows

  • :tabnew: Open a new tab.
  • gt/gT: Go to next/previous tab.
  • Ctrl + w + s/v/T: Split window horizontally/vertically, or move to a new tab.

Search and Replace

  • /pattern: Search for a pattern.
  • :%s/old/new/g: Replace all occurrences of ‘old’ with ‘new’ in the file.

Diff and Merge Tools

  • :diffthis: Mark the current window as part of a diff comparison.
  • :diffoff: Turn off diff mode.
  • :diffupdate: Update diff calculations.

Exiting

  • :w: Save the file.
  • :wq/:x/ZZ: Save and quit.
  • :q: Quit Vim (fails if there are unsaved changes).

Tips and Tricks

  • Prefix commands with a number: Performing an action n times (e.g., 5dd to delete 5 lines).
  • .vimrc: Customize Vim settings by editing the .vimrc file in your home directory.
  • ZZ: Save and exit Vim efficiently using this command.

Expanded Vim Cheat Sheet Topics

1. Macros

  • Macros in Vim allow you to record and replay a sequence of commands, automating repetitive tasks. Initiate a macro recording by pressing q followed by a register key (e.g., a to z) where the macro will be stored. Perform the desired actions, then press q again to stop recording. To execute the recorded macro, use @ followed by the register key (e.g., @a). To run it multiple times, precede with a number (e.g., 10@a).

2. Global Commands

  • The :global command is powerful for running a series of commands on all lines that match a specified pattern. Syntax: :g/{pattern}/{command}. For example, :g/foo/d would delete all lines containing “foo”. It’s an indispensable command for batch edits in large files.

3. Magic Mode

  • Regular expressions in Vim can be simplified using the magic mode triggered by \v. In this mode, most characters that usually need to be escaped ({}, (), +, -) do not require a backslash, simplifying complex regex operations. For instance, \v1+1=2 matches the string 1+1=2 without needing extra escapes.

4. Search Plugins

  • :FZF: Vim can integrate with the Fuzzy Finder (fzf), a general-purpose command-line finder, to open files rapidly or jump to desired locations in files. Install the fzf plugin and then use :FZF to start searching across files.
  • :Ack!: Similar to grep but optimized for programmers, Ack! can be used to search through the contents of files for specific strings or patterns. It’s especially useful in large codebases, providing more targeted search capabilities than built-in Vim searches.

5. Window Management

  • Vim provides robust window management commands:
    • Ctrl-w _: Maximize the current window vertically.
    • Ctrl-w |: Maximize the current window horizontally.
    • Ctrl-w H/J/K/L: Move the current window to the far left, down, up, or right, respectively.
      These commands enhance multitasking within Vim by allowing detailed layout control of multiple files or views.

6. Viewports (Splits)

  • Vim allows multiple documents or different parts of a single document to be viewed and edited simultaneously through splits:
    • :split or Ctrl-w n: Splits the window horizontally.
    • :sp filename: Opens a file in a new horizontal split.
    • :sp +/searchstring filename: Opens a file and moves the cursor directly to the first instance of searchstring.
    • Navigation between splits is facilitated by Ctrl-w commands like Ctrl-w j (move down), Ctrl-w k (move up), and Ctrl-w Ctrl-w (cycle through splits).

7. Text Manipulation

  • Use U to convert all highlighted text in visual mode to uppercase or u to make it lowercase, simplifying case adjustments across large text blocks.

8. Insert Mode Enhancements

  • Vim supports several commands to enter insert mode, enhancing how and where text is added:
    • i: Insert text before the cursor.
    • a: Append text after the cursor.
    • I: Insert at the first non-blank character of the line, ideal for coding styles that avoid leading whitespace.
    • o/O: Open a new line below (o) or above (O) the current line, useful for adding multiple lines efficiently.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *