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.

Installing “PortableGit from msysgit” on Windows 7

I wanted to install Aptana Studio 3 onto my Windows 7 machine at work, and according to Aptana’s Getting Started Guide, I needed to have Git installed.  According to the Aptana website, the Aptana Studio installer should install “PortableGit from msysgit” for me if I want, but that didn’t seem to happen.  So, I did a little searching for some instructions on installing PortableGit, but when I couldn’t really find anything helpful, I came up with a set of instructions on my own.

PortableGit is an implementation of the Git version control system for Windows machines.  It uses the very handy MSYS system to provide a Bourne Shell (bash) command line interpreter system. There’s at least a couple different versions of Git for Windows available from these msysgit guys; I like the PortableGit version because it doesn’t mess with my registry, it doesn’t install anything in my Start menu, and if I want to delete it, I only need to delete the files, not run an uninstall program.  It’s very self-contained, and I don’t have to worry about it mucking about with my computer. This isn’t the most user-friendly version, but if you’re an old command-line hack like myself, it’s fine.  And, for what I’m going to be using it for (letting Aptana Studio use it!), it’s all right.

I grabbed PortableGit from http://code.google.com/p/msysgit/downloads/list (see image below — click the image to see a readable full-size screenshot!).

Listing of all of the Git for Windows files available

I selected the file called PortableGit-1.7.11-preview20120710.7z.  This was the latest version at the time I downloaded it; of course, if you’re reading this anytime after the day I post it, there may be a newer version!  Clicking on that link takes you to this page (again, click the image to see the full-size screenshot):

I clicked on the little arrow on the left side of the filename, and it downloaded the file for me.

Although this is an executable installer, the file can’t be executed as-is – the file extension needs to be renamed from .7z to .exe.  Once I did that, I was able to run it.

Note that, depending on your Windows setup, you may need to run the installer as an administrator.  For me, I had to find the file in Windows Explorer (I saved it to my Downloads library), then right-click on the file, and select “Run as administrator”.  When I did that, I was greeted with the following dialog box:

That location was exactly where I wanted to install it, so I left it at that.  Click OK, and the program is installed.  If you get an “access denied” error, it means that you need administrator permission AND you didn’t run it as an administrator, so try again.

Once you’ve got the program installed, if you want to learn more about the “Portable” aspect of PortableGit, navigate to the C:\Program Files (x86)\Git folder.  There’s a README.portable file that explains everything.

Once we’ve got PortableGit installed, we’re going to want to be able to run it.  The program we’ll be running is called git-bash.bat, and that will give you a Bash shell that you can run Git commands in.  To be able to run that program, we’ll need to add Git to my list of executable folders (my “path”) so that I can run it from the command prompt.  To do so, do the following.  Open the Start menu, then right-click on Computer:

From the pop-up menu that comes up, select Properties.

You’ll get a window that is titled “View basic information about your computer”.  On the left-hand side of the window, there’s a menu titled “Control Panel Home”; select “Advanced system settings”.

You’ll get a System Properties window.  Select the Advanced tab, and then click the Environment Variables… button:

You will get a list of all the environment variables in the system, similar to this:

Right now, I have no Path user variable, but I do have a Path system variable (if I had been smart, I would have scrolled down to show it in my screenshot!).  The Path variable tells Windows which folders contain programs to execute from the command prompt or the “Search programs and files” box on the Start menu.

You now have a choice: if you want to add Git to the path for all users, you’ll want to add Git’s folder in the Path environment variable in the “System variables” section (and you’ll need to be an Administrator to do it); if you only want to add it for yourself, you’ll need to add it to (or create) the Path environment variable in the “User variables” section.  I’ll just add it for myself.  If you have a Path variable, select the Path variable and then click the Edit… button; if you don’t have one (which I didn’t), just click the New… button.

If you’re adding to an existing variable, you’ll get a window that is similar to this:

To the end of the existing value, type a semi-colon (“;”) followed by the complete path to the folder that you installed git in (in my case, C:\Program Files (x86)\Git).  The semi-colon is used to separate all the folders on your path.

