Installing VirtualBox Guest Additions in a Debian Vagrant box on Windows 10

Short answer: Use the vagrant-vbguest plugin to make things easy!

This is the second post in a series of three that describe how I set up my Debian development environment in Vagrant on Windows 10, using VirtualBox as the provider.  The first post shows how to set up a basic, command-line only, Debian box. The third describes how to install a GUI. In this post, I describe how to install the VirtualBox Guest Additions.

If you know what you’re doing, and you want to skip all the explanations and just see the steps involved, feel free to look at my summary!

Note: this blog post was written with Debian 9 Stretch, but I’ve tested it with 10 Buster, and everything works the same.  The screenshots will vary slightly, but everything works good!

What does the VirtualBox Guest Additions offer?

According to the VirtualBox User Manual, installing the Guest Additions provides the following advantages (read the User Manual for more details):

  • Mouse pointer integration: makes it easier to use the mouse between the virtual machine and your Windows host)
  • Shared folders: makes accessing files between your VM and your Windows host easier
  • Better video support: not a big deal if you’re doing command-line only work, but if you’re using a GUI, it makes a huge difference.  For me, I like to resize the GUI, and when I do that, the screen in Debian automatically resizes. Very nice.
  • Seamless windows: I’ve never actually used this.  Read the manual for what it is (as I should do!).
  • Generic host/guest communication channels: Something else I’ve never used….
  • Time synchronization: synchronizes the time between your VM and the Windows host.  If you ever pause your VM, this is really nice because it automatically updates the time in the VM when you unpause.  Not something you’ll ever notice when you’re using it, but you will notice if you don’t have this feature!
  • Shared clipboard: Easy cut-and-paste between your VM and the Windows host.
  • Automated logons (credentials passing): Never used this, so I can’t really comment.

What’s involved in installing the VirtualBox Guest Additions?

You’re probably convinced now that having the VirtualBox Guest Additions installed is a good thing.  Unfortunately, getting them installed is normally a bit of a pain. They are distributed within VirtualBox as an ISO file, which you can mount as a CD-ROM on your VM.  Then, you run the install script, and everything happens nice (hopefully): installing new device drivers, adding kernel modules, etc., etc. But, the problem I had, is that I need to have a bunch of software installed in my VM before I can install the Guest Additions.  I suppose that’s not that hard, I just need to figure out what I need first. I could even record the list of required software in a blog post!

Another problem is that every time you upgrade your version of VirtualBox, or the kernel in the virtual machine, you need to re-install the Guest Additions.  If you don’t, things likely still will work, but it’s better to be safe than sorry. VirtualBox will warn you if you’re running a different version of the Guest Additions, but you’ll need to update them yourself.

But, there’s a better way!

Using the vagrant-vbguest plugin

Vagrant allows you to extend its functionality using plugins.  There is a plugin, called vagrant-vbguest, that does all of the hard work for you involved in setting up the Guest Additions.  Once you’ve got it installed, every time you run vagrant, it will check to see if the Guest Additions are installed, and at the correct version; if not, it will automatically install them (or upgrade them) for you.  It will even automatically install the software required to do the installation!

I ALWAYS use the vagrant-vbguest plugin.  It’s possible to use plugins on a per-VM basis (I’ve read, anyways, but I’ve never tried it), but it makes things so much simpler, why would you not want to do it?

Installing the vagrant-vbguest plugin

Installing the vagrant-vbguest plugin is VERY easy.  Just execute:

vagrant plugin install vagrant-vbguest

This will go out to the Internet, download the plugin, and install it in Vagrant.


And that’s all there is to it!

Installing the VirtualBox Guest Additions with the vagrant-vbguest plugin

The wonderful thing about this plugin is, once it’s installed, you don’t have to do anything else!  Every time you start up a Vagrant box with “vagrant up”, the plugin will ensure that the Guest Additions are installed; if they’re not, it will install it for you.  Here we go:

vagrant up

And here’s some screenshots of the plugin in action.  First, the VM boots as usual:


Then, Vagrant checks to see if the Guest Additions are installed:


