How to type smart quotes on Ubuntu Linux

Note: I don’t know why, but all of the images for this article have been lost. I’ll replace them when I have some free time (but free time is scarce these days).

There seem to be a few different ways to type “smart quotes” on Ubuntu Linux, including using keys (keystrokes) like AltGr and Compose. In this tutorial I’ll document an approach that works best for me: creating macros I can assign to simple keystrokes rather than having to use more-complicated keystrokes.

Note: This article was written in January, 2017, and is about Ubuntu 16.04. I use an old Mac/iMac keyboard, but the solution should work with other keyboards.

Background: Switching from MacOS to Ubuntu

As a bit of background, I’m used to typing smart quotes on Mac systems — i.e., characters like “” and ‘’ — and when I switched from MacOS to Ubuntu this was one of the first things I wanted to learn how to do on Ubuntu.

Background: AltGr and Compose keys

There are a lot of articles on how to type smart quotes on Ubuntu, and they almost all mention these two terms:

  • The AltGr key
  • The Compose key

Just coming to a Linux/Ubuntu desktop, I didn’t know what these keys were. As I learned, they are special keys/keystrokes on Ubuntu Linux systems that let you type extended characters on your keyboard. For example, the “” characters aren’t on normal U.S. keyboards, so even on Macs you need to type some keystroke combination to create those characters.

Here are two things I can tell you about the AltGr and Compose keys:

  • The AltGr key seems to be an older approach, as its documentation refers you to the Compose key
  • The Compose key seems to be the preferred approach

Given that background, here’s how I type smart quotes on my Ubuntu system today.

Step 1: Figure out what the Compose key is

The first part of the solution is to figure out what the heck a Compose key is.

In retrospect the easiest way to explain it is with a series of images.

What Ubuntu thinks your keyboard looks like

First, if you open System Settings, then go to Text Entry, then click the little keyboard icon on the left side of the screen, you’ll see a window that looks something like this:

Ubuntu System Settings, Keyboard simulator

(If that image is too small you can right-click it, then select View Image to see a larger image.)

The tricky thing here is that the Compose key shown in this image doesn’t exist on my iMac keyboard. I have three keys to the right of the spacebar, and they look like this:

[command] [option] [control]

So that threw me for a loop for a little while.

One important thing to note at this time is that if you press keys on your keyboard while you’re looking at this window, you will see them light up on this window. Actually, I should say that you’ll see many of them light up.

Figuring out the Compose key

The next key is to go to System Settings, then Text Entry, then tap the “Keyboard Settings...” link. When you do that, you’ll see something like this:

Ubuntu System Settings, Text Entry, Keyboard Settings

If your window doesn’t look like that, be sure to click the Shortcuts tab, then click the “Typing” item in the list on the left.

I don’t remember what my initial Compose Key setting was — it may have been Disabled — but the important thing to know is that you can change it from this window. To do so, click in the area where my image shows “Right Alt.” When you do that, you’ll see that Ubuntu gives you a choice of what keys you can use as a Compose key. To be consistent with this tutorial, choose Right Alt for now; you can always change it to something else later.

Key point: That’s how you set the Compose key on Ubuntu.

Verifying the Compose key works

Now you can verify that your Compose key works. To do so, go back to the “keyboard simulator” window that I showed originally:

Ubuntu System Settings, Keyboard simulator

(To do this, go to System Settings, then Text Entry, then click the little keyboard icon on the left side of the screen.)

When this window comes up, tap the [Alt] key on the right side of your keyboard. When you do this you should see the Compose key on the keyboard simulator window light up. If you see this — great, you’re close to being able to type smart quotes on your Ubuntu system.

(If you don’t see the Compose key light up, you’re probably going to need to choose a new “Input source to use” on the Text Entry screen. But alas, I don’t know much about which setting is right for your keyboard, so I can’t help much there.)

How to type smart quotes on Ubuntu (the hard way)

With your Compose key set up you should be able to type smart quotes now. One way to do this is with Ubuntu’s built-in text editor (GEdit).

If you open that, you should then be able to type keystrokes like this to create smart quotes:

