DEV Community

Cover image for Introduction to the Sam Text Editor
Matthew D. Miller
Matthew D. Miller

Posted on • Updated on • Originally published at

Introduction to the Sam Text Editor

Programmers are passionate about their tools, and if there is one tool they are especially passionate about, it is their text editor. An editor war has raged between Emacs and vi for decades, and new challengers such as VS Code and Sublime Text vie for superiority. I decided to try out an editor that isn't near as popular as these others but has no less a distinguished pedigree.

Sam was one of two text editors included in the Plan 9 operating system. It was originally designed by Rob Pike for the Blit windowing terminal in the early 1980s.

Sam for modern Linux and BSD can be obtained from two different sources. One is Plan 9 from User Space that ports Plan 9 programs to other Unix-like operating systems, and the other is Deadpixi Sam that has continued development of the 1990s Unix port of Sam. I installed and reviewed Deadpixi Sam. If you install a different version of Sam, some of the configuration details might differ, but most of the information here should apply to all versions of Sam.

A Brief Tutorial

Vi is notorious for trapping novice programmers who don't even know how to exit it. I plead guilty here: The first time I was dropped into a vi session on an HP-UX server, I couldn't get it to do anything and was too afraid to ask someone thinking I had broken something. The learning curve for Sam isn't quite as steep, but it also isn't obvious what to do when you first launch Sam and are presented with this:

Sam on first launch

You could, of course, read the man page, but the man page being a reference source isn't really laid out in a way that introduces you to Sam. It starts out by detailing a whole bunch of editing commands you can't even use yet, because it's not clear how to even load a text file to edit. You have to read through over half of the man page before you get to that. Despite Sam being around since the 1980s, I couldn't find any quick tutorial to get you up and running in a couple minutes to experiment with Sam, so I'll try to do that here.

When you first launch Sam, it is divided into top and bottom areas or "windows." The top window is the command window. This is where you type commands to manipulate text and control Sam. The bottom is a text window. You start out with one text window, but you can open as many text windows as you want. The text windows display the contents of the files being edited.

Deadpixi Sam can be configured with a .samrc file in your home directory. I recommend copying the example that comes packaged with Deadpixi Sam in doc/samrc to ~/.samrc, because it comes with some helpful defaults. To edit this file with Sam, you would load it by typing B .samrc in the command window followed by Enter. The cursor will change. Point at the text window. Select it to display the file by right clicking anywhere inside it.

Selecting a text window for current file

You can left click within the text window to move the text cursor around. You can left click and drag to select text or double click to select a word. If you double click just inside brackets, parentheses, quotes, etc., it will select all the text within the brackets. By default, you can navigate with the keyboard using Ctrl+e,x,d,s to move the text cursor up, down, right, and left. The reason I highly recommended copying over the samrc that comes with Deadpixi Sam is because it enables moving the text cursor with the arrow keys and the more familiar (at least to those coming from Vim) Ctrl+k,j,l,h. You can edit files directly in the text window.

Menu of commands to manipulate windows

Right click and hold (with most modern operating systems, we are used to right clicking and releasing to access context menus, but you have to keep the right mouse button held down to access the menu in Sam) to access a menu with commands to manipulate windows. Some of the commands give you a crosshairs cursor to select the size and position of windows. Below the window operators is a list of available files starting with ~~sam~~, the command window. Selecting a file from the list makes the most recently used window on that file current. If no windows are open on the file, you are prompted to open one.

Menu of editing commands

Middle click and hold (just like above but hold down on your mouse wheel) to access a menu with editing commands. With this menu you can do things like copy (called snarf in Sam for some reason), cut, and paste. If you copied the samrc over that comes with Deadpixi Sam, it binds the familiar Ctrl+x,c,v,q with cut, snarf, paste, and exchange.

Sam doesn't use the system clipboard, but you can swap Sam's snarf with the system with <exch> from the editing menu. This allows you to copy and paste into other apps and vice versa. By default, it swaps snarf with the system selection. In Linux, this is what pastes when you middle click. This didn't integrate with the clipboard manager I use, so I added a line to .samrc to tell Sam to use the system clipboard instead of the system selection.

a/\n# Make work with system clipboard manager\n/
a/snarfselection clipboard/
Enter fullscreen mode Exit fullscreen mode

