Vi Tip of the Week: Find the matching brace

This is part of a series of tips on using Vi.  To see the complete series, click here.

A lot of the time when I’m working on a program, I want to quickly move my cursor to the matching brace.  For example, if I’m editing a nested if statement in C or Java, sometimes I’ll want to find the matching brace so that I know I’ve got my scope correct.  It’s very easy to do in vi — just place your cursor on the brace you want to match, then press the “%” key, and voila, you’re taken to the matching brace!

This works for parentheses and square brackets, too — it’s great for when you’re writing a long mathematical expression and you want to quickly check to make sure you’ve got your parentheses right!

Vi Tip of the Week: Move the current line to the centre of the screen

This is part of a series of tips on using Vi.  To see the complete series, click here.

Sometimes when I’m editing a file, I like to have the line I’m looking at centred on the screen.  This helps me to see an equal amount of context both above and below that line.  I seem to never remember the shortcut that centres the screen on the current line, so I end up using the j or k key to move around in the document until I’ve got the line I want in the middle of the screen, and then I move back to that line.  That’s a waste of effort, but vi has a nice easy way of doing it: press z then the period (z.), and it does it for you!

Let’s see what this looks like.  In this screenshot, I’m editing a file, and I’ve got my cursor on the comment line that starts “This function calculates…”:

Before centreing the current line

But that’s too low on the screen for my liking; I want that line centred.  Press z period (z.), and my screen now looks like:

After centreing the current line

The line with my cursor is now in the centre of the screen!  OK, it’s not that exciting, but the shortcut saves a bit of time when you want to do this.

There’s a couple variations on this that come in handy, too.  I can move the current line to the top of the screen by pressing z then the return key (z RETURN), or I can move it to the bottom of the screen by pressing z then the minus sign (z-).

Vi Tip of the Week: What line number am I on?

This is part of a series of tips on using Vi.  To see the complete series, click here.

If you are editing a file in vi, and you want to know what line number you are on, it’s very easy to find it: press Control-G, and the status line at the bottom of the screen will tell you!  See, for example, the following screenshot taken just after I pressed Control-G:

Vi: screenshot after pressing Control-G

As you can see, the status line displays the name of the file I am currently editing (in this case, Hamurabi.java), the line number that my cursor is currently on (line 30),  the total number of lines in this file (351), how far down into the file I am as a percentage (8% of the way through the file), and which column that my cursor is currently on (column 9).

Depending on which version of vi you are using, your status line may look slightly different. On both a Mac (where the above screenshot is from) and on a Debian Linux box, I see identical status lines.  However, with the version of vi on Fedora Linux, I don’t even need to press Control-G, as the status line always tells me which line and column I’m on:

In this case, the “30,9” indicates that my cursor is currently on line 30, column 9.  The “6%” on the right-hand side of the status line tells me that the top line displayed in my window is 6% of the way through the file.  If I press Control-G now, I get a little bit of additional information:

As with the Mac and Debian versions of vi, I am told the name of the file, how many lines in the file, and how far I am through the file.  It’s the same information, just presented slightly differently.

Vi Tip of the Week: Open a file in read-only mode

This is part of a series of tips on using Vi.  To see the complete series, click here.

Sometimes I want to open up a file to look at it, but not change its contents.  I could use the commands “more” or “less”, but I prefer to use vi for longer files, mostly because I’m so used to using vi.  To do so, I can use the command:

vi -R filename

where “-R” tells vi to open the file in read-only mode.  Although, I never open it with the -R option; instead I use the command:

view filename

which also opens the file in read-only mode.

Sometimes, though, I’ll be looking at the file, and realize I actually do want to make a change to it.  The hard way would be to close the file, then open it again in the regular editing mode, but instead, I can use the command mode to force a write:

:w!

If I use “ZZ” or “:w” to try to write the file, I get an error message telling me that the “readonly” option is set, but if I use the exclamation mark “:w!”, I can force vi to write the file.

Vi Tip of the Week: Go to a specific line

This is part of a series of tips on using Vi.  To see the complete series, click here.

