The Linux world is home to some unusual controversy. None is greater than the phylosophical battles regarding text editors. Some will tell you emacs is the best, while others will argue for vi as the supreme editor. I happen to like vi though I will tell you about emacs in a later article. The vi editor holds an almost mythical place in tech lore, with jokes like “vivivi, the editor of the beast” (think Roman numerals). You can almost hear this fearsome voice echoing eerily across the walls of your office or home; “I am vi, the Great and Powerful!” Yes, if there is one editor that strikes fear in the hearts of newbies alike, it is certainly vi (or “vim“), the visual editor. “vim”, by the way, stands for “vi improved” and is the version of “vi” that you will find in your Linux system. Anyhow, pay no attention to that fearsome voice behind the program. vi, or vim, is not so frightening once you get to know it.
Moving around with vi is easy. Depending on the terminal emulator you are using, you can usually simply use your cursor keys. In the absence of cursor key control, the up, down, and sideways motions are all implemented with single keystrokes. To move left, touch the letter “h”. To move right, touch “l”. The letter “k” is up, while “j” is down.
Here is a chart to help you remember. Trust me when I say that this will all become second nature once you start working with the program.
These commands let you move around
The next few commands let you start inserting text in various ways
Deleting or changing characters, lines, etc.
When you work with vi, the escape key (Esc) is your friend. If you don’t know where you are of what mode you are in (insert, replace, append), hit the escape key. You’ll go back to normal vi command mode. Your second best friend is “u” or “U” which stands for undo. The upper case undo will undo every change to the current line where the lower case undo is the last change only.
Writing and Quitting
All done editing? When it comes time to save your work, hit Esc (to get out of whatever mode you are in) and type “ZZ”. Another way to exit is to type “:wq” (write and quit). At any time during an editing session, you can type “:w” to save your current work. Finally, if you really don’t want to save anything you have done here, type “:q!”. The explanation point essentially says that you won’t take no for an answer. Had you modified the file and simply typed “:q”, vi would warn you that you were trying to exit from a modified file without having saved your changes.
Need help while in “vi”? Make sure you aren’t in insert or replace mode, then type “:help” (without the quotes).
I urge you to not let “vi” frighten you. Get to know it. The likelihood that you will ever log on to a modern Linux (or Unix) system that doesn’t have some form of vi installed is virtually non-existent. That said, if you need more than I’ve given you here, then consider the “vi tutor”. This little tool may or may not be installed on your system, so to run vimtutor, you may need to install some additional software. Those of you running Ubuntu or some variant should installed the vim-runtime package.
sudo apt-get install vim-runtime
For Red Hat like distributions like Fedora or Centos, install the vim-common package.
yum install vim-common
You’ll then be able to run vimtutor. The cover image for this article shows the vimtutor in action; just scroll to the top to see it.
Recovering a VIM session
From time to time you may find yourself trying to edit a file but someone else, maybe you, is already editing it. That session may either be open, or something may have happened to terminate it accidentally. As a result, you get a nice long-winded message along the lines of “swap file found” and a whole lot of information about what you could do about it. Here’s a much shortened version of that message.
E325: ATTENTION Found a swap file by the name ".ftl_ship_design.txt.swp" owned by: mgagne dated: Fri May 3 11:12:24 2013 file name: ~mgagne/Documents/ftl_ship_design.txt modified: YES user name: mgagne host name: fullhouse process ID: 28303 While opening file "ftl_ship_design.txt" dated: Fri May 3 11:12:19 2013
To locate these files, you could use our famous “find” command and look for anything with a “.swp” extension. Another and better way is to have vi report on these for you. You can check for the existence of swap files by using the “-r” option which will provide you will a little more information than the simple find.
[mgagne@fullhouse mgagne]$ vim -r Swap files found: In current directory: 1. .ftl_ship_design.txt.swp owned by: mgagne dated: Fri May 3 11:12:24 2013 file name: ~mgagne/Documents/ftl_ship_design.txt modified: YES user name: mgagne host name: fullhouse process ID: 28303 In directory ~/tmp: -- none -- In directory /var/tmp: -- none -- In directory /tmp: -- none --
Power vi : startup options
Here are a couple of cool tips to whet your appetite before you run off to the man pages or a bookstore for the latest and greatest 500 page vi reference guide. First, vi makes it easy to get right to the content you need. No need to scroll through reams of text to get what you want. Consider the following :
vim +100 ftl_program.c
This will take you right to line 100 in the file called (in this case), ftl_program.c . This can be a great little time saver when you are compiling programs and something goes wrong (as in the following example.
gcc -O2 -Wall -c –o ftl_program.o ftp_program.c ftp_program.c:100: parse error before `<' make: *** [ftp_program.o] Error 1
Another useful start flag is the same one you use to search for text inside a file, the forward slash. In the next example, I wanted to get back to the same place I was working on in my file. To mark my place, I had written my name on a blank line. This vi starter gets me right to where I was working.
vim +/Marcel ftl_program.c
Note the plus sign before the slash.
You can also turn on color highlighting in vim by typing “:syntax on” while in the editor. To turn the color highlighing off, it’s “:syntax off“. Note that in your Ubuntu or Mint distributions, you’ll need to have the vim-nox package installed. On the Red Hat or Fedora distributions, just make sure you call the vim command as opposed to vi. You can also turn on line numbering in the file by typing “:set nu” and turn it off with “:set nonu“.
If you always want to have color highlighting on and the numbers at the left, you can just add those commands to a .vimrc file in your home directory (note the dot). When you start vim, it will read those settings and you’ll have the editor of your dreams.
I’m particularly fond of this final tip. By design, vi is a text editor and basic text is our medium. As demonstrated earlier, you can use Control-V to insert control characters into your text, but you can also edit binary files. Here’s a crazy little example. There’s a great little game which you should probably have installed when you set up your system. It’s called “xbill”, a wonderful little piece of strangeness where you must stop a renegade program known only as “Bill” from spreading a most insidious computer virus, cleverly disguised as an operating system. If you don’t have this game, install it. But I digress . . .
Okay, I know that I can get the source and modify the program without going into the scenario I am going to describe, but trust me, there may be times when you have a binary file (of some kind) with no source and all you need is a simple modification. You don’t want to entertain this lightly. Changing things in this mode could leave you with a non-functional program or worse, but we are still going to do it. Just remember to make a backup. In my case, I made a copy of the xbill program into my home directory. If you pause in the middle of a game, you get the following message.
For reasons I won’t go into at this time, I didn’t like the fact that it said “Continue”. I would rather it said “Rock On!”. Since I only have the source, I start vi with the “-b” option which lets me edit a binary file. To make life just a bit easier, we are going to combine that with another helpful little command line option.
vim –b +/”Press Continue” /usr/games/xbill
This will put me right on the line I want. There I will change the word “Continue” to “Rock On!”.
Here’s what it looks like if I press “Pause” during a game now.
Notice that I used exactly the same number of letters for my change. Since this is a binary, the amount of space I had to work with in order to make my change was defined at compile time. Breaking this rule may get you the dreaded “Segmentation fault (core dumped)“ message. Using less space is probably okay if you fill with spaces.
And this is where I leave it for today.
As usual, if you wish to comment, please do so here on Google Plus or over here on Facebook and add me to your circles or friend list if you haven’t already done so; oh yeah, if you’re on Twitter, follow me there. Also, make sure you sign up for the mailing list over here so that you’re always on top of what you want to be on top of. Until next time . . .
A votre santé! Bon appétit!