Essential and practical vi – Part 2

vi

First, check out this nice post about every day, very practical vi stuff you can’t do without. Originally I only wanted to do a piece on vi buffers but the post grew and grew and here we are, at the second part.

In this post I want to explain some searching, search and replacing, more powerful usage of the last line mode and buffers.

Searching in large files is very valuable. First understand that every search is case sensitive by default. You can make your searches case insensitve by this last line command:
:set ic

Revert it to the default case sensitive:

:set noic

Now start your search by pressing the / key and entering the text to search for, followed by an enter to search. To find the next occurrence of your search term press n and N for the previous. To reverse search, begin your search with a ? instead of a /.

Not too complicated huh? Search and replacing, essential for a lot of files I’m working on, needs just a little bit more attention. From last line mode:

:s/server01/server02/g

This will replace every occurrence of server01 in the current line to server02. You can also do the entire file at once:

:%s/server01/server02/g

Add a c at the end to confirm every occurrence. These two I use a lot, but there is more powerful commands to be used. This page has some more good stuff.

What’s rather new for me, but I want to start using, are marks. There can be a  lot of jumping around in vi. There are many shortcuts to jump a file and I think marks are quite unique. First mark a line you want to return to later:

ma

This sets a mark named a at the current position. To later return to this exact position, use:

`a

To return to just the line you set the position, use:

'a

You can use any letter of the alphabet to set a mark. There is a lot more to be done with marks, but I only use it for this purpose.

Pro tip when having more than 1 file open: leave a mark with a capital letter and you can jump between files. Marks are persistent, even after closing a file. Leave a mark with a capital letter, save and close, open another file, jump to the capital mark: this opens up the first file at the correct mark, in a different buffer (next post!).

See all marks:

:marks

Delete them:

:delm! | delm A-Z0-9

In the last line mode, you can do a lot more than saving and quitting your file. First here’s a pretty basic but often overlooked trick before an important edit to a file:

:w ~/otherfile.txt

This saves the current state of the file, let’s say it is named myfile.txt, to a file otherfile.txt in the home directory. You’re still working in myfile.txt though. So any edits after this save, are just as normal edited in the original myfile.txt.

As easy you can write out files, you can also read them in. Say you want to insert a file in your current working file, at your current position:

:r ~/Documents/important.txt

You can use tab completion as well. Let’s say that you can’t find your document though. Let’s find it, right from within vi with the ! key that marks a command:

:! find ~ -name important.txt 2> /dev/null

This will jump out of vi and eventually give you the output of your command (I’m rerouting errors to /dev/null to avoid permission errors cluttering the output).

Combining the two is also easy. Let’s say you for some reason want to read in the output of a command to your current file:

:r ! ls -al

Will insert the output of ls -al to your current position in your document.

Fun thing I’ve discovered recently is that your can do remote editing as well, with a simple command. Let’s say I want to open up a port in my CSF firewall.

$ vim scp://root@myserver.external.domain.com//etc/csf/csf.conf

And restart the service remotely:

$ ssh root@myserver.external.domain.com 'csf -r'

This gets even easier when you have your server configured in your ~/.ssh/config.

This post grew as I went and I’ve decided to dedicate a separate one to buffers, windows splitting, etc. Next week!

Update: Complete series here, here and here!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.