Skip to main content

VIM Basics

VIM (Vi IMproved) is a powerful text editor commonly used for programming and system administration. This guide covers essential VIM commands and concepts to help you get started efficiently.


📌 Getting Started with VIM

VIM operates in multiple modes:

Mode Overview
  • Normal Mode: Used for navigation and text manipulation (default mode).
  • Insert Mode: Used for typing and inserting text (i or a to enter).
  • Visual Mode: Used for selecting text (v for character selection, V for line selection).
  • Command Mode: Used for executing commands (: to enter command mode).

To start VIM, use:

vim filename

If the file does not exist, VIM creates a new file.


🔄 Switching Between Modes

Quick Mode Switching
  • Normal Mode → Insert Mode: Press i (insert before cursor) or a (append after cursor).
  • Insert Mode → Normal Mode: Press Esc.
  • Normal Mode → Visual Mode: Press v (character selection) or V (line selection).
  • Normal Mode → Command Mode: Press :.

📜 Basic Navigation

Move the cursor without using arrow keys:

CommandAction
hMove left
lMove right
jMove down
kMove up
ggJump to the beginning of the file
GJump to the end of the file
0Move to the start of a line
^Move to the first non-blank character
$Move to the end of a line

✏️ Editing Text

Inserting Text

Insert Commands
  • i → Insert before cursor
  • I → Insert at the beginning of the line
  • a → Append after cursor
  • A → Append at the end of the line
  • o → Open a new line below
  • O → Open a new line above

Deleting Text

CommandAction
xDelete character under cursor
XDelete character before cursor
ddDelete the current line
DDelete from cursor to end of line
d$Delete from cursor to end of line
d0Delete from cursor to start of line

Copy and Paste

CommandAction
yyCopy (yank) the current line
y$Copy from cursor to end of line
y0Copy from cursor to start of line
pPaste after the cursor
PPaste before the cursor

🔍 Searching and Replacing

Search for Text

Use / to search forward and ? to search backward:

/SearchTerm    " Searches forward
?SearchTerm " Searches backward

Press n to go to the next occurrence and N to go to the previous occurrence.

Find and Replace

:%s/old/new/g  " Replace all occurrences of 'old' with 'new'
  • % → Entire file
  • g → Global (replace all occurrences in a line)
  • c → Confirm before replacing

🗂️ Working with Multiple Files

Open Multiple Files

vim file1 file2
  • Switch between files: :n (next) or :prev (previous)
  • Open file explorer: :e .

Split Windows

CommandAction
:split filenameHorizontal split
:vsplit filenameVertical split
Ctrl + wSwitch between splits

💾 Saving and Exiting

CommandAction
:wSave changes
:wq or ZZSave and exit
:q!Quit without saving
:xSave and close (only if changes were made)

🔧 Customization and Configuration

VIM can be customized using a .vimrc file. Example .vimrc:

set number        " Show line numbers
syntax on " Enable syntax highlighting
set autoindent " Enable auto-indentation
set tabstop=4 " Set tab width to 4 spaces

🚀 Advanced Tips

Undo and Redo

CommandAction
uUndo last action
Ctrl + rRedo last undone action

Macros (Recording & Replaying Commands)

CommandAction
q<letter>Start recording macro in register <letter>
qStop recording
@<letter>Run the recorded macro

🎯 Conclusion

Mastering VIM requires practice, but once you get familiar with the commands and shortcuts, it becomes an incredibly powerful tool for efficient text editing.

For more details, check out the VIM documentation:

:help