How To Use Vim Editor
Beginner’s guide to Vim text editor
What is Vim?
Vim is a modal text editor used by programmers in programs such as Git. Originated from the Vi editor from 1976, this text editor is still used and updated today. Vim helps with reading and editing files, and that is why it is called a modal editor. It has different modes for things like inserting text or modifying it.
Two modes: command and insert
In command mode, you can perform things like undo, redo, quit, or find and replace text. In insert mode, you enter text in the text editor. There is actually a third mode in Vim, called visual mode, which highlights and edits a lot of text in large amounts.
Opening Vim
If you have Git installed, you can open Vim right from there. In Git, enter the command below to open Vim:
$ vim filename
When you open Vim, a single window opens. You have entered command mode.
Enter Insert Mode
If you want to start typing in the window, you can go into INSERT mode. When you type “i” while in command mode, you will be switched to insert mode.
You might not even see what happened when typing “i”, but at the bottom of the Vim window, it says “INSERT”. This means you are now in insert mode, and you can start typing text in the window.
Exit Insert Mode
When you want to get out of insert mode and go back to command mode, use “Esc” (escape key) on your keyboard.
Closing Vim
When you want to close Vim, it is best to save your changes you made in the file (or, if you want, not save the changes at all!). There are a few different commands you can use:
:q
Quits Vim if you do not have unsaved changes (and a warning will pop up if you use this with unsaved changes).
:q!
Quits Vim if you do have unsaved changes.
:wq
Saves your changes made to the file and quits Vim at the same time.
Extra Vim commands to know (in command mode)
h — moves text cursor left
j — moves text cursor down
k — moves text cursor up
l — moves text cursor right
You can move through text in a file using h, j, k, and l keys
dd or :d
Deletes the current line the cursor is on
u
Undo the last action
yy or :y or Y
Copies the current line where the cursor is.
p — paste the text you copied or deleted on line below cursor
P — paste the text you copied above the current line
You can practice more Vim commands at the following website: https://sites.radford.edu/~mhtay/CPSC120/VIM_Editor_Commands.htm
Sources
MIT. (n.d.). Editors(Vim). https://missing.csail.mit.edu/2020/editors/
Radford University. (n.d.). VIM Editor Commands. https://sites.radford.edu/~mhtay/CPSC120/VIM_Editor_Commands.htm
Stanford University. (n.d). The Vim Editor. https://web.stanford.edu/class/cs107/resources/vim