Next, it checks to see if the packages required to install the Guest Additions are installed in the VM; if they’re not, it installs them:


I’m not going to bother showing the installation of these 34 packages.  After they are installed, Vagrant mounts the Guest Additions ISO from VirtualBox into our VM:


Then, it actually installs the Guest Additions:


And finally, it starts up the Guest Additions, unmounts the ISO, and finishes the “vagrant up”:


Because we’ve made changes to the kernel, I always like to reboot the system after installing the Guest Additions.  Maybe it’s not necessary, but I prefer to be safe. Just shut it down and restart:

vagrant halt
vagrant up

You’ll now see, as part of the bootup sequence, that Vagrant checks to ensure that the Guest Additions are installed and running:

Setting up shared folders in your VM

Now that we’ve got the Guest Additions installed in the VM, let’s set up shared folders, so that we can easily access our Windows files in our VM, and vice-versa.  What we’ll do is set up a single folder that will be shared between the VM and the Windows host. First, shut down the VM:

vagrant halt

Now, use your favourite text editor to edit the “Vagrantfile” configuration file.  Within that file, you’ll find some lines like:

We’re going to add a new line after this to specify how we want to set up our shared folder.  What I’m going to do is have the current folder (the one with Vagrantfile in it) show up in my VM in the location /vagrant.  To do this, add the following line after the lines above:

config.vm.synced_folder ".", "/vagrant", type: "virtualbox"

This tells Vagrant to mount the current folder (“.”) on the Windows host into the VM at location “/vagrant”, and to use a VirtualBox shared folder.  There is also an “rsync” way of sharing folders, but that requires installing additional software on both Windows and the VM, and it doesn’t automatically sync the files.  I much prefer the VirtualBox shared folders. Anyways, that section of my Vagrantfile now looks like:

Start Vagrant back up:

vagrant up

You’ll notice in the bootup sequence a message indicating the shared folder:

Using the shared folders

Let’s actually try out the shared folders.  First, ssh into the VM:

vagrant ssh

If, from within the VM, you look at the contents of the /vagrant directory, you’ll see:


which is the same as what you see in File Explorer in Windows (not counting the hidden .vagrant folder!):


If you look at the contents of the Vagrantfile in the VM, you’ll see that it is the same as the file on Windows — they are the same file!

Next, try creating a file in the VM:


And you’ll see it’s also visible in Windows:


So, if you ever want to move files between the VM and the Windows host, just drop them in the appropriate folder, and you’re good to go!

Cleaning up

If you want to remove the plugin from Vagrant, it’s pretty easy to do:

vagrant plugin uninstall vagrant-vbguest


Note that this only uninstalls the plugin, it does not uninstall the VirtualBox Guest Additions.  If you do want to uninstall the Guest Additions, I’d suggest destroying the box, and recreating:

vagrant destroy
vagrant up

I personally prefer having the Guest Additions installed, so I always have the plugin installed.

What’s next?

In the first post, I looked at how you can set up a basic Debian box in Vagrant.  But, it’s command-line only. That’s OK for an old Unix hacker like me, but if you prefer a GUI, the next post talks about how to do that.

Setting up a Debian Vagrant box on Windows 10 with VirtualBox

File was modified as follows: converted from SVG to JPG
Debian-OpenLogo” by Ebrahim is licensed under CC BY-SA 3.0

Update on July 23, 2019: Since the time I originally made this post, Debian has moved from version 9.6 Stretch to 10.0 Buster.  I’ve updated the post accordingly.  Thankfully, nothing changed but the name of the Vagrant box to use!  If you still want to use Stretch, replace everywhere that it says stretch64 with buster64.  Some of your error messages may be different, though…

Lately, for my Linux development, I’ve been using Vagrant, using VirtualBox as a provider.  In Vagrant, I set up a Debian box and do my development on that. It’s nice and easy to set up, and if I want to restart with a clean installation of Debian, it’s very easy to do.