If you’re creating a new variable, you’ll get an almost identical window that looks like this:

For the Variable name, type in Path.  For the Variable value, type in the complete path to the folder that you installed git in (in my case, C:\Program Files (x86)\Git):

Press OK, and you should be good!  You can now finish up by pressing OK on the Environment Variables window, OK on the System Properties window, and then close the “View basic information about your computer” window.

Next, let’s make sure it’s installed.  We’ll want to run the git-bash.bat file from the command line.  Click on the Start menu, then in the “Search programs and files” box in the bottom left corner, type git-bash and press the Enter key:

Your Start menu will go blank, and there will be a “Searching” message displayed, but soon you’ll see the git-bash command prompt:

If you see that, you’ve successfully installed PortableGit!

If you want to learn HOW to use git, I recommend the Git site: http://git-scm.com/.  I’d especially read the Pro Git book on that site.

Formatting percentages in Microsoft Word mail merges

When I mark student assignments, I usually put the students’ marks and comments in a spreadsheet, then I do a mail merge to send out the marks to the students.  I like doing it this way because then I’ve got a copy of all the comments and marks that I’ve given the students, and I can do some simple calculations to see how effective my teaching has been in different areas of the course material.  I’m also more able to be sure that I’m giving similar comments and marks for similar work.

The only problem I have, though, is that when I do a mail merge, the formatting of my calculated numbers is thrown off, and the worst is the percentages.  I know that there’s a way to format them correctly, but I can never remember how to do it, and the notes I’ve kept don’t seem to always work, and I’ve never quite got my head wrapped around the way it works. Until now!!!

I ran across this web page that really helped me to understand what’s going on.  While it’s still fresh in my mind, I wanted to write a blog post explaining it so that the next time I need to do this, I’ve got something to reference, and hopefully it will be helpful for other people, too!

Note that I’m using Microsoft Office 2010, although that webpage is for Office 2003, so I’m sure it works with other versions.

Update, Feb. 25, 2014: In the comments below, “Anna” mentioned that she’s verified that this also works in Office 2013.

Let’s start off with a simple spreadsheet.  I’ll assume I’ve got an assignment with 3 questions on it, each worth 10 marks.  I’ll record the marks for the students in that spreadsheet, then have a column that calculates the total marks, and another that calculates the percentage.  The spreadsheet looks like this:

Then, I set up a mail merge document in Word, add in the fields from the spreadsheet, and get a document that looks like this:

So that looks OK, but when I look at it in preview mode, I’ve lost all of the formatting on my percentage field:

The problem is that when I do a mail merge, the number is transferred from Excel to Word, but the formatting is not. The solution is to tell Word how I want to format this percentage field. To do that, I need to mess around with “field codes”. A better explanation is probably given here, but here’s a step-by-step description of how I do this.

First, we need to see the field codes. Press ALT-F9 to display the field codes (pressing ALT-F9 again will undo this). You’ll see:

We need to specify that we want to format this field differently. Place the insertion point after the “MERGEFIELD Pct”, and type in:

\# #0.0

The “\#” tells Word that it should format this field as a number, and the “#0.0” tells it which specific format it should use for the number. I find it a little confusing because the octothorpe character is being used for multiple things: both to say that this field should be formatted as a number, and to say how that number is to be formatted. Anyways, “#0.0” tells Word to use up to two numbers before the decimal point and to include a single zero if the integer part of the number is zero, and to use exactly one number after the decimal point, even if that number is 0. As I understand it, “#” means display the digit if you need it or a space if you don’t (when you’ve got a leading or trailing zero), and “0” means always display the digit.  Anyways, our field now looks like:

Let’s look at how this gets formatted. Press ALT-F9, then turn off and turn back on the preview results. Now we see:

Well, that’s close, but it’s not quite right, obviously. The problem is that the number is 0.8, not 80 (a decimal number, not a percentage). To get it to display it as a percentage, we need to tell Word to multiply that value by 100. To do this, first we need to highlight the merge field. Highlight “MERGEFIELD” and the name of the field, as shown:

Once you’ve done that, press CTRL-F9 to embed this mergefield into another field. Now we’ve got:

Notice the extra set of curly braces around the mergefield. It is extremely important that the curly braces be added by pressing CTRL-F9; if you type in the braces manually, it’s not going to work.

Once you’ve got this merge field embedded in another field, we can tell Word to do a calculation with it. Add an equals sign in front of the merge field, and “*100” after it; this tells it to multiply the value of that field by 100, making it a percentage rather than a decimal. It should look like:

Now, press ALT-F9 to turn off the display of the field codes, turn the merge preview results off then back on, and then we should see what we want!

I hope this helps!!!

Restaurant review — Flying Piggy’s Bistro in Ottawa

I’m in Ottawa on vacation with my son. After a fun day exploring the Canadian War Museum (which we are really enjoying — we need 2 days to take it all in), we were looking for some place to eat supper. Matthew told me about the UrbanSpoon app, so we loaded that on my iPad, and started looking for a place to eat.

We decided to try a pasta place called Flying Piggy’s Bistro, as the reviews sounded quite positive (91% positive!). It was kind of out of the way from where we’re staying, but I’m glad we tracked it down! I liked it so much, I signed up for an UrbanSpoon account and wrote the following review.

Matthew enjoying the Flighty Boar

Our main courses were really good, but the soup we had was AMAZING! It was a coconut curry carrot, lamb, and squash soup, and it was fantastic!!! After that, I was really looking forward to the main course. Although it was really good, it didn’t measure up to the soup. I had the fettuccini ragu alla bolognaise, and Sonny-boy had the “flighty boar.” I have had better pasta, but I have absolutely no complaints about the meal.

Overall, I liked the atmosphere of the place — it was a Wednesday night that we went, and it was pretty quiet. Our waiter had a good sense of humour, but wasn’t overbearing. The meal came quick, and was filling. We were both full (and my “little boy” is 6 feet, 235 pounds), so we didn’t have dessert, but the orange triple sec cheesecake sure sounded good!

I thought the prices were a little high, but I’d recommend this place.

We’ve all got a Point of View Gun

I watched the movie “The Hitchhiker’s Guide to the Galaxy” last night. It’s been a long time since I read the book (mid 80s, I think!), so I’m not too sure how closely the movie follows the book. One thing I don’t remember from the book is the Point of View Gun. You can watch the scene where it shows up on YouTube.

Wikipedia probably has the best description of the Point of View gun:

When used on someone, it will cause them to see things from the point of view of the person firing the gun (the Guide says that it “conveniently, does precisely as its name suggests”). According to the Guide, though the gun was designed by Deep Thought, it was commissioned by the Intergalactic Consortium of Angry Housewives, who were tired of ending every argument with their husbands with the phrase: “You just don’t get it, do you?”

In a moment of serendipity, I was reading a presentation from a guy named Matt Webb, I think it’s about design, and he gives a really cool quote in there:

when people start learning something new, they perceive the world around them differently. If you start learning how to play the guitar, suddenly the guitar stands out in all the music you listen to. […] as more and more people have access to things like iMovie, they begin to understand the manipulative power of editing. Watching reality TV almost becomes like a game as you try to second-guess how the editor is trying to manipulate you.

The quote comes from this site, but that seems kind of rambling to me, so just stick with the quote above!

Anyways, I thought that was kind of cool, AND after seeing the Point of View gun in the movie last night, it got me thinking — learn something new, and you’ll see things from a different point of view. Kind of cool, and I wish I was deep enough that I could make that seem cosmic, but I’ll leave that for someone more philosophical than me.

Anyways, I highly recommend reading through that presentation, and even reading the blog post that took me there, talking about the 100 hour challenge.

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!

Moving Time Machine backups to a new disk on a Mac

On my Mac, I’ve got a 1 TB external USB drive to hold my Time Machine backups, and I’ve got a 360 GB drive to hold my media files. Unfortunately, I’ve ripped too many of my DVDs, and now my media drive is full. This past weekend, I saw that Best Buy had a 2.5 TB USB drive for $99! (Not like this post will look dated in a year or so! 🙂 ) So, needing more drive space, I decided to pick one up.