Sometimes when I open up a file in vi, I want to go to a specific line.  Perhaps I had a compile error on a certain line, and I want to take a look at that line.  It’s nice and easy to do: when in command mode, just type the line number, followed by an uppercase G.  So, for example, if I type:

35G

I’ll be taken to line 35.

Actually, in the situation I mentioned, there’s an even better way to do it — when I open the file, I can specify what line to go to with the +linenumber option on the command line.  For example, to open a file called dummy.txt and go directly to line 35, I can type:

vi +35 dummy.txt

and the cursor will be placed on line 35.

There are a couple variants that I use frequently: if I want to go to the first line, I enter:

1G

and if I want to go to the very last line of the file, I don’t put a number first, just the uppercase G

G

takes me to the last line.

Vi Tip of the Week: Transpose characters

This is part of a series of tips on using Vi.  To see the complete series, click here.

OK, this is my favourite command in vi, not because it’s especially useful, just because I think it’s neat.  If you make a typing mistake and get two characters switched around, it’s easy to fix.  For example, if I want to type “Linux”, but I mistakenly type “Liunx”, all I have to do is place my cursor on the “u”, use the “xp” (transpose) command, and it swaps the “u” and the “n” around, making “Linux” as I originally wanted.

I sort of lied; this isn’t a single command — it’s two commands that result in transposing the characters.  The first command, x, deletes the character under the cursor and saves it in vi’s “unnamed” buffer.  Then, the second command, p, spits out what’s in that buffer, placing it after the cursor.  So, the result is that the two characters are transposed (fancy word for switched around).  What I think is cool is that these two separate commands, x and p, when combined as xp, perform an operation that is easy to remember: xp = transpose!  Cool!!!

Vi Tip of the Week: Read the manual!

When I teach a class that is using Linux, I always encourage the students to use the Vi editor, rather than something simple like gedit, just because Vi is so powerful AND you can use it on a terminal.  But, the students are often confused by Vi, and because I’ve used it for so long and it’s just second nature to me, I’m surprised at how many students don’t use the great features of Vi and just use it like gedit.  So, I thought it would be a good idea to start a series of tips on using Vi, and this is the first one.

This week’s tip: Read the manual!  There’s a great manual written way back in the either the late 70s or early 80s by Bill Joy, the creator of Vi.  It’s called An Introduction to Display Editing with Vi, and if you work through that, you’re going to know more than probably 95% of the people who use Vi.  Anyone who wants to start using the true power of Vi should read this manual.  Actually, even as someone who has used Vi since 1982, everytime I re-read the manual I always find some command that I had forgotten about.

Be warned, though, that some of the things described are more relevant to a 1970s timesharing system than today’s computers: do we really have to worry about editing on slow terminals nowadays???  But, for the most part, this is a great manual.

You should be able to do a google search for the manual, or it’s available in the FreeBSD documentation at http://docs.freebsd.org/44doc/usd/12.vi/paper.html.

This is part of a series of tips on using Vi.  To see the complete series, click here.

Upgrading to Fedora 16

I upgraded my Fedora installation from Release 15 to Release 16 today. It was a very smooth upgrade, although I don’t run much on Fedora — mostly trying things out for teaching in class. I was very impressed, though, with how well the upgrade went.

It wasn’t completely seamless, though, but that was my fault. Let me explain. I’m running Fedora in a virtual machine on my Mac, using VirtualBox. My primary Linux box is a Xiao Hu thin client computer, a MIPS processor based system, and I run Debian on it. But, because I’m teaching a Linux class using Fedora, I wanted to be able to test things out and make sure that things I was used to doing in Debian worked similarly on Fedora. Anyways, because I don’t use Fedora full-time, I created a small 8GB disk to hold the OS. Unfortunately, I needed more than that much space to upgrade, and that caused a slight, but easily manageable, hiccup.

My disk is configured with a 500MB /boot partition, a 6GB / partition, and the rest of the disk is used as swap space. With all the packages installed that we use in class, I’m using just under 4GB on my root partition, leaving a couple gig free.