I’ll write three posts about how I set it up.  In this first post, I detail the steps that I use to create a basic Debian box, with no GUI.  In my second post, I describe the steps I use to set up VirtualBox’s Guest Additions, so that I can share files between my host Windows system and my Debian Vagrant box.  In the third post, I give the steps that I use to set up a GUI.

If you know what you’re doing, and you want to skip all the explanations and just see the steps involved, feel free to look at my summary!

Debian’s Vagrant Box

The Debian project provides standard Vagrant boxes in the Vagrant Cloud box catalog.  You can read about them on the Debian wiki, and look at the provided boxes on https://app.vagrantup.com/debian/.  The latest version of Debian is version 10, codenamed Buster.  At the time of writing this post, the latest point release was 10.0, and I’m using the 64-bit version of Debian.  So, the Vagrant box I’m using is debian/buster64.

The Debian Vagrant box provides a standard basic Debian system, containing all packages with priority required, important, and standard.  Note that it does NOT include a GUI; you’re just going to get a command-line version. We’ll look at how to set up a GUI in the third post.

Prerequisites

To set this up, you need to have both VirtualBox and Vagrant installed.  You can download VirtualBox from the website https://www.virtualbox.org.  The download page is https://www.virtualbox.org/wiki/Downloads, and the installation instructions are in Chapter 2 of the User Manual. You can download Vagrant from the website https://www.vagrantup.com, where the download page is at https://www.vagrantup.com/downloads.html, and the installation instructions are at https://www.vagrantup.com/intro/getting-started/install.html.  I installed VirtualBox first, then Vagrant.  I don’t know if the order matters, but it made sense to me, and I know that works!

Creating the Vagrantfile

Once you’ve got Vagrant and VirtualBox installed, you’re going to want to create the configuration file for your box.  This file is called Vagrantfile. I’ve created a folder under my home directory called vagrant-VMs, and underneath that, another folder called buster.  I’ve created multiple folders under vagrant-VMs, one for each Vagrant configuration, but in this post, I’m only discussing my Buster installation.

Here’s looking at my folder in File Explorer:

Open a command prompt, then change to the vagrant-VMs\buster folder:

cd vagrant-VMs\buster

Next, use Vagrant to create the Vagrantfile for us:

vagrant init debian/buster64

This will create the Vagrantfile that configures the usage of the Vagrant box.  The box won’t actually be downloaded until you try to bring the box up for the first time.

Here’s a screenshot.

You might want to take a look at the Vagrantfile; it’s just a text file (actually, a Ruby program file), so you can look at it in your favourite text editor, such as Notepad, Notepad++, or Vim.  There’s actually very little in the file; most of it is commented-out configuration, giving examples of things you may want to configure. But, for now, we’ll just leave things as they are.

Here’s a screenshot of the complete file (click to view it larger):

Creating the Virtual Machine

The next step is to create and start up the virtual machine.  We’ll use the “vagrant up” command to do that.  The first time you run this, it will connect to the Hashicorp atlas (a repository of Vagrant boxes, maintained by the developers of Vagrant) and download a copy of the box (called the “base box”).  Then, it will create a virtual machine as a copy of the downloaded base box; subsequent executions will just use the virtual machine you’ve already created.

Note: Vagrant creates a folder to hold all its configuration data under your home directory, with the name .vagrant.d.  If you’re curious where the downloaded base box goes, it goes underneath that folder, in a subfolder named boxes. On Windows, I use File History to back up my files; because these boxes are so big, and because I can easily re-download them, I don’t want to back them up, so I configure File History to not backup the .vagrant.d\boxes folder.  There’s another folder that I also don’t back up, but I’ll mention that next…

Second note: the virtual machine gets created in a folder in your home directory called “VirtualBox VMs”.  Once again, I don’t want to back these up, so I add this folder to my list of excluded folders in File History.  You may want to back them up, but as I said before, you can easily recreate these VMs, so it’s a waste of backup space to back them up.

OK, so let’s create our Virtual Machine:

vagrant up

