Vim Editor in Linux
In this topic, you will learn how to work with Vim editor in Linux.
Vim has below features.
- Vim allows you to create,edit text files.
- Vim is advanced version of Vi editor.
- We can also compare files using vimdiff.
- Vim supports syntax highlighting for C, C++, Python etc
To start the Vim from shell, just type “vim” command. You can also give the name of the file.
If you want to open file in read only mode, you can use below command.
vim -R abc.txt
There are 2 modes in vim.
- Command mode
- Insert mode
By default, you are in command mode. To go to insert mode, press i. Once you are in insert mode, you can edit the text file very easily. You can use backspace and delete keys just like how you use it in notepad in windows.
To go back to command mode, press escape.
In command mode, you can use below commands to perform various actions.
- :w – This command will save your file. :w command will save the current file. If you want to save the current file in another file say f2, you can use command “:w f2”. Note that f2 file will be created but vim will continue to hold original file in buffer. To work on new file, you will have to use command “:sav f2” instead of “:w f2”.
- :q – This command will exit the editor. ALternatively you can use :quit.
- :wq – This command can be used to save and exit at the same time.
- :q! – This command can be used to exit without saving the file.
- v – command can be used to copy the selected text.
- V – command can be used to copy entire line
- d – command can be used to cut the text or entire line
- y – command can be used to copy the text or line
- p – command can be used to paste the text at cursor location
Errors in Vim
You may encounter below errors when working with vim. But you can easily avoid these errors by following basic instructions.
- Press enter or type command to continue – This error comes when you don’t use the proper command. For example if you use :!q command instead of :q!, you will get this kind of error.
- readonly option is set ( add! to override) – This error comes when you try to save the changes to read only file. For example If you have read only permission on a file and you change that file in vim, then you will not be allowed to write to that file. Note that vim allows you editing that file but does not allow you saving that file. You can make any file read-only by executing “chmod 0444 myfile” command.
- No write since last change – This error comes when you try to quit without saving changes using :q command.
- Not an editor command :W – This error comes when you are using capital W instead of small cap w.