Linux Gazette's Utility Room

# The Linux Gazette'sUtility Room

### I would like this segment to be used to give tips and advice on the various utilities that are available for Linux. If you have ever gone through the /bin and /sbin directories you have probably seen dozens if not hundreds of programs and have had no clue as to what most of them do. Their usefulness varies but I have found many a life-saver in one of those two of three letter utilities. Unfortunately there are still a lot of them I don't know anything about.
There are also a lot of commonly used utilities that have interesting applications or features that many people don't know about. I figure I will share what I know with you, and you can share what you know with me, and we can all get something out of this.
I will start off with one of my favorite utilities vi, becuase I have found that it is the most used and least understood. First I would like to say that while I am big vi fan, I by no means think that this is the editor that everyone should use. There are good reasons to use other editors, however, vi is always available. My first reaction to vi was probably about the same as anyone's. "MY GOD, what is going on????" I then had the good/bad luck of having someone chuckle at my misfortune. That was enough to insure that I would devote myself to learning vi.

Let me start out with a list of some of the keys and what they do.

## Top 10 vi commands.

- `esc......couldn't use vi without it. ;)`
- `i........insert of course`
- `x,X......delete current/last character`
- `ZZ.......save and quit--same as :wq`
- `dd.......delete line`
- `o/O......insert new line before/after and insert`
- `p/P......put yanked or deleted text before/after`
- `y/yy.....yank 1 character/yank one line`
- `R........overtype from this point onward`
- `/ ?......search forward--search backwards.`

## 2¢ vi Tips

- `1G` takes you to the top of the file.
- `0G` takes you to to bottom of the file.
- `d$ and D` delete from the cursor to end of line (especially good if 'end' key is mapped.)
- `d0` deletes from the cursor to beginning of line (see above about mapping.)
- In command mode `CTRL-G` shows the line number you are on.
- `m#` -where # is a letter, sets a mark, `'#` takes you back to that mark, `# takes you back to the exact same place.

I would like to start out by saying that there are many versions of vi out there. There is vi itself, vim, elvis, nvi, etc. Most of the commands that I talk about here should work with all or at least many of them. However, I personally use vim and I will cast my recommendation for that particular flavor of vi. I would also like to note that I use vi to refer to any of the vi flavors, not just the original

The last time I talked about vi I talked about textural substitution. Now I would like to talk about key mapping. vi will let you map any key to any series of commands. These mappings will work as long as you are in command mode. I should forewarn you that vi has no problem with you changing existing key definitions. I do not recommend this however; you could be begging for trouble. These mappings can be applied either at the ":" prompt or in your .exrc, .vimrc,.elvisrc, etc... file. Let me share one of my favorite key mappings with you.

                                       map T ^[I     ^[^[OB

#### `Here "^[" is CTRL-v then ESC and "^[OB" is CTRL-v then Down-arrow.`

###

This little macro takes care of one of my greatest editorial banes. Whenever I program I always find that I need to add one more encompassing 'if' statement. What this means to all of you non-programmers is that I need to tab over a whole bunch of lines by hand. What this macro does is

- 1) Makes sure it is in command mode.
- 2) Inserts at the beginning of the line (Capital I)
- 3) puts a tab in, and
- 4) Drops me down one line.

This macro works well for all versions of vi. However, most versions of vi support a command that does almost this exact thing for you. A line is shifted to the right with the command "<<", and the command to shift a line the other way is ">>." To adjust the width of the shift you can set the shiftwidth variable(in vim). ":set shiftwidth=#". There is one big advantage to using the built in version; you can precede the command with a number to tell it how many lines you want to tab over.

Not all systems and not all versions of vi have the `insert`, `delete`, `end` and `home` keys defined. These can all be easily defined in the vi rc file.
This method will also let you make use of those underused `F`-keys. Want a save button. Boom, there it is. Want a save and exit, Exit without save, spell check button, compile button? Any command that vi will let you execute you can set up as a key-mapped macro.
Another good mapping is the map the backspace button to `esc-X`. This way the backspace will behave more like you are used to. You can also map it to `esc-X-i`; this will put you back in insert mode without a hitch. 8)

Well, this is a lot of fun and I have a lot more interesting vi tricks to share, but they will have to wait for next time. Some of the things I hope to show you are:

- Multi-file editing
- Buffers/clipboards
- Shell commands from within vi
- Search/Search and Replace
- Jump tags
- More interesting Mappings (I hope to hear from you.)
- Some of the less commonly used keys.
- The repeat command

Well, that ought to just about do it for vi then. It shouldn't take more than a few more articles to finish this off. If anyone is interested in having me learn emacs and write some stuff up on that, please let me know, otherwise I will leave it at that and move on to other things. If I miss any interesting aspects of vi please let me know.

### Back to Linux Gazette #8