You’ll see a whole bunch of messages.  Vagrant goes through the following steps when it creates the VM:

  • Imports the debian/buster64 base box and uses it to create a new VM
  • Configures the networking of the new VM
  • Boots the VM, and waits for the booting to complete
  • Uses SSH to connect to the VM, and replaces the SSH key in the VM (more on that later)
  • Checks to see if the VirtualBox guest additions are installed.

You’ll now see one of two messages about the VirtualBox guest additions.  If they are not installed in the Vagrant box (they shouldn’t be, but version 10.0.0 of the box seems to include them), you’ll get a scary error message telling you that you should install them, and that a lot of things won’t work until you install them.  Ignore that for now; I’ll look at installing the guest additions in my next post.  If they are installed (version 10.0.0, I’m looking at you!), you may get a slightly less scary message that the guest additions on the VM don’t match the installed version of VirtualBox.  Don’t worry about that, as we’ll install the proper version in the next post.

At the time of writing, I also get a VERY scary error message about attempting to rsync a shared folder.  Once again, don’t worry about that; we’ll resolve that in the next post as well.

With regards to SSH: the buster64 box that was downloaded from the Hashicorp atlas has a well-known SSH key, but you really want that to be secret.  But, if it was secret, we wouldn’t be able to connect to the VM. So, when we first bring up the VM, Vagrant connects to the VM using the well-known key, then generates a secret key, and replaces the well-known key with our new secret key.  Finally, it disconnects and reconnects using the new key, just to make sure everything is working OK.

Here’s a screenshot of all the output (click to enlarge):

You’ll notice that it doesn’t appear that the VM is running.  The “vagrant up” command completes and returns us to the command prompt, and there isn’t a VirtualBox GUI.  But, don’t worry, VirtualBox is running; you just can’t see it! If you really want, you can start up Windows’ Task Manager, and see the VBoxHeadless process listed under Background Processes.  Or, you can start up VirtualBox, and you’ll see that you’ve got a running VM!

Bringing Debian Up-to-date

There may be security patches issued by Debian that aren’t incorporated into your imported box.  The next thing we’re going to do is to bring all the software up-to-date.

Log in to the VM using the “vagrant ssh” command:

vagrant ssh

This will connect to the VM using SSH, and will log you in to an account named “vagrant”.  This account was present in the Vagrant box you downloaded; it’s a standard convention used by Vagrant boxes.  If you ever need to log in without using the “vagrant ssh” command, the password is also “vagrant”.

Here’s a screenshot:

Once you’re logged in, you’ll be at the Debian command prompt.  We’ll want to execute the following commands:

sudo apt-get update
sudo apt-get upgrade

The first command will connect to the Debian package repository, and download a list of all of the latest versions of Debian packages.  The second command will download and install any packages that have been updated in the repository since the Vagrant base box was created.

Here’s a screenshot of executing the update:

and another of executing the upgrade (click to enlarge).  Note that your packages being updated almost certainly will be different from mine.

Depending on which packages have been updated, there are two other possible commands you may need to execute.  If you get a message about some packages being held back, execute this command:

sudo apt-get dist-upgrade

And, if you get a message that some packages are no longer needed, you can remove them with:

sudo apt-get autoremove

Actually, there’s really no harm in executing both of these commands anyways, since we’re setting up a new VM; they just might not do anything.

And now, we’re done!  We’ve got an up-to-date Debian system running.

Log out of the SSH session to return to the Windows command prompt:

logout

Shutting down the Virtual Machine

The last thing to do is to shut down the virtual machine.  That’s very easy to do from the Windows command prompt. Execute:

vagrant halt

And you’ll get a message that Vagrant is gracefully shutting down the virtual machine.

Next time you want to start the VM back up, just go to your vagrant-VMs\buster folder (or whatever you called it), and re-execute “vagrant up”.  It won’t need to recreate the VM; it’ll just use it as it was the last time you used it.

Do you want to clean up?

For completeness, I should mention how you can clean up what you’ve done.

If you want to delete the VM that you created, execute:

vagrant destroy

