Wikipedia defines digraphs (and trigraphs) as

sequences of two and three characters respectively, appearing in source code, which a programming language specification requires an implementation of that language to treat as if they were one other character.

— Wikipedia
Digraphs and trigraphs

If you’ve spent much time in Unix, you have likely seen their character representations on a rare occasion. Usually they begin with a ^ followed by some key code. Note though that I said "spent much time in Unix though. This is because Linux doesn’t usually (with some exceptions) have problems with digraphs. When I say Unix though, I am referring to the really old ones that claim to be up-to-date like AIX, Solaris, and HPUX.

What do digraphs have to do with old Unix?

Digraphs are actually used every time you use a Unix/Linux box from the command line. There’s this realy nifty thing called stty that flies under the radar most if not all of the time on newer systems. I don’t know of a single Linux distro that doesn’t set stty for you. The reason it flies under the radar so often is because it’s something that’s been standardized for so long that it is all but set in stone (as far as I know). It’s also super handy to have set, and super infuriating to not have set.

What is stty?

Well, technically STTY is an acronym for "Set TTY". That’s tons of help though. What’s TTY? It turns out that TTY is an acronym for TeleTYpewriter. Combining all that goodness, we have Set TeleTYpewriter.

Now, all this is great, but really, what does this have to do with anything? It turns out that while we nearly never need to directly deal with it, we actually use it all the time. Here’s a short list of a few things we use it for in *nix…​

  • Backspace

  • Scrolling with a mouse in a terminal

  • Ctrl+C (sigterm)

  • Ctrl+D (logout/eof)

  • All arrow keys, both horizontal and vertical

I mentioned earlier that stty is set by default on nearly all modern Linux and Unix distributions with the exception of old Unix distributions such as AIX, Solaris, and HPUX. I posed this question to a few AIX admins I know and all of them told me that IBM doesn’t set stty for you by default because it’s more customizable than Linux, therefore better. I have my own very charged opinion as to why they don’t set a default, but I will leave that out of this post.

What does stty look like?

Where I work, management is endeavoring to make their Linux environment as much like AIX as possible. One step in that process is to merge the .profile configurations. Since Linux doesn’t have stty set in .profile because the system has a default, AIX using a Linux .profile doesn’t support the afforementioned list of modern keyboard keys (backspace? really? no). Imagine how infuriating command line can get without arrow keys for cursor movement, a backspace to correct your mistakes, and Ctrl+C to clear your line or stop your process. The only option we have here is to re-set the Linux stty so when the profile is sent over to an AIX system, it also has stty set on login. Here’s my attempt at porting my Arch Linux stty to aix.

stty erase ^? kill ^U intr ^C eof ^D quit ^\ start ^Q stop ^S susp ^Z rprnt ^R werase ^W lnext ^V flush ^O time 0 -parenb -parodd cs8 -hupcl -cstopb cread -clocal -ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff -iuclc -ixany -imaxbel -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0

What does all that do?

I really only want to cover a few things in that list because they are the most frequently used and caused me trouble when I was trying to set this up.

Each of those items up there starting with a \^ (Circumflex Accent) represents a control key combination. For instance, eof \^D will send the logout signal upon pressing Ctrl+D. The problem here is that those "circumflex accents" aren’t caret characters. A circumflex accent is its own character. How do we do these in vi/vim? You need another control key combination to tell vi/vim that you are going to be pressing a control key combination of course!

To do, for instance, the Ctrl+D sequence in vim, go into insert mode and type Ctrl+v Ctrl+d (the d is not capitalized) and you should see \^d show up.

I did have two problems with this method though: \^S and \^Q. It turns out that those aren’t Ctrl+S and Ctrl+Q. Since I didn’t know those, I elected to use the actual digraph instead of the character version to set them. To do this, go into insert mode again and hit Ctrl\+k and type the digraph. In the case of \^Q and \^S, these are D1 and D3, respectively.

Category:Linux Category:Vim Category:Unix