VIM provides varios modes for editing the files.
1. normal mode
As soon as you start vim, it will be in “normal” mode by default. In this mode we can view/search/replace/copy/paste the opened file.
To move to any other mode there are following command
- Go to Insert mode – Esc, I
- Go to Replace mode – Esc, R
- Go to Visual mode – Esc, V
2. insert mode
One we are in insert mode, we can do all the thing related to edition of the file.
3. replce mode
Replace mode is similar to insert/replace key of any other editor. If you are in replace mode, whatever u type, that will be overwritten on the existing data.
4. visual mode
Visual mode is used for copy/paste operation.
Commands in VIM
- search – Esc, /<search_string> (press “n” to search next, like F3 in windows editors)
- search back – Esc, ?<search_string> (press “n” to search previous, like Shift+F3 in windows editors)
- replace – Esc, :%s/<search_string>/<replace_string> (It will replace all the searched string in a single shot… i dont know how to replace it one by one :( )
- run any linux command from vim – Esc, !<cmd_name>
- save file – Esc, :w (w means write)
- quit from file – Esc, :q (q means quit)
- save n quit – Esc, :x OR Esc, :wq
- quit without saving a file – Esc, :q!
- forcefully write to a file – Esc, :w!
- goto starting of the file – Esc, gg
- goto end of the file – Esc, -Shift+g
- goto particular line no – Esc, :<line_no>
- open a file in vi (from command line) with cursor on some particular line no. – vi <file_name> +<line_no>
- open a file within vi – Esc, :split <abs_path_of_file> (this will split the current window and open the file)
- to move on other file – Esc, Ctrl+w, <up_arrow>/<down_arrow> (as per your file position in your current window)
- open a file without splitting – Esc, :o <file_name>
- delete a line – Esc, dd
- copy a line (in normal mode) – Esc, yy (y means yank)
- paste a copied line from the same file – Esc, p
- copy a selected portion (for this, we have to be in visual mode) – Esc, :v (to go to visual mode) now you use the arrow keys to select the area and press “y“
- paste the selected area – Esc, i (go to insert mode), move the cursor at desired position and press “p“, it will paste the copied content into next line
- find name of the currently opened file and curser postion (if ruler is not set to ON) – Esc, Ctrl+g
- show the matching braces (it help in programming, shows the matching braces for loops, if/else cases) – Esc, :show match
- find the corresponding braces in a block – Esc, Shift+5
- jump to the definion of a function/structure/macro (this requires the “tags” variable set in ur VIM environment) – Esc, Ctrl+[
- go back from the defination – Esc, Ctrl+o
- set the tags variable inside vim – Esc, :set tags=<abs_path_of_tags_file>
- set the fonts color to bright/dark if you background is dark/light – Esc, :set background=<dark/light>
- set text wrapping off – Esc, :set nowrap
- use mouse under vim (for scrolling, and selecting text) – Esc, :set mouse=a