That will delete the virtual machine.  You can easily recreate the VM, though, just be executing “vagrant up” again.  This is a nice easy way to start your VM over fresh if you mess it up somehow. It leaves your Vagrantfile intact, so any configuration changes you made in there will be used the next time you recreate the virtual machine.  But, any changes you made inside the VM (such as updating Debian!) will need to be redone.

If you also want to remove the base box, execute:

vagrant box remove debian/buster64

If you want to use it again, you’ll need to re-download it; executing “vagrant up” will automatically do that for you if you need it.

Next steps

It’s very useful to have the VirtualBox guest additions installed on your system; I detail how to do that in my next post.  This will allow you to share files between your VM and your Windows machine that the VM is running on. Also, if you’re running a GUI (which I’ll describe how to set up in my third post), it allows you to resize the window, which is very handy!

My Mom’s Jumbo Raisin Cookies recipe

Last weekend was the first Mother’s Day since my mother passed away.  Unrelated to that, I’ve had a craving for her raisin cookies.  I found her recipe, and just made them.  They aren’t as good as hers were, but that’s probably because I’m not a good baker!  I thought I’d share the recipe.

Jumbo Raisin Cookies

  • 2 cups raisins
  • 1 cup water

Boil 5 minutes and set aside to cool.

Cream:

  • 1 cup shortening
  • 2 cups sugar
  • 3 eggs
  • 1 teaspoon vanilla

Add cooled raisins (use juice and all)

Sift together:

  • 4 cups all-purpose flour
  • 1 teaspoon baking powder
  • 1 teaspoon soda
  • 2 teaspoons salt
  • 1 1/2 teaspoons cinnamon
  • 1/4 teaspoon nutmeg
  • 1/4 teaspoon allspice

Add to raisin mixture.  Add 1 cup chopped nuts if desired (she never did this, and neither do I!).  Mix well and drop by teaspoons full. Bake at 375° F. for 12 to 15 minutes.  Makes 5 dozen cookies.

A few assorted fortune cookies

