Introduction
- 'vi' is the default editor of RHEL and also the most widely used text editor.
- It is the also used as the default editor by other UNIX variants.
- 'Vim' (Vi iMproved) is the enhanched version of vi editor, a programmer's text editor.
Modes of operation
Vi / Vim operates in one of the following modes
- Insert Mode
- Command Mode
- Ex Mode
- Visual Mode
1. Commands of Insert Mode
- i -----> To insert text before the current cursor pointer
- a -----> To insert text after the current cursor pointer
- I -----> To insert text from the beginning of the current line
- A -----> To insert text from the end of the current line
- o -----> To open a New line below the current one
- O -----> To open a New line above the current one
- s -----> Substitute the current character
- S -----> Substitute the current line
- r -----> To replace the current character
- R -----> To replace the current line
- 'Esc'-----> To come out of the current line
2. Commands of Command Mode
* Cursor Movement
- h -----> To move one character left
- l -----> To move one character right
- j -----> To move one line down
- '<Enter>' -----> To move one line down
- k -----> To move one line up
- O -----> To move to the beginning of the current line
- $ -----> To move to the end of the current line
- gg -----> To move to the top of the file
- G -----> To move to the bottom of the file
- 'N'G -----> To move to Nth line of the file
- w -----> To move one word ahead
- b -----> To move one word back
- ) -----> To move one sentence ahead
- ( -----> To move one sentence back
- } -----> To move one para ahead
- { -----> To move one para back
- :set number ----------> To show line number before each line (Temporarily)
3. Commands of Ex - Mode
* Searching Text
- /text -----> To perform search in forward direction (Top-to-bottom search)
- ?txt -----> To perform search in reverse direction (Bottom-to-top search)
- n -----> To search next occurance of the searched text in forward direction
- N ----->To search must occurance of the searched text in reverse direction
* Save and exit
- w -----> To save the changes
- :wq or :x -----> To save the changes into the current file and exit
- :wq <filename> -----> To save the changes in the given file and exit
- :wq ! -----> To forcefully save into the read-only file
- q! -----> To exit without saving the changes
4. Commands of Visual Mode
- V -----> starts line oriented visual mode
- CTRL + V -----> starts block oriented visual mode
- y -----> To copy the selected text
- d -----> To delete the selected text
- p -----> To paste the copied / deleted text
5. To password protect a file (supported in Vim Editor only)
Example:
vim file1
.............
.............(text)
Esc
:X -----> Enter encryption key: mypassword
Retype: mypassword
:wq -----> Save and exit
6. Modifying / Removing password from a password protected file
Example:
vim file1
password ........
Esc
:X -----> Enter Encryption Key (Leave Blank to remove password)
Retype : (Leave blank and press enter)
:wq -----> Save and Exit
7. Recovering a unsaved data of a file after crash
Suppose, file1 is the file which was not saved and we want to recover:
# ls -a
(This command will show the .swp file of your original file which was
stored in the virtual memory (swap memory)).
# vi -r .filename.swp
(To restore / recover unsaved data of a file)
:wq (Save the file and exit)
Now, remove the .swp file after data recovery
# rm .filename.swp
8. Open the file in Read-Only mode
# vi -R <filename>
If you need any further help, go and try
# man vim or # vim --help in your terminal.
Thankyou.. :)
0 Comments