my vi first steps

Posted April 26th, 2009 by j00p34

I remember when I first started administrating Linux systems, I had to use the vi text editor to edit configuration files. My first encounters with vi were not of the pleasant kind. I hated vi, to me it felt completely counter intuitive with its command and editing mode. Which it apparently inherited from the days where terminals had few keys and didn't sport all the typewriter functions we have today.

After a while of using vi I started to appreciate it's tremendous power. Nowadays I use it whenever I can. I must say vi has a steep learning curve but when you get past the first steps and have the feeling of the interface it's an incredible time-saver. It's comparable to the Linux command line interface, it takes a little while to learn but it's so efficient and powerful you get back the time you invested in learning a hundred fold.

While utilizing vi to it's full extent takes years of training and using it for all kinds of purposes, there are a few things I use all the time. Learning and getting used to only a few basic things doesn't take too much time and can already save you lot's of time.

So here's a list of the things I use all the time. I'll put in some pretty basic stuff here, so be warned if you already use vi you're probably not going to learn much here.

1. i for insert

If you want to type anything, create a text or insert a word in a text you need to enter editing mode first. Any text insertion command brings you to editing mode. This if one of the most frustrating things for beginning users. As you are used to start editing immediatelly as you see the text or the empty screen. If you do this in vi you'll probably end up with a half word or some messed up text. So when you want to insert something in a text you have to start with a text insertion command first. This one is not difficult to remember, i is insert. The only thing difficult to remember is you have to enter the editing mode first. I remember it took a while to get used to, just because I was used to start in editing mode in other text editors (notepad lol).
The big benefit of using two different modes is the plethora of easy keycodes at your hand. If you would always be in the same mode you would need a lot of combination keys to perform a lot of actions. Vi has a lot of single key and two letter combination commands for the most used functions. These keys are in close range to your fingers this gives a lot of speed.
Other insertion commands are: a,A,I,gI,o,O

2. d for delete

Deleting characters is something you do a lot when editing files, this is also frustrating for starting users. You open a file, position your cursor and start deleting the text using the backspace key. When using this works if you are in editing mode,in command mode it only moves your cursor back. This too seems like an annoyance at start, but when you are used to the deletion features of VI you can't imagine living without them anymore.
Using d and a movement command combined you can delete any sequence of characters as you wish. The combining of commands is one of the great powers of VI. If you use the d and move the cursor forward you delete the character the cursor is on, if you move it backwards the previous character is deleted. The great thing about this is you can choose to move any number of characters forward or backward at once. For instance: typing d6 followed by the movement backward key will delete 6 characters before the current position. Typing dd will delete a whole sentence at once.
Other deletion commands are: x,Del,X,D,J (delete is actually join),gJ,:d

3. y for yank

Copy/paste is something I use all the time too. It was one of the harder commands for me to learn because the word yank didn't say anything to me. I was used to copy, happily the paste command is p so that wasn't to difficult to remember. The same as for delete goes here, the great power of vi lies in the combinations you can make. You can combine the yank command with move commands to copy any part of the text at will. You don't need to select blocks with the mouse first, just type a movement command followed by y and you copy multiple lines/words or anything you want in to the buffer. Just use the p to paste the buffer in your current position.

4. u for undo

Undo your changes with the u. Especially if your startting to learn vi you are going to make a lot of mistakes at first. Thu undo command will be your friend. u will undo one change U will undo all latest changes. Use CTRL-R for redo.

5. q for record macro

Many of the commands in vi are based on the location of the key on the keyboard I assume this is the case with the key for macro recording. I can't think of any other reason to choose the q for this anyway. Macro recording is one of the things I like most about vi. This is such a great tool I often use it instead of the usual text editing tools on the command line like sed. The benefit of using vi is that you can do interactive editing.

For example:

I sometimes have to edit many almost equal configuration files in one directory. I have to replace domain names and ip addresses or some configuration option. The problem is often I don't exactly know what I'm looking for to replace. So I would need some time and trial and error to find the right regular expression and command to change the exact things I need changed. What I do is the following:
I open all files using vi *.conf

I look for the thing I have to change, often this is something different in all files but equal in format.
Then I look for some way to get to the position I need to be on. Like searching for a word before the line I need and stepping one line down or a number of letters forward. In this way I can easily locate the line I need and the position I need to be on. Find the sequence of commands needed to make the change. When I know the complete sequence of commands (easily found because you can do and undo with the u) I start recording by pressing q followed by another letter (just q again will do)

So qq and than the command sequence followed by :wn to write the file and go to the next opened file. When I'm in the next file I stop recording by pressing q again. When I now press @q my macro is executed again and the change is made in the next file too and I end up in the following file. The trick is now to execute the macro on all files at once. So if I know there are 80 config files I just do 80@q and all my configs are changed at once.

It's easy to do this and it saves me often when I need to have a quick and dirty approach to edit many almost the same things in a lot of text files.

One latest thing

There's a lot more to know about vi, but if you want to learn those just type vimtutor at the command line. If you have the full vim package this will bring you to the tutorial. One thing I like to do is create the default vimrc file, this will give you all kind of nice defaults like syntax coloring and search as you type. One easy way of doing this is starting vimtutor search trough the tutorial using /vimrc (that's type / for search followed by vimrc enter) copy the r $VIMRUNTIME/vimrc_example.vim from the text. Type :e ~/.vimrc and in the new opened file type : and than paste the r $VIMRUNTIME/vimrc_example.vim on the command line. enter and :w followed by :x to quit vi. next time you start vi you'll have the settings from the example vimrc. This will give you mouse activation in vi too (and switching screens using the mouse in vimdiff). There are easier ways of doing this but this one I find easy to remember.

Have something to say?

Am I doing things overly complex? Do you know better or more efficient methods? If you know better or easier to learn ways of doing the same, or if you have some easy tricks to share, please leave a comment.
It can take a while before comments get published (other time zone)


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Vi is great, I am using it

Anonymous 30 weeks 4 days 14 hours 1 min ago

Vi is great, I am using it for almost a year, thanks for teaching me some more tricks! Best regards, Huis Kopen

Thanks!

Anonymous 40 weeks 6 days 17 hours 50 min ago

I have been using vim for several years and I know many tricks but you have showed me new ones :) (I didn't know about :wn)

Best karma users

  1. wolfrickwilmer
  2. susan
  3. reet1983
  4. KarenBecker82
  5. jack1234
Custom Search