Vim Quick References
Posted on September 07, 2022 in DevOps
This article is a quick getting started guide to Vim editor commands. Vim editor is an improved version of classic Vi
editor, hence named 'Vi Improved' or Vim
in short.
Note: I'm using macOS Terminal to run vim editor.
Installing Vim Editor (macOS)
You can download Vim editor on macOS using Homebrew package manager.
brew install vim
Verify the installation of Vim editor by checking its version as below:
vim --version
The above command will print the current version of Vim editor as below:
% vim --version
VIM - Vi IMproved 8.2 (2019 Dec 12, compiled Nov 13 2021 05:05:08)
macOS version - arm64
Opening Vim editor
You can start opening the editor by using vim
followed by the file name as below:
vim test_file
The above command will open the test_file
in vim editor. Press i
to change to insert mode to start writing into file.
Modes of Vim
There are mainly three modes in Vim editor:
-
Normal mode: This is the mode
Vim
editor opens in. If you try to type in this mode, you would not be able to. That may leave you in bit confusion on how to edit this file. You would need to change intoInsert
mode discussed next. -
Insert Mode: In order to write/edit the file, you will need to press
i
. This enables the editing mode for Vim. You will need to hitescape
key to return back to Normal mode. -
Line Mode: This mode is activated by hitting
escape
key when in Insert mode followed by colon:
. This is used to perform operation on the file like saving and closing the file or just discarding the edits.:w!
: This command is used to save the contents without closing the file.:wq!
: This command is for saving + closing the file.:q!
: This command is used for quiting out of file without saving any changes. Be careful using this command, since it will discard any changes to file that you might have made.
Note: You will need to hit enter to execute the above commands.
Basic File operations
Creating file: You can create a file using one of the option below:
Option 1#
vim test.txt
Option 2#
vi test.txt
Option 3#
touch test.txt
Opening a file: In vim
you can open a file using vim
followed by the file name.