“  -  type [Compose][Shift][,] then "
”  -  type [Compose][Shift][.] then "
‘  -  type [Compose][Shift][,] then '
’  -  type [Compose][Shift][.] then '

Technically those are the keys you need to type, but it may be easier to remember them if I write them like this:

“  -  type [Compose][<] then "
”  -  type [Compose][>] then "
‘  -  type [Compose][<] then '
’  -  type [Compose][>] then '

If nothing else, the use of the left- and right-arrows in this approach makes sense.

Hopefully that works on your system. If it’s not clear, you can increase the font size in GEdit to be sure that these keystrokes are creating smart quotes.

This is where I got to on Day 1, but when I went to bed that night I knew those long keystrokes wouldn’t make me happy, so I resolved to come up with a better solution.

Note: You can stop here

If you’re okay with long keystroke combinations you can stop here, but because I can never remember things like this I kept pushing on to create something better.

Using Ubuntu macros to make it easier

My final solution uses Ubuntu macros (custom shortcuts) to type smart quotes. With macros I can press one key (or maybe two keys at most) to type smart quotes. Here’s how I set this up.

The first step is to install an application/utility named xdotool. You install it like this:

sudo apt-get install xdotool

Once it’s installed you can test it at the command line in your terminal by typing something like this:

xdotool type "ls"

If you see ls echo’d to the command prompt in your terminal window that’s good news; it means that xdotool is working. (You might want to press [Ctrl][c] now to cancel that command.)

Now all you need to do is go to Ubuntu’s system settings and map some custom keystrokes to simple xdotool commands.

Mapping keystrokes to xdotool commands

To do this, go to System Settings, then Text Entry, then click “Keyboard Settings...” Then under Shortcuts click “Custom Shortcuts.” I mapped the smart quotes to some keys I never use on my keypad, and in this window those settings look like this:

Ubuntu System Settings, Custom Shortcuts

Because I never use those keys on my iMac keyboard, I wrote on them like this to make my life easier:

iMac keypad keys

As one example of how this works, when I click on “Quote-Left” on my system, I see this dialog:

Ubuntu custom shortcut

The “Name” on this dialog is any name that’s meaningful to you. The Command is much more important; this is the xdotool command that will be run when you press the key that is assigned to this command.

For you to create your own macros, here are the steps you need to follow. I’ll show how to create the macro I call “Quote-Left”:

  1. Click the + symbol on this window.
  2. For Name, type “Quote-Left”
  3. For Command, type xdotool type '“'
  4. Click Apply
  5. The right column for this entry should now show Disabled. Click that word, and you will see a prompt that says “New Accelerator.” Type whatever keystroke you want to use. This keystroke will create an opening double-quote whenever you type it.

That last step can be a little tricky. You more or less need to know what other keystrokes are used in Ubuntu and all of your applications. I’m lucky that I have that numeric keypad over there on the right, so I mapped mine to those keys. But before I thought to use those keys, these are the keystrokes I used:

“ - [command][']
” - [command][shift][']
‘ - [command][;]
’ - [command][shift][;]

I’m sure there are better key combinations you can use than those, but in my case I stopped thinking about it once I realized that I could use my keypad keys.

The xdotool commands

For the record, here are the correct xdotool commands you’ll need for each of the single- and double-quote macros:

Left double quote:

xdotool type '“'

Right double quote:

xdotool type '”'

Left single quote:

xdotool type '‘'

Right single quote:

xdotool type '’'

(I share those here so you can copy and paste them.)

Summary

In summary, if you want to map smart quotes to custom keystrokes on your Ubuntu system, I hope this has been helpful. There may be better ways to do this, but at the moment I’m pretty happy with my final solution.

As you can imagine, this solution works not only for smart quotes, but any other extended key combination that you want to create a macro for. For instance, if you’re into functional programming and want to be able to type the Lambda character -- λ -- you can just map that character to another custom keystroke.

Also, the first part of this solution -- the Compose key -- is still valid, which is why I showed it. By learning more about the Compose key you’ll eventually be able to type any keyboard character you want (although the keystrokes you’ll need may be a little long and hard to remember).