How to Vim Like a Pro

Vim is one of the most powerful text editors out there, but it comes with a steep learning curve. If you're looking to boost your productivity and efficiency when coding or writing in Vim, mastering its commands is essential. This guide will cover key commands and concepts that will help you Vim like a pro.

1. Basic Navigation

Vim operates in different modes:

  • Normal mode (default for navigation and commands)
  • Insert mode (for text input)
  • Visual mode (for selecting text)

Switching Modes:

  • i: Switch to Insert mode before the cursor.
  • I: Switch to Insert mode at the beginning of the line.
  • a: Insert after the cursor.
  • A: Insert at the end of the line.
  • v: Switch to Visual mode to start selecting text.
  • V: Switch to Visual Line mode, selecting entire lines.

Moving Around:

  • h, j, k, l: Move left, down, up, and right respectively.
  • w: Jump to the start of the next word.
  • b: Jump to the beginning of the previous word.
  • e: Jump to the end of the current word.
  • 0: Jump to the beginning of the line.
  • $: Jump to the end of the line.
  • gg: Jump to the top of the file.
  • G: Jump to the bottom of the file.

Example:

Let's say you're at the top of a file, and you want to jump to the end of the line and start editing:

  1. Use $ to move to the end of the current line.
  2. Press a to start adding text after the last character.

2. Editing Text

Editing in Vim can be quick once you understand some core commands.

Deleting Text:

  • x: Delete the character under the cursor.
  • dw: Delete the word from the cursor.
  • dd: Delete the current line.
  • d$: Delete from the cursor to the end of the line.

Copy and Paste:

  • yy: Copy (yank) the current line.
  • y$: Yank from the cursor to the end of the line.
  • p: Paste after the cursor.
  • P: Paste before the cursor.

Changing Text:

  • cw: Change the current word (delete the word and enter Insert mode).
  • C: Change to the end of the line from the cursor.
  • cc: Change the entire current line.

Example:

You can combine commands to edit text efficiently. Suppose you want to change a word in the middle of a line:

  1. Navigate to the word using w or b.
  2. Use cw to delete the word and switch to Insert mode.
  3. Type the replacement word and press Esc to return to Normal mode.

3. Working with Multiple Files and Buffers

Vim allows you to open and edit multiple files using buffers and tabs.

Open Files:

  • :e filename: Open a file in the current buffer.
  • :vsp filename: Open a file in a vertical split window.
  • :tabnew filename: Open a file in a new tab.
  • :ls: List all open buffers.
  • :bnext: Move to the next buffer.
  • :bprev: Move to the previous buffer.

Closing Buffers and Tabs:

  • :bd: Close the current buffer.
  • :q: Close the current window.
  • :q!: Quit without saving changes.
  • :wq: Save and quit.

Example:

To open two files side-by-side:

  1. Type :vsp file2 to open the second file in a vertical split.
  2. Navigate between them using Ctrl-w followed by an arrow key.

4. Search and Replace

Efficient search and replace is key when handling large files.

Searching:

  • /pattern: Search forward for a pattern.
  • ?pattern: Search backward for a pattern.
  • n: Go to the next occurrence.
  • N: Go to the previous occurrence.

Replacing:

  • :s/old/new: Replace the first occurrence of "old" with "new" on the current line.
  • :s/old/new/g: Replace all occurrences of "old" with "new" on the current line.
  • :%s/old/new/g: Replace all occurrences of "old" with "new" in the entire file.
  • :%s/old/new/gc: Replace all occurrences with confirmation.

Example:

Suppose you want to replace every occurrence of "foo" with "bar" throughout your file:

  1. Type :%s/foo/bar/gc.
  2. Vim will prompt you for each replacement, allowing you to confirm or skip.

5. Undo and Redo

Don't worry if you make mistakes! Vim's undo/redo system has you covered.

  • u: Undo the last change.
  • Ctrl-r: Redo the last undone change.

Example:

You deleted a paragraph by mistake? No problem! Just press u to undo the change and recover it.

6. Visual Mode for Selections

Visual mode is great for selecting and manipulating text.

Selection Types:

  • v: Start character-wise selection.
  • V: Start line-wise selection.
  • Ctrl-v: Start block-wise selection.

Commands with Visual Mode:

  • y: Yank the selected text.
  • d: Delete the selected text.
  • >, <: Indent and unindent the selected text.

Example:

To indent multiple lines, you can:

  1. Press V to select the first line.
  2. Use j to extend the selection downward.
  3. Press > to indent the selected lines.

7. Macros: Automate Repetitive Tasks

Macros allow you to record and repeat sequences of commands.

Recording Macros:

  1. Press q followed by any letter (e.g., q a) to start recording a macro.
  2. Perform any series of commands.
  3. Press q again to stop recording.

Playing Macros:

  • @a: Play the macro stored in register a.
  • @@: Replay the last macro.

Example:

If you need to delete a word at the start of each line:

  1. Start by pressing q a to record the macro in register a.
  2. Move to the start of the line and press dw to delete the first word.
  3. Press j to move to the next line.
  4. Press q to stop recording.
  5. Run @a on the next line, and repeat as needed by pressing @@.

8. Configuring Vim with .vimrc

To truly Vim like a pro, customize your Vim experience by configuring .vimrc. Here are a few handy settings:

  • set number: Display line numbers.
  • set relativenumber: Display relative line numbers.
  • set tabstop=4: Set the width of a tab character.
  • set shiftwidth=4: Set the number of spaces used for indentation.
  • set expandtab: Convert tabs to spaces.

Example:

If you add these lines to your .vimrc, you'll get line numbers, a 4-space tab width, and automatic spaces instead of tabs, making your code more readable.

`set number
set tabstop=4
set shiftwidth=4
set expandtab`

Conclusion

Vim may seem daunting at first, but once you get comfortable with its commands, it becomes a productivity powerhouse. Start with basic navigation and editing, and then move on to advanced features like macros and search/replace. Soon, you'll be Vimming like a pro, breezing through files and writing code faster than ever!

Continue Learning

Discover more articles on similar topics