Power vi

So now you’ve got the basics of vi down, but you’re thinking “so what?” Well Miles, let me tell you. vi has a lot of cool features that a jazz man like yourself is going to be able to riff on for a long while. It isn’t the archaic version of notepad that you think it is. Did you know that vi has 26 unique buffers for you to copy text into, and an additional 10 that save things you’ve deleted? That’s like having 26 clipboards you can use. Have you ever tried opening and changing a file in scite, only to find that you couldn’t save it because you didn’t have permission? In vi that isn’t such a big deal because you can force vi to save the file. Or did you know that you can compile and run the text you’re working on by writing a macro that makes a system call with whatever file you’re working on as the argument? That was one of the things I thought only scite had.

In my last blog entry, I mentioned how to mark text using the ma command. What I didn’t mention was that any character works, from ma to mz, giving you 26 marks. I also simplified copying by using the d`a and y`a commands. Again, you have any of the 26 letters instead of a to use as your mark. You also have 26 usable buffers. A buffer is like the clipboard in Windows, but you now get 26 of them instead of just one. To throw everything from the current cursor back to mark a into buffer b, use the
command. You can put buffer b into the current cursor location using

I always open up /etc/apt/sources.list as the user dsl instead of root. Then when I go to save my changes, I find that the file is read only. If I had been using vi, I could have saved the file using the :w! command, which forces the save, even on a write protected file. Dangerous sometimes, but handy to know if you need it.

Another useful feature of vi is being able to run shell commands without leaving vi. The :! command runs a shell command, and the % sign is replaced automatically by the name of your current file by vi. You could have run the file you are editing with
:! perl %
or whatever runs your script.

Another fun thing to try out is the macro feature. You use
:map [name] [what to do]
to make a macro of your own. To include control codes and carriage returns, press CTRL V and then your key. So for the perl macro you might do
:map <F7> :! perl –w % (and hit CTRL V and the enter key, then the enter key again to end the line)
After that, hitting the F7 key starts perl with your file as the argument. You could set up keys to run makefiles, or anything else.

.vimrc is the vi configuration file (it’s in /home/dsl). By adding your map commands here, you’ll be able to have your macros ready for every file you open. Make a copy of your .vimrc file called newvim. Then add any macros you wrote. Now you can try them out by opening vi using
vi –u newvim
After you get it right, you can replace .vimrc with your new one.

One last nugget of vi before I stop is the search and replace feature. The basic form is
this replaces the first occurrence of old on the current line with new.
replaces all occurrences of old with new on the current line
replaces every occurrence of old with new on every line in the entire file. Regular expressions can be used as well. Here is one that I use all of the time to replace /ramdisk/ with a single / when I’m creating *.dsl files:

Look around, play a bit and there is a whole world hiding in the vi program in DSL.

Comment viewing options

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


Cool! Didn't know that vim could do all of that! But I knew it was pretty powerful.

Deep Thought was wrong. The Answer isn't 42, it's DSL!