OK, now I was faced with a difficult choice: do I use my new drive to hold my media and choose not to back up those files, or do I move my backups to the new drive, move my media files to the 1 TB drive, and take my 360 GB drive off-line? Well, I’d really be uncomfortable not having backups of my songs and videos, so I decided on the latter. I’ve got a Garibaldi board just dying to use that small drive for a new installation of MIPS Debian Linux, so my plan was set.

I wasn’t quite sure how to set up a new drive for Time Machine and maintain all my existing backups (you never know when I’ll need a file from 2008 — now you know why my basement is as full of “archives” as it is!), so I did a quick google search and found the answer on Apple’s support site. Unfortunately, it didn’t quite work for me, but I was able to find other information to help me to resolve my problem, so I thought I’d document it in a blog post in case anyone else runs into this problem.

The Apple knowledge base article, “Mac 101: Time Machine”, had the information I was looking for. About three-quarters of the way down the page is a section entitled “Mac OS X v10.6: How to transfer your backups from your current hard drive to a new hard drive”. That sounds exactly what I wanted, and it almost worked for me, but just not quite.

Here’s what the page had to say:

If you upgrade to a new hard drive with a larger capacity, you may want to transfer your existing Time Machine backups to it before using it for regular backups. To keep your existing backups on your new, larger backup hard drive, follow these steps:

  1. Connect your new hard drive.
  2. In Disk Utility, make sure it has a GUID partition and is formatted as Mac OS Extended (Journaled). If needed, reformat the drive as Mac OS Extended with a GUID partition. If you do not know how, see the steps in this article. Note: Back up any data on the hard drive before you format it.
  3. Select the new drive’s icon on the desktop and choose Get Info from the File menu.
  4. Make sure “Ignore ownership on this volume” is not enabled.
  5. Open Time Machine preferences in System Preferences.
  6. Slide the Time Machine switch to Off.
  7. In Finder, double-click the current backup hard drive to open its Finder window.
  8. Drag the folder “Backups.backupd” to the new hard drive.
  9. Enter an administrator user name and password, then click OK to start the copying process. This may take some time to complete because all your backups will be copied.
  10. In Time Machine System Preferences click “Select Disk…”
  11. Select your new hard drive, then click “Use for Backup.”

Now, on your new hard drive, you will have all of your existing backups from the previous backup hard drive plus have more room for more new backups.

As I worked through the steps, my first problem came at step number 9. Because my original backup disk was formatted as “Mac OS Extended (Case-sensitive, Journaled)”, and because step 2 instructed me to create a non-case-sensitive partition, I got an error. That was easy to fix — just go back to step 2 and create a case-sensitive partition.

My next problem also occurred in Step 9. After dragging “Backups.backupd” from the old drive to the new one, it proceeded to start copying. It took a long, long time — 1 TB takes a long time to transfer! I left it overnight, and when I checked on it the next morning, the Mac Finder reported an error with error code -8003, and copied some but not all of the files. I googled for that error code, but wasn’t able to find anything. I was (temporarily) stumped. Back to Google! I needed to find some way to copy that directory from the old to the new disk.

After some searching, I found this page: Apple Tips: Using Disk Utility 7. Copying a Volume via the Restore tab. This is a really good page, and tells how to copy an entire volume from one disk to another. This is EXACTLY what I needed. I’ll leave the details to that page (screenshots and all!), but basically, it describes how to use OS X’s Disk Utility’s Restore function to copy the contents of one drive to another.

Following those instructions, I was able to copy my old backups over to my new disk, turn off my old backup drive, and start using my new drive, with no problems! If you’re going to do something similar, just replace Steps 7 to 9 on Apple’s page with the instructions on the Apple Tips page. For the curious, it took about 25 hours (!) to copy over that terabyte of data.

Next, of course, I’ll be moving my media files from my 360 GB drive over to my 1 TB drive (the old backup drive). Again, this will be easily accomplished using the Disk Utility. Thanks, James Pond, for your Apple Tip!