Vim Editor in Linux

In this topic, you will learn how to work with Vim editor in Linux.

Vim has below features.

  1. Vim allows you to create,edit text files.
  2. Vim is advanced version of Vi editor.
  3. We can also compare files using vimdiff.
  4. 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.

vim xyz

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.

  1. Command mode
  2. 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.

  1. :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”.
  2. :q – This command will exit the editor. ALternatively you can use :quit.
  3. :wq – This command can be used to save and exit at the same time.
  4. :q! – This command can be used to exit without saving the file.
  5. v – command can be used to copy the selected text.
  6. V – command can be used to copy entire line
  7. d – command can be used to cut the text or entire line
  8. y – command can be used to copy the text or line
  9. 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.

  1. 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.
  2. 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.
  3. No write since last change – This error comes when you try to quit without saving changes using :q command.
  4. Not an editor command :W – This error comes when you are using capital W instead of small cap w.

You may also like...