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 file, or to just read a file. Vim editor is more useful in editing different kinds of plain text. This command is more used in editing programs.
There are many up gradations done to Vi editor like multiple windows can be opened at a time, multi-level undo option and buffers, syntax highlighting, filename completion, command-line editing, on-line help, and many more.
How does Vim Command work in Linux?
Operating modes in vim editor:
- Command Mode: By default, command mode is on as soon as the vim editor is started. This command mode helps users to copy, paste, delete, or move text. We should be pressing [Esc] key to go to command mode when we are in other modes.
- Insert mode: Whenever we try to open vim editor, it will go to command mode by default. To write the contents in the file, we must go to insert mode. Press ‘I’ to go to insert mode. If we want to go back to command mode, press the [Esc] key.
Syntax for vim editor is given below:
You can check the options that are available for vim in Linux by giving the ‘help’ command as below:
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:
— 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:
Command Mode: The below screenshot is taken when vi editor is in command mode.
Step 2: Go to Insert Mode: To switch from command mode to insert mode, press ‘I’ from the keyboard. 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 the file and exit from it, you can press the [Esc] key and the ‘:wq’.
[Esc] + :wq
Step 5: Check the data has been created successfully or not: To view the content in the file, you can use the cat command in unix.
Basic Vim Commands used in Linux
Below are a few commands that is 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 from the file without first saving that you were working on.
- Esc + :wq – To save the file and exit from vim.
Shortcut commands in Vim
There are few shortcut commands in vim editor to move the cursor from where it was last ended. Below are few of them for easy movement of cursor:
- h- move the cursor to the left
- l- move the cursor to the right
- k- move the cursor to 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 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 start of the file
- G- moves the cursor to the end of the file
In Unix Shell Scripting, VIM editor is one of the most used and very useful text editor. It is the default editor which is installed in Unix OS. The most usage of vim editor is to create a new file, edit an existing file or to just read a file. This editor is default present in all Linux or Unix OS. The most usage of vim editor is to create a new file, edit an existing file or just read a file. The editor is user-friendly and most flexible to use. So, users prefer using this editor for their needs.
This is a guide to Vim Command in Linux. Here we also discuss the definition and how does Vim Command in Linux work? along with different examples and its code implementation and Basic Vim Commands used in Linux. You may also have a look at the following articles to learn more –