Vim is a powerful and efficient text editor built into most Unix-like systems, and learning how to use Vim to edit a file helps you work quickly at the command line. This guide walks through opening, navigating, editing, and saving files with precise, repeatable steps.
Instead of switching to a graphical editor, you can rely on Vim for fast in-place changes, scripting workflows, and remote server maintenance directly from the terminal.
Open and Navigate a File in Vim
Before you edit, you need to launch Vim and move the cursor to the right location.
| Action | Command | Result | Notes |
|---|---|---|---|
| Open a file | vim filename |
Starts Vim and loads the file | Creates a new file if it does not exist |
| Enter insert mode | i |
Text can be typed at cursor | Most common editing state |
| Move cursor left | Character left | No need for arrow keys | |
| Move cursor down | Line down | Works without shifting | |
| Move cursor up | k |
Line up | Fast vertical navigation |
| Move cursor right | l |
Character right | Next to h on home row |
| Jump to start of line | 0 |
Cursor to line beginning | Works in normal mode |
| Jump to end of line | $ |
Cursor to line end | Efficient line traversal |
| Search within file | Highlights next match | Press n to jump forward |
Entering and Exiting Modes Correctly
Vim relies on different modes to separate editing from commands.
Normal Mode
Start in normal mode after launching Vim; here you run commands for movement and editing.
Insert Mode
Press i to switch to insert mode, where typed characters appear in the file.
Command-Line Mode
Type : in normal mode to enter command-line mode for saving, quitting, and advanced operations.
Editing Text and Making Changes
Once in insert mode, you can add, remove, and rewrite content just like in a standard editor.
- Type text to insert characters at the cursor.
- Press
Backspaceto delete leftward in insert mode. - Use
xin normal mode to delete characters under the cursor. - Combine numbers with commands, such as
3dw, to delete multiple words efficiently.
To undo the last change, press u in normal mode; press Ctrl-r to redo.
Saving and Writing Your Edits
After making changes, you must explicitly write the buffer to disk to keep your edits.
| Command | Description | When to Use |
|---|---|---|
:w |
Save changes to file | Keep edits without exiting |
:w newfile |
Save to a new filename | Create a copy or rename |
:wq |
Save and quit Vim | Finish editing and return to shell |
:q! |
Discard changes and quit | Exit without saving |
For files that require elevated permissions, you can use :w sudo tee % to write changes without re-opening Vim with sudo.
Advanced Navigation and Large Files
When working with large configuration files or logs, precise navigation reduces friction.
- Use
ggto jump to the top of the file quickly. - Use
Gto jump straight to the bottom of the file. - Press
%to jump between matching parentheses or brackets. - Open line numbers with
:set numberto reference specific lines in scripts or edits.
Essential Vim File Editing Practices
- Always open files with
vim filenameto start controlled edits. - Use
ito enter insert mode and<Esc>to return to normal mode. - Navigate with
h,j,k,land jump with0,$,gg,G. - Edit text in insert mode, and delete or modify content in normal mode.
- Save with
:w, write and quit with:wq, and discard changes with:q!.
FAQ
Reader questions
How do I open an existing file and immediately jump to a specific line number?
Use vim +linenumber filename to open the file with the cursor positioned at the given line number.
What is the fastest way to replace all occurrences of a word in the current file?
In normal mode, run :%s/oldword/newword/g to substitute all instances across the entire file, or :s/oldword/newword/g to replace only on the current line.
How can I edit a file without accidentally introducing extra line endings or trailing whitespace?
Enable :set list to visualize whitespace characters and use :set nowrap to avoid automatic line wrapping while reviewing changes.
If I accidentally quit without saving, how can I recover unsaved changes?
Recover with vim -r filename if Vim left a swap file, or restore from system-level backups or your editor’s persistent undo file if configured.