DevOps Blog

A Beginner's Guide to Vim: The Powerful Text Editor

What is Vim?

Vim (Vi Improved) is an enhanced version of the classic Vi editor. It’s:
Fast – Works entirely in the terminal.
Modal – Uses different modes for different tasks.
Extensible – Highly customizable with plugins.


Installing Vim

Most Linux/macOS systems come with Vim preinstalled. To check:

vim --version

If not installed:

  • Linux (Debian/Ubuntu):
    sudo apt install vim
  • macOS:
    brew install vim
  • Windows: Download from vim.org.

Opening and Closing Vim

  • Open a file:
    vim filename.txt
  • Exit Vim:
    • Press Esc (to ensure you're in Normal mode).
    • Type :q and press Enter (quit).
    • If unsaved, use :q! to force quit.

Vim Modes

Vim has multiple modes, but the most important are:

  1. Normal Mode (Default) – Navigate and execute commands.
  2. Insert Mode – Edit text (like a regular editor).
  3. Command Mode – Run commands (save, quit, etc.).

Switching Between Modes

  • Normal → Insert: Press i (insert) or a (append).
  • Insert → Normal: Press Esc.
  • Normal → Command: Press :.

Basic Navigation (Normal Mode)

Use these keys to move around:

  • h ←, j ↓, k ↑, l
  • w – Move to next word.
  • b – Move to previous word.
  • 0 – Start of line.
  • $ – End of line.
  • gg – Top of file.
  • G – Bottom of file.

Editing Text

Inserting Text

  • i – Insert before cursor.
  • a – Append after cursor.
  • o – New line below.
  • O – New line above.

Deleting Text

  • x – Delete character under cursor.
  • dw – Delete word.
  • dd – Delete entire line.

Undo & Redo

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

Saving Files

From Normal mode:

  • :w – Save.
  • :wq or :x – Save and quit.
  • :q! – Quit without saving.

Searching in Vim

  • /search_term – Search forward.
  • ?search_term – Search backward.
  • n – Next match.
  • N – Previous match.

Copy & Paste (Yanking & Putting)

  • yy – Copy (yank) a line.
  • p – Paste (put) after cursor.
  • P – Paste before cursor.

Bonus Tips

  • Line numbers: Toggle with :set number / :set nonumber.
  • Syntax highlighting: :syntax on.
  • Split windows: :split (horizontal), :vsplit (vertical).

Conclusion

Vim might feel intimidating at first, but with practice, it becomes second nature. Start with these basics, and gradually explore more advanced features like macros, plugins, and custom configurations.

Pro Tip: Run vimtutor in your terminal for an interactive guide!

Happy editing!


Would you like me to add anything specific, like advanced commands or customization tips? Let me know!