Working with vi/vim editor
vim is a modified version of old unix vi editor which is a command line based text editor. It can be used to edit all kinds of plain text. It is especially useful for editing programs.
vim’s enhancement above vi
- Multi level undo
- Multi windows and buffers
- Syntax highlighting
- Command line editing
- Visual selection
How to install vim?
$sudo apt-get install vim
Installation from source
- Download vim source from vim.org
- tar xvzf vim-7.0.tar.gz
- cd vim70
- ./configure –prefix=/opt –enable-cscope
- make install
Within working directory
$vim/vi [option] file_name
Out of working directory
$vim/vi [option] path/to/destination/file_name
vim will start with default empty buffer when file_name is not given by user. Some vim options are being listed here:
-d option is used to compare two or more then two files. In this you need to use more then one file names as follows
$vim -d file_name1 file_name2 file_name3…….
- -h or –help
This option is used to ask a help from system about vim/vi
This option is used for read only mode.
$vim -R file_name
- -r (without file name)
This option will list all of swap files present in directory
- -r (with file name)
To recover a file from swap file
$vim -r file_name
This option is used to access a binary file
$vim -b file_name
To edit a encrypted file
$vim -x file_name
How to undo and redo in vi/vim editor
undo can be done by using below mentioned shortcut
- press ESC
- after it you need to press u only
And redo can be done by pressing
- Ctrl + r
How to open a file using vim ?
In above section I have mentioned about the syntax ( $ vim file_name ) of vim and vi editor to open a blank file. Now here I am going to discuss about different operations which can be made in a vim/vi file.
These operations are as follow:
As we know that vi/vim opens file in read only mode. Therefor to edit this file you will have to go in edit/insert mode which can happen by pressing “i” from your keyboard.
To jump on n number line from top of you file (suppose you m number lines is at top of your terminal/ file then you will come to m+n number linein your file)
To use it – ESC + :n ( n is a positive integer )
This operation will recover a file after an unexpected interruption.
To use it – ESC + :recover
- :w file_name_new
This operation is used to copy the content into a new file ( here file name is file_name_new )
To use it ESC + :w file_name_new
This operation will delete character on which cursor is positioned.
This option will save your written content in file.
How to use it – ESC + :w
How to come out form vim/vi file?
You can come out from a file by using following options:
Used to quit a file without saving your contents.
How to use it – ESC + :q
forces the exit when you want to quit a file containing unsaved changes.
This option is used to save and exit purpose
This option is used to overrides read-only permission (Need to be a root user)