In the post “vi Editor Basics”, I covered the basics of using the vi editor, including command and input mode, file management, some window motions, cursor motions, deletion and input commands. In this part, I will cover some more advanced features like word substitutions, yank/put (copy/paste), regular expressions and search/replace.
First up, lets talk about yanking and putting. This is vi‘s copy and paste and is very useful with very little to learn. Basically, vi uses buffers to store information. Things like deletion (dd and dw) and yanking (see below) are sent to a buffer which is either the general buffer or a specific buffer of your choosing. At a later time, you can then put the information stored inside a buffer into the text file you are working on.
To yank (copy) a line, you use yy like so:
- yy # yank a single line to the general buffer
- “a5yy # yank 5 lines to the buffer a
To cut a line, you use dd like so:
- “a5dd # delete 5 lines to buffer a
- “A5dd # delete 5 lines and append to buffer a
Then to put the contents of the buffer in the file at a different location, you use p like so:
- p # put the general buffer after the cursor
- P # put the general buffer before the cursor
- “ap # put the contents of buffer a after the cursor
Personally, I’ve gotten into the habit of always specifying the buffer I place my yanked/cut text into, that way I only have to remember three commands (“a5yy, “a5dd, “ap), but you may like to use the others – this is only my personal preference.
One of the [arguably] most useful features of vi is its search and replace commands. With these very powerful tools, you can do in seconds what would take minutes or hours the long way.
There are four parts to any search and replace command:
The s will always be there, and the old-text, new-text fields are self-explanatory. As for the [place-to-search], you have a number of options:
- . # specifies the current line
- n # specifies line number n
- .+x # specifies the current line plus the next x lines
- $ # specifies the last line
- /string/ # specifies a line containing string
- % # specifies the entire file
- [start],[stop] # specifies the range from start to stop
The trailing c,g,i,I options work as follows:
- # [left blank] only finds and replaces the first occurrence per line
- c # prompts to confirm each substitution
- g # greedily replaces all occurrences it finds
- i # ignores the case for old-text
- I # doesn’t ignore the case for old-text
I’ll give you some examples.
To replace the first occurrence in each line of the word for with the word and in the 10 next lines including the current line, you would use:
If you want to replace every occurrence in each line of the word for with the word and in the 10 next lines including the current line, you would use (note the trailing /g):
Replacing .,+9 with % will search and replace through the entire file ignoring case of For:
Replacing the +9 with $ will search and replace from the current line to the end of the file without ignoring the case of For:
Replacing the . with -9 will search and replace from the previous 9 lines through the next 9 lines of the file and confirm each substitution:
And so on and so forth.
I don’t use the change or replace commands very often, but they are there in case you want to use them:
- cw # this changes the current word
- cc # this changes the current line
- ra # this replaces the current character with the letter a
- R # this allows you to type over characters in the current line
I don’t often move text from one file to another, but you can do that as well. The following commands will open the file filename1 at /path/to/some/, yank 5 lines from the cursor to buffer a, write the work to a buffer, open a new file, filename2 at /path/to/new/ for editing, put the yanked lines from buffer a after the cursor and save the new file.
vi /path/to/some/filename1 "a5yy :w :e /path/to/new/filename2 "ap :w
Phew! Hopefully this will help you using the vi editor quite a bit. With these commands, you should be able to become a vi rockstart with a bit of practice. Keep in mind that this isn’t an exhaustive guide either. You can do things like open two files for editing in diff[erence] mode, etc., but this should give you a very strong start.