A few months back I came across an instance of using VI Editor. Trust me being a SQL DBA it was quite a task to remember the editing commands. So I made myself a list of commands that came in handy. Off course you may find amples of site providing the same info. Am sharing across my list hoping it may be helpful.
Basics
In this mode all the keys pressed by the user are considered as executable commands. Once the Vi editor is invoked it enters into the command mode. To return to command mode from any of the below mode press ‘ESC’ key.
-> Insert Mode
This mode permits insertion of new text, editing of the existing text & replacing of the existing text for the file.
To enter in the insert mode by selecting any of the below keys.
I, i, A, a, O, o, R, r, C, c
-> Ex Command Mode
This mode permits the user to give commands at the command line (the bottom line of the vi editor screen).
The command line is used to display messages & commands.
All block commands are executed in this mode
Any commands proceeded with the : (colon) symbol are given in this mode.
Basics
- VI stands for visual editor.
- Can handle text files.
- Its case-sensitive. Needs to be dealt with care as no error messages appear like while executing SQL queries or UNIX commands.
VI editor modes of operation
-> Command Mode
In this mode all the keys pressed by the user are considered as executable commands. Once the Vi editor is invoked it enters into the command mode. To return to command mode from any of the below mode press ‘ESC’ key.
-> Insert Mode
This mode permits insertion of new text, editing of the existing text & replacing of the existing text for the file.
To enter in the insert mode by selecting any of the below keys.
I, i, A, a, O, o, R, r, C, c
-> Ex Command Mode
This mode permits the user to give commands at the command line (the bottom line of the vi editor screen).
The command line is used to display messages & commands.
All block commands are executed in this mode
Any commands proceeded with the : (colon) symbol are given in this mode.
Invoking vi editor
$vi file1
The vi editor shows the full screen view of the file. If the file isn’t long enough to fill the screen; vi editor shows tildes(~) on the blank lines beyond the EOF.
Moving the cursor
Quitting Vi editor
Block commands in vi editor
First press the ‘Esc’ key to enter the command mode.
Then the ‘:’ key to begin with block commands.
To display line numbers enter the command ‘set number’ after following the above commands.
To turn off the numbering type command ‘set nonumber’ after following the above commands.
$vi file1
The vi editor shows the full screen view of the file. If the file isn’t long enough to fill the screen; vi editor shows tildes(~) on the blank lines beyond the EOF.
Moving the cursor
- h-moves the cursor one character left
- 2h-moves the cursor 2 characters left
- l-moves the cursor one character right
- 3l-moves the cursor 3 characters right
- j-moves the cursor one character below
- 4j- moves the cursor 4 characters below
- k-moves the cursor one character above
- 5k-moves the cursor 5 characters above
- w- moves cursor one word forward
- b- moves the cursor one word backward
- e- end of the current word
- 0- moves cursor at the beginning of the current line
- $-moves cursor at the end of the current line
- +-moves cursor below the beginning of the next line.
- --moves the cursor above the beginning of the previous lines
- H- go to the first line on the screen
- M-go to the middle line on the screen
- L- go to the last line on the screen
- G-go to the last line of the file
- Ctrl+f – Scroll forward one window
- Ctrl+b – Scroll backward one window
- i- Inserts text before the current position
- I-Inserts text at the beginning of the file
- a- Inserts text after the current position
- A- Inserts text at the end of the file
- o -Inserts a blank line below the current position
- O-Inserts a blank line above the current position
- c –Change current object
- C-Change from current position till end of line.
- r –Replace character at current position
- R- Replace all characters until <ESC> is pressed.
- dw –Delete current word
- dd –Delete current line
- d0- Delete from current position to the beginning of the line
- d$ -Delete from current position to the end of line
- x- Deletes the character directly under the current position
- <n>x- Deletes n characters
- <n>dw –Deletes n words
- <n>dd- Deletes n lines
- J – Join the EOL character. Join the current & next line.
- <n>J –Join the next n lines
- u –Undo the effect of the last command
- U –Undo all changes to the current line since the cursor was moved to this line.
- ~ - Changes the character in the current position from upper to lower & vice-versa
- :sh –Temporarily returns to the shell to perform some shell commands. Type ‘exit’ to return to the vi editor.
- /<string> -Search forward to the next occurrence of the string
- ?<string> -Search backward to the next occurrence of the string
- ^<string>- Search for all the lines which begin with the string
- <string>$-Search for all the lines which end with the string
- \<<string>-Search for all the words which begin with the string
- <string>\>-Search for all the words which end with the string
Quitting Vi editor
- :q! –Will terminate the file whether or not the changes made in the buffer were written
- :wq –Write all changes & quit editor.
- :w file1 –Write all changes to file1 & quit editor
- :q –Quits editor if the changes made were written to a file
Block commands in vi editor
First press the ‘Esc’ key to enter the command mode.
Then the ‘:’ key to begin with block commands.
To display line numbers enter the command ‘set number’ after following the above commands.
To turn off the numbering type command ‘set nonumber’ after following the above commands.
- :4,12d -Lines 4 to 12 should be deleted from the current.
- :5 mo 6 -Moves line 5 after line 6
- :5,7 mo 9 -Moves lines 5 to 7 after line 9
- :10 co 11 -Copies line 10 after line 11
- :10-15 co 16 –Copies line 10 to 15 after line 16
- :21,31 w file1- Writes lines 21 to 31 to file1
- :21,31 w>>file1 –Appends file1 with lines 21 to 31
No comments:
Post a Comment