Pages

Saturday, April 2, 2016

Working with short keys for vi editor

Home

vi and vim are command line Text editor.

One will always use these commands whenever you have a need of modifying a file using Linux command line

There are three basic modes in vi or vim command editor 
- Command Mode
- Insert Mode
- Execute Mode or Last Line Mode

Command Mode 
- Default mode of vi command
To be used for navigation for content of file
- Can't insert text immediately when you are in this mode, you need to press i to get moved to Insert Mode to get start with insertion of text

Insert Mode 
- In this mode you can insert text
- Arrow keys can be used to navigate file's content
- To navigate back to command mode [Esc] key can be used
- Consecutive use of keys [Esc] and [i] can be beneficial in all scenarios of editing text file
- Example:- searching locating string in a file using command line and changing it using insert mode

Last Line Mode (Execute Mode)
- You can go to this mode only from "Command Mode" by pressing key [:]
- Immediately after you press key [:] same colon symbol will appear in last line of your vi screen, which indicates that you are using "Las Line Mode"
- This mode normally used to save or discard alteration to file and related operations 

Below are some examples of one liners that i found beneficial in daily use,


- To quit vi command without saving file 

:q

- To force quit vi command without saving file 

:q!

- To quit vi command by saving file 

:wq

- To force quit vi command by saving file 

:wq!

:ZZ


- To get list of file names in your current working directory 

:!ls

You will be taken back to your command lines session as below,

[root@localhost ~]# vi testfile
anaconda-ks.cfg  configfile  file_out  testfile  test.sh  test.txt
Press ENTER or type command to continue

Press enter to resume back to you vi command session

- To get list of file names in your current working directory 

:set nu

- To insert text at current cursor position 

Press Key [i]

- To insert text at beginning of line 

Press Key [I]

- To append text after current cursor position 

Press Key [a]

- To append text after end of line 

Press Key [A]

- To open a new line below current line for insertion of text 

Press Key [o]

- To open a new line above current line for insertion of text 

Press Key [O]

- To copy current line's content (YANK) 

Press Key [y][y]



- To copy next 5 lines from current line (YANK) 

Press Key [5][y][y]



- To delete current line's content 

Press Key [d][d]

- To delete 5 lines, i.e. current line plus next 4 lines 

Press Key 5[d][d]

- To paste one or more lines copied using Yank after current line



Press Key [p]

- To paste one or more lines copied using Yank before current line



Press Key [P]

- To delete current character 

Press Key [x]



- To delete 10 characters 

Press Key [1][0][x]

- To delete current line's content 

Press Key [d][d]



- To delete current line's content 

Press Key [d][d]

- To search word foo in all lines starting from 1 and replace it with bar 


:1,$s/foo/bar/g




Reference :-

https://www.shortcutworld.com/en/linux/vi.html
http://www.kostis.net/hints/vi-editor.htm
https://linuxacademy.com/blog/linux/vi-short-cuts-for-beginners
https://www.cs.colostate.edu/helpdocs/vi.html


Back To Top
Home

No comments:

Post a Comment