To upgrade from Fedora Release 15 to 16, I installed and ran the preupgrade package. As part of its installation process, it checks to make sure that there’s enough free space available to do the upgrade. Of course, with all the crap I always seem to accumulate, there wasn’t, and it warned me that I needed to free up about half a gig of space. When I did that, the installation process continued, downloading all the upgraded packages (1130 of them!), then the system rebooted and began to install the new release. Unfortunately, during that installation, it again detected that there wasn’t enough space available, and reported I needed to free up another ~800MB of space! I just didn’t have enough available, so I had to stop the upgrade. Amazingly, I rebooted the VM, and I was back in Fedora 15 with no problems! That was a pleasant surprise, because I was worried that I’d be in a half-installed state, but that wasn’t the case.

It was an easy solution; using VirtualBox’s modifyhd command, I was able to increase the size of my virtual disk, increasing it to 12GB, and I created a new partition with that extra 4GB in it. Now, I needed to free up space on the root partition, and most of the space was being taken up in /var/cache/yum, where the upgrade was storing the updated packages. That was easy — I created a yum directory on my new partition, moved the contents of /var/cache/yum into there, then mounted the new partition on top of /var/cache/yum. That freed up enough space on the root partition to proceed.

At that point, I restarted “preupgrade”, and that quickly detected that I had already downloaded all of the updated packages, AND it saw that I now had enough disk space, so it automatically continued the upgrade. That ran without further input from me, so I wandered off to do something else, and when I came back, I had a nice Fedora 16 system running!

Screenshot of new Fedora 16 desktop

So, although I had a problem, I was very impressed by how well the upgrade process recovered from the problem and finished up the upgrade with little intervention from me. Good job, Fedora!

Replacing a forgotten root password in Fedora 15

At school, one of the classes I teach is the Linux class. In this class, we use Fedora 15. One of my students had forgotten the root password of his machine, and despite trying everything he could think of, he wasn’t able to guess what he had set the password to. He came to me looking for a way to reset the password.

The easiest way to do this is to boot into single-user mode. In single-user mode, you don’t need to log in; you’re automatically logged in to the root account. From there, you just need to run the “passwd” command, and you can set a new password for root.

It was a little more difficult than I expected. When Fedora 15 is installed, by default it installs the GRUB bootloader. If we have a choice of more than one operating system to boot into, the Fedora installation gives us a few seconds to select which one. During these few seconds, we can stop the boot process, then tell GRUB that we want to enter single-user mode (via a Linux kernel command-line argument), and away we go. However, if we only have a single OS on the computer, as was the case for my student, GRUB boots immediately into Linux, and we don’t have a chance to modify the kernel arguments. I knew there was some way to stop the boot, and after a little googling, I found the answer. I thought it was worth a blog post in case anyone else runs into this situation.

Short answer: hold down the shift key as the computer is booting, then GRUB will allow you to specify single user mode.

Here’s the longer answer.

Turn on the computer, and as the computer is booting, press and hold the shift key. If you time it right (if you press and hold too early, GRUB won’t recognize the keypress), you will see the following screen:

Initial GRUB screen

That’s GRUB. OK, we want to edit the kernel command-line arguments (you can find a complete list of them here), so at this point press the “a” key. You’ll go into a screen that allows you to edit the arguments:

Editing the kernel parameters

To enter single-user mode, you just need to append an “S” to the end of the command line, being sure to leave a space between the previous last argument and the S. See the screenshot below for what it should look like.

After adding the single-user kernel parameter

Once you’ve got that, just press the Enter key, and you’ll boot into single-user mode (also known as rescue mode — because it will rescue you from the forgotten root password!). You’ll be at the console, not the GUI, and your screen will look like this.

In single-user mode

Now, we can run the “passwd” command, and enter in the new password. The password won’t be displayed on the screen, but we’ll be asked to enter it twice, to make sure we didn’t make a typing mistake. After running the command, your screen will look like:

After changing the password

Once that’s done, you’ll be able to use your new password to log in to your root account. Now, just type CTRL-D, and the system will boot as normal, and away you go!