The dollar sign tells Sam to jump to the end of the file. The a/text/ command appends text at the current location (there is also i to insert and c to replace). I did this to demonstrate some of the commands you can use in Sam, but for the above example, it probably would have been easier to type the above directly into the text window. You can also type regular expressions like /font/ into the command window to locate strings in the text window. Consult the man file that comes with Sam for a complete list of available commands.

Appending option to Sam configuration file

Now we're ready to save our changes and exit Sam. Type w in the command window to write the changes to the file. Then type q to quit. If you type q when there are unsaved changes, instead of closing, Sam will print ?changed files. To force it to close with unsaved changes, just type q a second time. If you copied the samrc over that comes with Deadpixi Sam, it binds the familiar Ctrl+s with write (and also Ctrl+z to undo).

Review of Sam

Sam does a lot of things different than the conventions used by most modern operating systems. For example, the scrollbar is on the left instead of the right. You can grab the scrollbar by left clicking and drag to scroll up and down, but moving your mouse up scrolls down, and moving your mouse down scrolls up. As mentioned in my review, you must hold down the right and middle mouse button to select from the menus (you can't just click and release). These would be fine if they followed the conventions of the operating system, but they will be the opposite of what most new users expect. This is because Sam is almost 40 years old and was created before many of these conventions were established.

It's obvious from projects like Plan 9 from User Space and posts on Hacker News and Reddit that Plan 9 still has a number of ardent followers. Sam is also reportedly the preferred editor of Ken Thompson, Bjarne Stroustrup, and Brian Kernighan. A project like Deadpixi has to tread a fine line between preserving Sam as it was to satisfy users who have built up muscle memory and modernizing it to attract new users. Copying the samrc over that comes with Deadpixi goes a long way toward configuring key bindings that make Sam mostly behave like other text entry in modern operating systems. What I still miss is being able to hold Shift while using the arrow keys to select text, and I don't think there's any way to do this just using .samrc. I made a couple additional tweaks to my .samrc during the couple of days I played around with Sam.

bind * Home command bol
bind * End command eol
Enter fullscreen mode Exit fullscreen mode

This makes the Home and End key behave as you would expect. The maintainer of Deadpixi sets 12 pt. Go Regular as the font in the example, samrc; I changed this to 13 pt. Inconsolata. The Deadpixi example samrc also adds a list of background colors for new windows, but I decided to stick with just white. I set tabs to 2 spaces and the aforementioned snarfselection to clipboard.

Sam was created before desktop environments and window managers were common. The original Sam was designed to run on a terminal (not a terminal emulator like you have on your computer but an actual terminal). Instead of using more modern metaphors such as tabs, Sam includes its own rudimentary window management. You can have multiple text windows open and make Sam look something like this (the screenshot from the Deadpixi REAMDE):

Sam with multiple text windows open

The only way to create and position windows is using the mouse to sweep an area for the window. This is awkward, and during the couple of days I experimented with Sam, I primarily used the right-click menu to switch between which file occupied the single text window that was created upon launch. Key bindings for window tiling or even Acme-like tiling is listed as a development goal in the Deadpixi README but not yet implemented.

The Sam GUI is built with Xt, a library that provides primitives to create widgets for the X Window System used by widget toolkits like Motif (whereas GTK and Qt draw their own widgets). This gives it a decidedly dated look and feel. It's telling the the screenshots in Rob Pike's original 1987 paper describing Sam look exactly like Sam does today. Removing the Xt dependency is listed as a stretch goal in the Deadpixi README, but continued development seems slow. At the time of this writing, it has been over a year since the last commit.

It also doesn't support other features most user's probably expect from a modern text editor. It doesn't do syntax highlighting, and that is listed under "Things That Won't Ever Happen (Sorry)" in the README. In a GitHub issue, the maintainer clarifies it's not that he'll never implement syntax highlighting but just that it would be almost impossible in the current 30-year-old codebase. If he rewrites the GUI in Tcl to get rid of the Xt dependency, syntax highlighting might become a possibility.

While I enjoyed playing around with Sam for a couple of days, I don't see it becoming my main text editor. It was a nice peek at computing history, and I might keep it around to edit a configuration file now and again, but I'll continue using Geany when programming. The structural regular expressions and being able to pipe the current selection to a shell command and replace the selection with the result are neat but not compelling enough to do without things like syntax highlighting (plus most modern text editors can do those other things too).

Are you a Sam fan? Do you use it as your daily driver? I'd love to hear from you. How long have you used it? What is it about Sam that makes you prefer it to other text editors? Is there something I missed? Any tips or tricks to share?

Top comments (0)