7:50 PM Working with VI Editor: Basic and Advanced | ||||||||||||||||
This article will get you acquainted with one of the most useful and frequent editor in Linux system. You can learn the basic commands to work with VI Editor.
Inside Vi Editor, how we do the editing, saving, deleting etc.
Search using pattern
To position the cursor on a line number in a file, we can try this:
vi +[num] filename
Eg. Promt:> vi +4 abc.txt
The above command will put the cusrsor to the 4th line in the file abc.txt
Find and replace
To find a word in the file and replace it with another word, we can give commands inside VI editor directly. For this we first open the file in vi editor then we give ":" followed by below command:
Prompt:> vi abc.txt
Eg. :%s/old/new/g The above command will find "old" and replace all occurances of "old" in the file by "new" But this command will be case sensitive. So next we will see the case insensitive word match and replacement.
Eg. :%s/old/new/gi The above command will search for the word "old" in case-insensitive manner and will replace the words old, OLD, Old, or any other permutations by the new word new. The same if you want to be done after a confirmation you can try:
Now the system will ask you before doing the replacement like below. Here you can give "y" to say yes, "n" to say no "a" for all "q" to quit and so on.. replace with this (y/n/a/q/l/^E/^Y)? Suppose you want to do substitutin in a certain range of file, then you have to specify start and end no. of lines of file where find and replace will take place.
Eg. :5,10s/old/new/gi The above command will replace any occurances of old by new from the opened file only in the range of line no. 5 to 10 in the file. Copy, paste, delete, undo and other editing inside VI editor
Delete one or more words in VI editor To delete a word in vi, put the cursor at the first character of the word and press 'dw'. It will delete the word starting at the cursor position. To delete more than one words, press 'dNw'. Here 'N' referes to the number of words to be deleted. Example: d5w will delete 5 words staring with the word at cursor postion in the same line or from next lines. If you dont place the cursor at first character of the word, the entire word will not be deleted. Only the characters starting from the cursor position will be deleted. Example: If the cursor is at 'e' in the below line and you press 'dw', it will delete 'enius' not the entire word genius. I am a genius. Output : I am a g. Numbering the lines of a file Suppose you want to number the all the lines of a file. You can use the "set -nu" command inside VI editor. Open The file by VI editor, give ":" followed by below command:
The above command will a numbering to all the lines starting from 1, 2, 3.. and so on.. To remove numbering from the file, we can use below command:
Setting environment as VI editor. The below command is used to set the Linux environment as VI editor. When we press UP/Down buttons, we can see recent commands.
Reading from another file into the current VI editor.
This command will read from the file name mentioned and append the content in VI editor Writing to a seperate file from vi editor If you want to save or write the current content of vi editor to a seperate file, you can use ":w" inside vi editor.
The above command will write everything present in vi editor to a seperate file as mentioned.
Splitting inside VI editor Suppose you have one window open in vi editor and you are working with a file or you are editing a file. Now, you want to open another file in the same window. You dont want to close the previous file You can open another file while previous file being opened. by splitting your vi editor. You can split your vi editor in two or more section to open more than one files in one go. You have one file open. Now use below command to open another new or existing file:
This will split the editor in two sections and now you have newfile opened at the top and the previous file at the bottom. See below figure: Split current window in two. The result is two viewports on the same file. Make new window N high (default is to use half the height of the current window). Reduces the current window height to create room (and others, if the 'equalalways' option is set and 'eadirection' isn't "hor"). Note: CTRL-S does not work on all terminals and might block further input, use CTRL-Q to get going again. Also see |++opt| and |+cmd|. VIM editor: open mutiple files in one go To open several files in one go, we can vim instead of vi. Lets say, I want to open file1, file2, file3 in one go, i can use below command at the prompt
so it will open all three files, to jump from one file to anoter, use ":n" inside vi editor. To open multiple files in several windows Now the requirement is I want t open two or more files in seperate windows either vertically or horizontally inside a vi editor.
-o[N] Open N windows stacked i.e. horizontally one over another. When N is omitted, open one window for each file. -O[N] Open N windows side by side i.e. vertically. When N is omitted, open one window for each file.
To enable/disable mouse inside vi editor Many times for editing, we face prolems in vi. We may need to enable or disable mouse to select texts for copy/paste/edit. To enable mouse:
Once mouse is enabled, you will enter into visual mode and see a mouse pointer inside vi editor. You can use that pointer to select texts for easy editing. To disable mouse again:
The mouse pointer is gone, back to normal mode.
To open a file with the first match of a keyword vi +/search-key file
| ||||||||||||||||
|
Related blogs
You may also like to see:
[2014-09-13] | [Open System-Linux] |
md5sum: calculate and check md5 message digest of a file in Linux |
[2014-03-11] | [Open System-Linux] |
STRINGS : a command in linux to read the non-text files |
[2016-05-24] | [Open System-Linux] |
FACTER command in Linux : showing system facts |
[2015-06-01] | [Open System-Linux] |
DIG command : A DNS lookup utility |
[2014-01-09] | [Open System-Linux] |
How to send a mail using unix/linux terminal? |
Total comments: 1 | |
| |