If you are reading this article then you are in trouble. You wanted to edit a file using vim editor but got stuck in it. Stop panicking and wait with re-installing the OS and starting all from scratch – in this article I will show you how to exit a vim trap.
Exit VIM editor using key combinations
Vim editor is tricky for beginners, but when you learn right key combinations using VIM becomes easy. Here are most popular key combinations to quit vim.
Exit vim saving changes
To exit vim with saving changes press Esc key to exit editing mode, then hold down Shift and press colon key (:). Then enter “wq” and press Enter. Here “w” stands for write and “q” stands for “quit”. On US keyboards the colon : is the same key as semicolon ;, located right of the letter “L” key.
:wq
Exit vim without saving changes
To exit vim without saving changes hold down Shift and press the colon key (:). Then enter “q!” and press Enter.
:q!

Save changes without existing VIM editor
To save changes without exiting vim hold down Shift and press the colon key (:). Then enter “w” and press Enter.
:w
Getting proficient in VIM
Now when I covered the basics of exiting Vim and saving changes lets proceed to some additional useful Vim shortcuts and features to enhance your proficiency.
Starting VIM to edit the file
Usually developers launch vim to edit the file using command:
vim filename
If the file is missing it will be created. You can specify multiple files to edit:
vim file1 file2 file3
To open file on specific line you can add +[num] after the vim command. For example to open file on line 127 enter:
vim filename +127
Navigating in VIM
Once you’ve entered Vim, navigating efficiently is key. Here are some popular shortcuts:
- Moving the Cursor:
- Use the arrow keys to move in the respective directions (up, down, left, right).
- Alternatively, employ the
h
,j
,k
, andl
keys for left, down, up, and right movements, respectively.
- Word Navigation:
- Move the cursor to the beginning of the next word with
w
. - Navigate to the end of the current word using
e
. - Jump to the beginning of the previous word with
b
.
- Move the cursor to the beginning of the next word with
- Navigating Lines:
- Go to the start of the line by pressing
0
. - Move to the end of the line with
$
.
- Go to the start of the line by pressing
Editing Text in VIM
Once you’ve reached the desired location, these shortcuts will aid in text manipulation:
- Entering Insert Mode:
- Press
i
to enter insert mode before the cursor. - Use
I
to start inserting at the beginning of the line. - Employ
a
to append after the cursor. A
allows appending at the end of the line.
- Press
- Deleting Text:
- Delete the character under the cursor with
x
. - Erase the entire line with
dd
. - Cut (delete) the current line and enter insert mode with
cc
.
- Delete the character under the cursor with
- Copy and Paste:
- Copy a line with
yy
. - Paste the copied line below the current line using
p
.
- Copy a line with
- Undo and Redo:
- Undo the last action with
u
. - Redo with
Ctrl + r
.
- Undo the last action with
Searching in VIM
Efficient searching is crucial. Here are some search-related shortcuts:
- Searching for Text:
- Initiate a search with
/
, followed by the desired term. - Use
n
to jump to the next occurrence andN
to go to the previous one.
- Initiate a search with
- Replacing Text:
- Substitute the current word with a new one using
:s/old/new/g
.
- Substitute the current word with a new one using
Exiting Vim
I have already covered the basics in the beginning, but here are a few more options:
- Save and Quit All:
- To save all changes and quit multiple files simultaneously, use
:wa
and:qa
.
- To save all changes and quit multiple files simultaneously, use
- Exiting Without Saving:
- If you want to exit Vim without saving any changes made in any of the files,
:qa!
will do the trick.
- If you want to exit Vim without saving any changes made in any of the files,
Conclusion
Remember, mastering Vim takes time and practice. The more you use it, the more comfortable and powerful it becomes. Happy editing!