There used to be a good Chinese food restaurant near my work, and my co-workers and I would go there for lunch on Fridays.  The restaurant is still there, but the ownership has changed and the quality has gone way down since.  So, instead we’ll often go to the Spring Roll Restaurant.  Excellent food and good prices (I recommend the Special # 1, which unfortunately doesn’t show up on their online menu), but that’s not the point of this posting!

The old place used to give fortune cookies after the meal, and I collected a number of them.  I was cleaning up my office last week, and decided to throw out the fortunes, but I wanted to record them.  Here they are, in no specific order:

  • Focus in on the color yellow tomorrow for good luck!
  • You will run into an old friend soon.
  • Tomorrow your friend or partner will tell you some exciting news.
  • The next full moon brings an enchanting evening.
  • Happiness surrounds your events this week.
  • Others admire your assertiveness.
  • You have a great appreciation for beautiful things.
  • Get ready to do something daring.
  • Enough is as good as a feast.
  • You will obtain your goal if you maintain your course.
  • You will receive a big compliment in front of others.
  • A job well begun is half done.
  • It’s the journey and not the destination that counts.
  • You constantly struggle for self improvement — and it shows.
  • A simple kindness today will bring you great reward.

Here’s a fun (and a little spicy) game — add “in bed” to the end of any fortune you get.  It puts them in a whole different light!

 

Installing Google Fonts on Windows 10

A quick how-to guide on how to install Google Fonts into Windows 10.

Google Fonts is a collection of fonts intended for use on the Web.  From the Google Fonts About page:

Google Fonts makes it quick and easy for everyone to use web fonts, including professional designers and developers. We believe that everyone should be able to bring quality typography to their web pages and applications.

Our goal is to create a directory of web fonts for the world to use. Our API service makes it easy to add Google Fonts to a website in seconds. The service runs on Google’s servers which are fast, reliable and tested. Google provides this service free of charge.

I was trying to make a graphic that used Ultra, one of the fonts, and I wanted to use Inkspace to make it.  But, Google Fonts are web fonts, and I wanted to have Ultra as a font installed on my computer so I could use it.  It turns out that it’s not that hard to do.  I’ll show the steps for installing Open Sans.

First, go to the Google Fonts homepage.  In the upper left corner is a search box:

The Google Fonts search box
The Google Fonts search box

Type in the name of the font you’re looking for, in this case, Open Sans.  The Google Fonts website will filter down all of the fonts it knows about, and only show the ones matching what you’ve typed in.  For me, it showed me two fonts: Open Sans and Open Sans condensed.

GoogleFonts2

With each of the fonts displayed, there’s a blue “Add to Collection” button.  Click that button, and that font will be added to a collection of fonts, displayed at the bottom of the page.

GoogleFonts3

Next, we’re going to want to download a Zip file that contains a TTF file (short for True Type font, one of the types of fonts that Windows understands) of the font we’ve selected.  If we add more than one font to our collection, all the fonts will be in the Zip file.  Anyways, there’s a download button on the page, but it’s not real obvious.  It’s in the upper right corner of the page, and looks like a down arrow:

GoogleFonts4

Click that, and you’ll get a dialog box telling you that you don’t need to download the font.  If you’re just using it for the web, that’s right, but we want to install the font into Windows 10.

GoogleFonts5

We want to choose the first option, “Download the font families in your Collection as a .zip file”.  Click on the .zip file link, and your browser will download a zip file to your computer — in my case, it was called Open_Sans.zip.  Go to wherever that file was downloaded (likely your Downloads folder), right-click on the file, and select “Extract All…”.  You’ll be asked where to extract the files to, and it will default to the current directory.  Click the Extract button, and a new folder will be created containing a LICENSE.txt file and one or more TTF files.

The next thing to do is to install the TTF file (or files) into Windows 10.  Open up the Control Panel (type Control Panel into the Windows search box if you’re not sure how to open it), and you’ll get this window:

GoogleFonts6

Click on the Appearance and Personalization heading to get:

GoogleFonts7

Under Fonts, click on “Preview, delete, or show and hide fonts”.  You’ll be taken to a window showing all the fonts installed on your system:

GoogleFonts8

Finally, from that folder of downloaded fonts, drag the fonts you want to install into this new window, and the fonts should now be installed!

What font is used for the 7 symbol on a slot machine?

Short answer: Clarendon Bold.

For fun, I’m writing a little slot machine game in Java.  I wanted to create some symbols to go on the reels, and one of those symbols is the number 7.  And, I wanted it to look like the fancy 7 you see on slot machines, like this:

Slot machine with 7 symbol
Spin baby” by Hyun Lee is licensed under CC BY 2.0

So, after some googling around, I discovered that it uses the Clarendon Bold font.  Here’s what the 7 looks like in Linotype’s Clarendon Std Bold:

The letter 7 in Monotype's Clarendon Bold BT font.
The letter 7 in Linotype’s Clarendon Std Bold font

This is a screenshot grabbed from the preview feature of fonts.com’s page for Clarendon Std Bold.

Unfortunately, I don’t have that font on my computer.  After further googling, I discovered the Ultra font on Google Fonts.  The Google Fonts page describes it as follows:

Ultra is an ultra bold slab typeface with nods to wood type styles like Clarendon and Egyptian. Strong and dramatic letterforms for titling, a serious, yet friendly, and easily legible typestyle. Perfect for power headlines and titling for impact.

Here’s what the 7 looks like in Ultra:

The symbol 7 in Ultra font
The letter 7 in Ultra font

To make my slot machine 7, I installed Ultra on Windows, fired up Inkscape, added a Text object with the Ultra font at 72 points, set a fill colour of Red, added a black stroke 1 point thick, and here’s the final result:

A "7" slot machine symbol
My final “7” slot machine symbol

I’m pleased with it!

 

Blogging 101: Who I Am and Why I’m Here

siteiconIt’s been a long time since I’ve added anything to my blog — almost three years, now that I look back at my last post.  I’ve been meaning to add more entries to my blog, but I just haven’t gotten around to it.  I think the main problem is because I really don’t know what the focus of my blog is, or what I want to blog about.  So, to help me get over this hurdle, I decided to sign up for WordPress’ Blogging 101 course.

The first assignment in this course is to “Introduce Yourself to the World”.  The intention is to blog about who we are and why we’re here.  Maybe if I do this, it will help me to gain a little clarity on why I’m keeping a blog.

Well, here goes…

My name is Shane McDonald.  I live in Saskatoon, Saskatchewan, Canada.  I work as an instructor at Saskatchewan Polytechnic, in the Computer Systems Technology program.  I mostly teach computer programming courses, with a smattering of Word / Excel / PowerPoint / etc. introductory classes thrown in the mix.  Tomorrow is the first day of a new semester; I’ll be teaching a class in Java, and a class in C.

Why am I blogging?  Well, thinking back to when I started this blog, I had a number of reasons:

  • I wanted to get some experience with WordPress.
  • I wanted to practice writing, in order to help to express my thoughts more clearly.
  • I wanted a place to record solutions to computer problems that I’ve run across, and I thought other people might also run across.  I know a quick Google search often helps me to find the solution to problems, but if nobody had bothered to record their solution, it wouldn’t help me out very much.

Thinking about it now, I think that third reason is probably the reason that seems most important to me.  I know I get a good feeling when I see people accessing my posts, and I hope that others are able to solve the same problems I had, but without a lot of searching!  My top post seems to be this one about how to use numbers in formulas in Microsoft Word mail merges; my least popular posts seem to be my series of tips on using the Vi text editor, which was an outgrowth of a set of tips that I had created on a corporate Wiki at a previous employer.

My problem seems to be that I either don’t run across too many problems (yeah, I wish), or that I don’t bother to take the time to record my solutions for posterity (more likely!).

Anyways, by taking this blogging course, I’m hoping to gain a little more focus on my blog, and to get some motivation to write posts.  Who knows, I might even completely change the focus of my blog!  If nothing else, at least I’ve created this one post, and people will see that I still exist!  I’m excited to work through this course and see how things come along!!!

Vi Tip of the Week: Moving a word at a time

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

If you want to move around in a document quickly with vi, there are 3 useful commands (or 6, depending on how you look at it) for moving word-by-word.

Pressing the w key will move you forward word-by-word.  For example, if you’ve got the line:

This is a test of moving word-by-word.

and your cursor is positioned on any character in “This”, you can press w and the cursor will be moved to the start of the next word, “is”.  Pressing w again will move the cursor again, this time to “a”.

If you want to move backwards word-by-word, you can use the b key.  For example, if the cursor is positioned at the start of “moving”, pressing b will move the cursor to the start of “of”.  Pressing b again will move the cursor to “test”, and so on.

If you want to move forward to the end of a word, you can press the e key.  With the cursor placed on the start of “This”, pressing e will move the cursor to the “s” at the end of “This”. If the cursor is already positioned at the end of a word, pressing e will move you to the end of the next word.  For example, with the cursor positioned on the “s” in “This”, pressing e will move the cursor to the end of “is”.

But I mentioned that there are really 6 commands.  Try the following line of text:

Let’s eat, Grandma.

(A classic example of the importance of punctuation!)  If the cursor is positioned on the “L” in “Let’s”, pressing w will move the cursor to the apostrophe.  Pressing it again will move the cursor to the “s”, and pressing it three more times will move you to the “e” in “eat”, the comma, and the “G” in “Grandma”.  So, as you can see, the w command considers punctuation to be separate words.

If you press Shift-W instead, vi ignores punctuation.  With the cursor on the “L” of “Let’s”, pressing Shift-W moves the cursor to the “e” in “eat”, and pressing it again will move the cursor to the “G” in “Grandma”.

Similarly, Shift-B moves the cursor backwards a word, ignoring punctuation.  With the cursor on the “a” at the end of “Grandma”, pressing Shift-B once will move the cursor to the beginning of “Grandma”.  If you were to press b now, the cursor would be positioned on the comma, but if you press Shift-B, the cursor is positioned on the “e” at the start of “eat”.

Finally, Shift-E works similarly with punctuation.  With the cursor on the “L” in “Let’s”, pressing e moves the cursor to the “t” in “Let’s”, but pressing Shift-E moves the cursor to the “s”.

Summary:

  • w moves the cursor forward a word at a time, and stops at punctuation
  • Shift-W moves the cursor forward a word at a time, ignoring punctuation
  • b moves the cursor backward a word at a time, and stops at punctuation
  • Shift-B moves the cursor backward a word at a time, ignoring punctuation
  • e moves the cursor to the end of the word, or to the next word if it’s already at the end of the word, and stops at punctuation
  • Shift-E moves the cursor to the end of the word, or to the next word if it’s already at the end of the word, ignoring punctuation.

Vi Tip of the Week: Searching

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

If you want to search for something in a file, you can use the forward slash character: /. Type forward-slash, the text that you want to search for, then press the Return key.  Vi will take you to the next place in the document where that text appears, and will place the cursor on the first character.  So, if I wanted to search for my name, Shane, I would type:

/Shane

and press Return.  If the text doesn’t appear in the file, I’ll get a message on the status line telling me that (the exact contents of the message depend on which version of vi you are running).

If you want to search for the same text again, you can just type the forward slash followed by Return — vi will remember what the last text you searched for, and will search for it again.  But, if you type anything after the slash, it will search for that instead.

Another thing you can do to search for the same text is to press the n key — it will take you to the next occurrence of that text!

/ searches forward; if you want to search backwards, use the question mark instead.  If I want to search for the previous occurrence of my name, I type

?Shane

And if I want to search for the same text again, it works just like the forward slash — type ? followed by Return, and it remembers what I searched for last, and searches backwards for that again.

OK, here’s some magic — if I press n, it searches in the same direction as my last search! If I had been searching forward, then press n to search for the same text, it searches forward.  But, if I had been searching backward with ?, pressing n will now search backward for the same text!

The search string can also include some simple regular expressions.  If I want to search for my name at the start of a line of text, I use ^:

/^Shane

Or, if I want to search for it at the end of a line of text, I use $:

/Shane$

Finally, if I want to go back to the line I was on just before my previous search, I can press the backquote twice: “.  This will take me to my previous position.

Have fun with searching!!!

Vi Tip of the Week: Scrolling up and down in a file

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

When someone wants to move up or down in a file that they are editing, I often notice that they either use the arrow keys to move one line at a time, or the Page Up and Page Down keys to move a screen at a time.  Although this works fine, it has the disadvantage that you have to move your hands off the home keys, which messes up your touch typing.  Fortunately, vi offers commands for doing the same thing:

  • Ctrl-F works like Page Down: it moves you down a page in the document, leaving the last two lines on the previous page as the top two lines of the next page to help retain context.  You can remember Ctrl-F as “move Forward”.
  • Ctrl-B works like Page Up: it moves you up a page in the document, leaving the top two lines on the previous page as the bottom two lines of the next page to help retain context.  The mnemonic for this command is “move Backward”.

But maybe you don’t want to move a full page at a time — maybe the common 2 lines isn’t enough context.  You can also scroll up and down half a page at a time:

  • Ctrl-D moves you down half a page.  The previous bottom line is now displayed in the middle of your screen.  You can remember this with “move Down”.
  • Ctrl-U moves you up half a page.  The previous top line is now displayed in the middle of your screen.   Remember this one with “move Up”.

Finally, maybe you just want to move the display one line up or down:

  • Ctrl-E moves the display up one line, but, unlike the previous commands, it doesn’t move your cursor — your cursor stays on the same line it was on before executing Ctrl-E.  Another way of thinking of this command is that it exposes a new line at the bottom of the screen, hence the mnemonic “Expose a line”.
  • Ctrl-Y is the opposite of Ctrl-E: it moves the display down one line (exposes a new line at the top of the screen), and leaves your cursor in the current position.  Sadly, I’ve got no easy memory trick for remembering this one — if you’ve got one, add it in the comments below!