Commonly Used and Useful Vim Commands (Search / Undo / Large Block Deletion)

Commonly Used and Useful Vim Commands (Search / Undo / Large Block Deletion)

Commonly Used and Useful Vim Commands (Search / Undo / Large Block Deletion)

No bloated manuals — just practical examples. Learn how to use Vim like a modern editor, from inserting text to searching and undo/redo.

Vi and Vim are highly popular command-line text editors known for their powerful features. However, their extensive functionality can also make them challenging to master — especially for users accustomed to mouse‑driven graphical editors. This guide skips the overwhelming parameter manuals and uses practical examples to show you how to perform common editing tasks in Vim.

Insert Mode / Editing

When you open a file with Vim, you start in command mode (normal mode). You cannot simply type text right away. To begin editing, press the i key to enter insert mode. Once in insert mode, you can edit the text directly as you would in any other editor.

Quick tip: Press ESC at any time to return to command mode.

Quick Page Navigation

Use the Pg Up and Pg Dn keys (located above the arrow keys on most keyboards) to quickly scroll up and down by page.

Quickly Jump to Beginning or End of a Line

Press the Home key to jump to the beginning of the current line, or the End key to jump to the end of the current line.

Exit Vim

  • If you haven't made any changes and you are in command mode, type :q and press Enter to quit.
  • If you are in insert mode, press ESC to return to command mode first, then type :q.
  • If you have made changes but don't want to save them, press ESC to enter command mode, then type :q! and press Enter to force quit without saving.

Save and Exit

Return to command mode (press ESC if needed), then type :wq and press Enter to save your changes and exit. If you see a permission error (e.g., "readonly" or "permission denied"), first force quit without saving using :q!, then reopen the file with sudo vim filename and save normally.

Note: When running Vim with sudo, you can edit and save system files that require root privileges.

Insert a New Line

  • In command mode, press o (lowercase) to insert a new line below the current cursor position. Vim will automatically enter insert mode.
  • Press O (uppercase, i.e., Shift + O) to insert a new line above the current cursor position and enter insert mode.

Delete / Cut a Line

In command mode, press dd (press the d key twice quickly). This deletes (cuts) the entire current line. All content below will move up one line. The deleted content is stored in Vim's clipboard and can be pasted later.

Paste

In command mode, press p to paste the most recently deleted or yanked (copied) content at the current cursor position. Alternatively, you can paste directly from your system clipboard using the standard shortcut Ctrl + Shift + V (in many terminal environments) — this will insert at the cursor position regardless of Vim modes.

Delete Part of a Line

Suppose you have a long line and you want to keep only the text up to a certain point, deleting everything after it. Move the cursor to the last character you want to keep, then press ESC to ensure you're in command mode, and press d$ (the $ symbol represents the end of the line). This deletes everything from the cursor position to the end of the line.

Example: If you have the line Hello world, this is a long sentence. and your cursor is on the comma after "world", pressing d$ will delete " this is a long sentence.", leaving Hello world.

Undo / Redo

  • To undo the last action, press u in command mode. You can press it multiple times to undo several steps.
  • If you undo too many steps, press Ctrl + r in command mode to redo (reverse the undo).

Search

Vim provides powerful search functionality:

  • Forward search (toward the end of the file): In command mode, press / (forward slash). The cursor moves to the bottom of the screen. Type the text you want to search for and press Enter. Vim will highlight the first match. Press n to go to the next match in the same direction.
  • Backward search (toward the beginning of the file): In command mode, press ? (question mark). Type your search term and press Enter. Again, press n to continue searching in the same direction (now backward).
Pro tip: After a search, all matching terms remain highlighted until you start a new search or run :noh (no highlight) to clear the highlighting.

Quick Reference

Action Command
Enter insert mode i
Exit without saving :q!
Save and exit :wq
Insert new line below o
Insert new line above O
Delete current line dd
Paste p
Delete from cursor to end of line d$
Undo u
Redo Ctrl + r
Forward search /text
Backward search ?text
Next search match n


Previous post

Комментировать