How to Save a File in Vim on Linux: Simple Steps
To save a file in
vim on Linux, press Esc to enter normal mode, then type :w and press Enter. This writes (saves) the changes to the file without exiting Vim.Syntax
In Vim, saving a file uses the :w command. Here is what it means:
:- Enters command-line mode where you can type commands.w- Stands for 'write', which saves the file.Enter- Executes the command.
You must be in normal mode (press Esc) before typing :.
vim
:w
Example
This example shows how to save a file named example.txt after editing it in Vim.
bash
vim example.txt # After editing, press Esc :w # Press Enter to save the file
Output
"example.txt" 10L, 200C written
Common Pitfalls
Common mistakes when saving in Vim include:
- Not pressing
Escfirst, so commands don’t work. - Typing
wwithout the colon:, which Vim treats as text input. - Trying to save a file without write permission, causing an error.
Always press Esc to enter normal mode, then type : followed by w and Enter.
vim
:w # Correct way to save
w # Wrong: inserts 'w' in the fileQuick Reference
| Command | Description |
|---|---|
| :w | Save the current file without exiting Vim |
| :wq | Save the file and quit Vim |
| :q! | Quit Vim without saving changes |
| :x | Save and exit Vim (like :wq) |
Key Takeaways
Press Esc to enter normal mode before typing commands in Vim.
Use :w and press Enter to save your file without exiting.
Use :wq to save and quit Vim in one step.
If you want to quit without saving, use :q!.
Always check file permissions if saving fails.