vim
text editorsLinux/Unix
The vim command is one of the most frequently used commands in Linux/Unix-like operating systems. vim Vi IMproved, a programmers text editor
Quick Reference
Command Name:
vim
Category:
text editors
Platform:
Linux/Unix
Basic Usage:
vim [options] [arguments]
Common Use Cases
Syntax
vim [options] [file...]
Options
Option | Description |
---|---|
-b |
Edit in binary mode |
-c cmd |
Execute command cmd after loading the first file |
-d |
Start in diff mode (vimdiff) |
-e |
Start in Ex mode (like "ex") |
-f |
Run in foreground (for gvim) |
-h |
Display help message and exit |
-i VIMINFO |
Use VIMINFO file instead of .viminfo |
-L |
Same as -r (recover mode) |
-n |
No swap file, changes only in memory |
-N |
Not compatible with vi (use Vim features) |
-o[N] |
Open N windows horizontally (default: one for each file) |
-O[N] |
Open N windows vertically (default: one for each file) |
-p[N] |
Open N tab pages (default: one for each file) |
-r |
List swap files or recover a crashed session |
-R |
Read-only mode (like "view") |
-s SCRIPTFILE |
Source SCRIPTFILE after loading the first file |
-t TAG |
Edit the file containing TAG |
-u VIMRC |
Use VIMRC file instead of any .vimrc |
-v |
Start in Vi mode (default) |
-x |
Edit encrypted files (will prompt for key) |
--noplugin |
Don't load plugin scripts |
--servername NAME |
Become server NAME |
--remote FILE |
Edit FILE in a Vim server if possible |
+[num] |
Start at line number [num] |
+/pattern |
Start at the first occurrence of pattern |
+cmd |
Execute command cmd after loading the first file |
-- |
End of options, only file names follow |
Common vim Commands
Vim operates in multiple modes. The main ones are:
- Normal mode: For navigation and simple editing (default)
- Insert mode: For inserting text
- Command-line mode: For executing longer commands
- Visual mode: For selecting text
Basic Navigation (Normal Mode)
Command | Description |
---|---|
h | Move cursor left |
j | Move cursor down |
k | Move cursor up |
l | Move cursor right |
w | Move to next word |
b | Move to previous word |
0 | Move to beginning of line |
$ | Move to end of line |
gg | Move to first line of file |
G | Move to last line of file |
{number}G | Move to line {number} |
Ctrl+f | Page down |
Ctrl+b | Page up |
Editing (Insert Mode)
Command | Description |
---|---|
i | Insert before cursor |
I | Insert at beginning of line |
a | Append after cursor |
A | Append at end of line |
o | Open new line below |
O | Open new line above |
Esc | Exit insert mode |
Deleting and Changing Text
Command | Description |
---|---|
x | Delete character under cursor |
X | Delete character before cursor |
dd | Delete current line |
{number}dd | Delete {number} lines |
dw | Delete word |
D | Delete from cursor to end of line |
cw | Change word (delete word and enter insert mode) |
cc | Change line (delete line and enter insert mode) |
C | Change from cursor to end of line |
r | Replace single character |
R | Enter replace mode |
Copy and Paste
Command | Description |
---|---|
yy | Yank (copy) current line |
{number}yy | Yank {number} lines |
yw | Yank word |
p | Paste after cursor |
P | Paste before cursor |
Search and Replace
Command | Description |
---|---|
/pattern | Search forward for pattern |
?pattern | Search backward for pattern |
n | Repeat search in same direction |
N | Repeat search in opposite direction |
:%s/old/new/g | Replace all occurrences of "old" with "new" in file |
:s/old/new/g | Replace all occurrences of "old" with "new" in current line |
:%s/old/new/gc | Replace all occurrences with confirmation |
File Operations
Command | Description |
---|---|
:w | Write (save) file |
:w filename | Save as filename |
:q | Quit |
:q! | Quit without saving |
:wq | Write and quit |
ZZ | Write and quit (shortcut) |
:e filename | Edit another file |
:e! | Reload current file (discard changes) |
:saveas filename | Save as filename and edit that file |
Visual Mode
Command | Description |
---|---|
v | Enter visual mode (character-wise) |
V | Enter visual mode (line-wise) |
Ctrl+v | Enter visual block mode |
gv | Reselect last visual selection |
Multiple Windows and Tabs
Command | Description |
---|---|
:split | Split window horizontally |
:vsplit | Split window vertically |
Ctrl+w h | Move to the window on the left |
Ctrl+w j | Move to the window below |
Ctrl+w k | Move to the window above |
Ctrl+w l | Move to the window on the right |
:tabnew | Create a new tab |
gt | Go to next tab |
gT | Go to previous tab |
{number}gt | Go to tab {number} |
Miscellaneous
Command | Description |
---|---|
u | Undo |
Ctrl+r | Redo |
. | Repeat last command |
:set number | Show line numbers |
:syntax on | Enable syntax highlighting |
:set autoindent | Enable auto-indentation |
:set tabstop=4 | Set tab width to 4 spaces |
:help | Open help |
:help keyword | Get help on keyword |
Vim-Specific Features
Feature | Description |
---|---|
:NERDTree | Open file browser (if plugin installed) |
:terminal | Open terminal window (Vim 8+) |
:PluginInstall | Install plugins (if using Vundle) |
:AirlineTheme theme | Change airline theme (if plugin installed) |
:fzf | Fuzzy file finder (if plugin installed) |
Examples
How to Use These Examples
The examples below show common ways to use the vim
command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.
Basic Examples:
Open a file for editing
vim file.txt
Open multiple files
vim file1.txt file2.txt file3.txt
Open a file at a specific line number
vim +10 file.txt
Open a file and position cursor at the first occurrence of a pattern
vim +/pattern file.txt
Start vim in read-only mode
vim -R file.txt
Open a new file and immediately start in insert mode
vim +i newfile.txt