Vim is a highly efficient and customizable text editor, but its steep learning curve can be intimidating for beginners. This guide provides a complete list of essential Vim commands, from basic navigation to advanced editing, to help you master Vim quickly.


Getting Started with Vim

Opening and Closing Files

  • Open a File:
    1
    vim filename  
  • Save and Quit:
    • :w: Save changes
    • :q: Quit (if no changes)
    • :wq: Save and quit
    • :q!: Quit without saving

Basic Navigation

Cursor Movement

  • Arrow Keys: Use h, j, k, l for left, down, up, and right.
  • Word Navigation:
    • w: Move to the start of the next word.
    • b: Move to the start of the previous word.
    • e: Move to the end of the current word.
  • Line Navigation:
    • 0: Move to the start of the line.
    • $: Move to the end of the line.
    • gg: Go to the first line of the file.
    • G: Go to the last line of the file.
    • :[line number]: Jump to a specific line (e.g., :10).

Screen Navigation

  • Ctrl + f: Scroll down one page.
  • Ctrl + b: Scroll up one page.
  • Ctrl + d: Scroll down half a page.
  • Ctrl + u: Scroll up half a page.

Editing Commands

Insert Mode

  • i: Insert before the cursor.
  • a: Insert after the cursor.
  • o: Insert a new line below the current line.
  • O: Insert a new line above the current line.

Deleting Text

  • x: Delete the character under the cursor.
  • dw: Delete from the cursor to the end of the word.
  • dd: Delete the entire line.
  • D: Delete from the cursor to the end of the line.

Copy, Cut, and Paste

  • yy: Copy (yank) the current line.
  • yw: Copy the current word.
  • p: Paste after the cursor.
  • P: Paste before the cursor.

Search and Replace

Searching

  • /keyword: Search forward for a keyword.
  • ?keyword: Search backward for a keyword.
  • n: Move to the next match.
  • N: Move to the previous match.

Replacing

  • :%s/old/new/g: Replace all occurrences of old with new.
  • :%s/old/new/gc: Replace with confirmation for each match.

Advanced Commands

Undo and Redo

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

Working with Multiple Files

  • :e filename: Open another file in the same Vim session.
  • :bn: Switch to the next buffer.
  • :bp: Switch to the previous buffer.
  • :bd: Close the current buffer.

Split Screen

  • :split: Split the window horizontally.
  • :vsplit: Split the window vertically.
  • Ctrl + w + [arrow key]: Switch between split windows.

Customizing Vim

Configuration

  • Edit ~/.vimrc to customize Vim settings.
  • Example: Add set number to display line numbers.

Plugins

  • Use a plugin manager like Vim-Plug to extend Vim’s functionality.

Conclusion

Vim is a powerful tool for text editing, and mastering its commands can significantly improve your workflow. Start with the basics, practice regularly, and explore advanced features to unlock its full potential.

For more resources, visit the official Vim documentation.

How to Save and Exit Nano Editor A Quick Guide | flyday