Updated September 1, 2023
Definition of Vim Command in Linux
Vim is a text editor that is an upgraded version of the Vi editor and is more compatible with Vi. The most usage of vi editors is to create a new file, edit an existing one, or just read one. Vim editor is more helpful in editing different kinds of plain text. This command is more used in editing programs.
Many upgrades are done to the Vi editor, like multiple windows that can be opened simultaneously, multi-level undo options and buffers, syntax highlighting, filename completion, command-line editing, online help, and many more.
How does Vim Command work in Linux?
Operating modes in Vim editor:
- Command Mode: By default, command mode is on when the Vim editor is started. This command mode helps users to copy, paste, delete, or move text. We should press the [Esc] key to go to command mode while in other modes.
- Insert mode: Whenever we try to open the Vim editor, it will go to command mode by default. We must go to insert mode to write the contents in the file. Press ‘I’ to go to insert mode. If we want to return to command mode, press the [Esc] key.
Syntax:
The syntax for the Vim editor is given below:
vim filename
You can check the options that are available for Vim in Linux by giving the ‘help’ command below:
vim –help
Vim Command Usage:
usage: vim [arguments] [file ..] edit specified file(s)
or: vim [arguments] - read text from stdin
or: vim [arguments] -t tag edit file where tag is defined
or: vim [arguments] -q [errorfile] edit file with first error
Options available in Vim Command Linux:
— Only file names after this
-g Run using GUI (like “gvim”)
-f or –nofork Foreground: Don’t fork when starting GUI
-v Vi mode (like “vi”)
-e Ex mode (like “ex”)
-E Improved Ex mode
-s Silent (batch) mode (only for “ex”)
-d Diff mode (like “vimdiff”)
-y Easy mode (like “evim”, modeless)
-R Readonly mode (like “view”)
-Z Restricted mode (like “rvim”)
-m Modifications (writing files) not allowed
-M Modifications in text not allowed
-b Binary mode
-l Lisp mode
-C Compatible with Vi: ‘compatible.’
-N Not fully Vi compatible: ‘nocompatible.’
-V[N][fname] Be verbose [level N] [log messages to fname]
-D Debugging mode
-n No swap file; use memory only
-r List swap files and exit
-r (with file name) Recover crashed session
-L Same as -r
-A start in Arabic mode
-H Start in Hebrew mode
-F Start in Farsi mode
-T <terminal> Set terminal type to <terminal>
-c <command> Execute <command> after loading the first file
-S <session> Source file <session> after loading the first file
-s <scriptin> Read Normal mode commands from file <scriptin>
-w <scriptout> Append all typed commands to file <scriptout>
-W <scriptout> Write all typed commands to file <scriptout>
-x Edit encrypted files
-X Do not connect to X server
–clean ‘nocompatible,’ Vim defaults, no plugins, no viminfo
-h or –help Print Help (this message) and exit
–version Print version information and exit
Examples of Vim Command used in Linux
Step 1: Create a new file: To create a new file, you can use the below syntax:
Syntax:
vim filename.txt
Example:
vim new_file.txt
Command Mode: The below screenshot is taken when the vi editor is in command mode.
Step 2: Go to Insert Mode: Press’ I’ from the keyboard to switch from command mode to insert mode. At the bottom of the editor, you can see INSERT’ written as shown below:
Step 3: Write the content: Once the editor is in insert mode, you can start writing the content in the file.
Step 4: Save the file and exit from the editor: To save and exit from it, you can press the [Esc] key and the ‘:wq.’
Syntax:
[Esc] + :wq
Example:
Step 5: Check whether the data has been created successfully: To view the content in the file, you can use the cat command in unix.
Syntax:
cat filename.txt
Example:
cat new_file.txt
Basic Vim Commands used in Linux
Below are a few commands that are used in Linux, which will help to open or close the document as well as to save the file. Basic Vim commands that are used in the editor are:
- Shift +:e[file] – Opens a [file] that you want to open. Here [file] is the filename that you want to open.
- Esc +:w – Save the file but do not exit.
- Esc +:q! – To quit the file without first saving what you were working on.
- Esc +:wq – To save the file and exit from Vim.
Shortcut commands in Vim Linux
There are a few shortcut commands in the Vim editor to move the cursor from where it last ended. Below are a few of them for easy movement of the cursor:
- h- move the cursor to the left
- l- move the cursor to the right
- k- move the cursor up one line
- H- moves the cursor to the beginning of the screen
- M- moves the cursor to the middle of the screen
- L- moves the cursor to the bottom of the screen
- j- moves the cursor to the down of one line
- b- moves the cursor to the end of a word
- e- moves the cursor to the end of a word
- w- moves the cursor to the start of the next word
- gg- moves the cursor to the beginning of the file
- G- moves the cursor to the end of the file
Conclusion
In Unix Shell Scripting, the VIM editor is one of the most helpful text editors. It is the default editor which is installed in Unix OS. Most usage of vim editor is used to create a new file, edit an existing one, or read a file. This editor is default present in all Linux or Unix OS. Most of the Vim editor is used to create a new file, edit an existing one, or just read one. The editor is user-friendly and flexible to use. So, users prefer using this editor for their needs.
Recommended Articles
We hope that this EDUCBA information on “Vim Command in Linux” was beneficial to you. You can view EDUCBA’s recommended articles for more information.