SAM(1plan9)
โจ NAME โจ
sam, B, E, sam.save, samterm, samsave โ screen editor with structural regular expressions ๐
๐ธ SYNOPSIS ๐ธ
sam [ option ... ] [ files ]
sam -r machine
sam.save
B file[:line] ...
E file
๐ฆ DESCRIPTION ๐ฆ
okay so sam is a multi-file editor. It modifies a local copy of an external file. The copy is here called a file. The files are listed in a menu available through mouse button 3 or the n command. Each file has an associated name, usually the name of the external file from which it was read, and a modified bit that indicates whether the editor's file agrees with the external file. The external file is not read into the editor's file until it first becomes the current fileโthat to which editing commands applyโwhereupon its menu entry is printed. The options are
-aโ Autoindent. In this mode, when a newline character is typed in the terminal interface,samtermcopies leading white space on the current line to the new line.-dโ Do notdownloadthe terminal part ofsam. Editing will be done with the command language only, as ined(1).-r machineโ Run the host part remotely on the specified machine, the terminal part locally.-s pathโ Start the host part from the specified file on the remote host. Only meaningful with the-roption.-t pathโ Start the terminal part from the specified file. Useful for debugging.
๐ซ Regular expressions ๐ซ
Regular expressions are as in regexp(7) with the addition of \n to represent newlines. A regular expression may never contain a literal newline character. The empty regular expression stands for the last complete expression encountered. A regular expression in sam matches the longest leftmost substring formally matched by the expression. Searching in the reverse direction is equivalent to searching backwards with the catenation operations reversed in the expression.
๐ Addresses ๐
An address identifies a substring in a file. In the following, character n means the null string after the n-th character in the file, with 1 the first character in the file. Line n means the n-th match, starting at the beginning of the file, of the regular expression All files always have a current substring, called dot, that is the default address.
๐ Simple Addresses ๐
#nโ The empty string after character n;#0is the beginning of the file.nโ Line n;0is the beginning of the file./regexp/?regexp?โ The substring that matches the regular expression, found by looking toward the end (/) or beginning (?) of the file, and if necessary continuing the search from the other end to the starting point of the search. The matched substring may straddle the starting point. When entering a pattern containing a literal question mark for a backward search, the question mark should be specified as a member of a class.0โ The string before the first full line. This is not necessarily the null string; see+and-below.$โ The null string at the end of the file..โ Dot.'โ The mark in the file (see thekcommand below)."regexp"โ Preceding a simple address (default.), refers to the address evaluated in the unique file whose menu line matches the regular expression.
๐ Compound Addresses ๐
In the following, a1 and a2 are addresses.
a1+a2โ The addressa2evaluated starting at the end ofa1.a1-a2โ The addressa2evaluated looking in the reverse direction starting at the beginning ofa1.a1,a2โ The substring from the beginning ofa1to the end ofa2. Ifa1is missing,0is substituted. Ifa2is missing,$is substituted.a1;a2โ Likea1,a2, but witha2evaluated at the end of, and dot set to,a1.
The operators + and - are high precedence, while , and ; are low precedence.
In both + and - forms, if a2 is a line or character address with a missing number, the number defaults to 1. If a1 is missing, is substituted. If both a1 and a2 are present and distinguishable, + may be elided. a2 may be a regular expression; if it is delimited by the effect of the + or - is reversed.
It is an error for a compound address to represent a malformed substring. Some useful idioms: a1+- (a1-+) selects the line containing the end (beginning) of a1. 0/regexp/ locates the first match of the expression in the file. (The form 0;// sets dot unnecessarily.) ./regexp/// finds the second following occurrence of the expression, and .,/regexp/ extends dot.
๐ฅ Commands ๐ฅ
In the following, text demarcated by slashes represents text delimited by any printable character except alphanumerics. Any number of trailing delimiters may be elided, with multiple elisions then representing null strings, but the first delimiter must always be present. In any delimited text, newline may not appear literally; \n may be typed for newline; and \/ quotes the delimiter, here Backslash is otherwise interpreted literally, except in s commands.
Most commands may be prefixed by an address to indicate their range of operation. Those that may not are marked with a below. If a command takes an address and none is supplied, dot is used. The sole exception is the w command, which defaults to 0,$. In the description, range is used to represent whatever address is supplied. Many commands set the value of dot as a side effect. If so, it is always set to the result of the change: the empty string for a deletion, the new text for an insertion, etc. (but see the s and e commands).
โจ Text commands โจ
a/text/
or
a
lines of text
.
Insert the text into the file after the range. Set dot.
ciโ Same asa, butcreplaces the text, whileiinserts before the range.dโ Delete the text in the range. Set dot.s/regexp/text/โ Substitutetextfor the first match to the regular expression in the range. Set dot to the modified range. Intextthe character&stands for the string that matched the expression. Backslash behaves as usual unless followed by a digit:\dstands for the string that matched the subexpression begun by the d-th left parenthesis. Ifsis followed immediately by a number n, as ins2/x/y/, the n-th match in the range is substituted. If the command is followed by ag, as ins/x/y/g, all matches in the range are substituted.m a1t a1โ Move (m) or copy (t) the range to aftera1. Set dot.
๐ Display commands ๐
pโ Print the text in the range. Set dot.=โ Print the line address and character address of the range.=#โ Print just the character address of the range.
๐ File commands ๐
* b file-listโ Set the current file to the first file named in the list thatsamalso has in its menu. The list may be expressed<Plan 9 commandin which case the file names are taken as words (in the shell sense) generated by the Plan 9 command.* B file-listโ Same asb, except that file names not in the menu are entered there, and all file names in the list are examined.* nโ Print a menu of files. The format is:'or blank indicating the file is modified or clean,-or+indicating the file is unread or has been read (in the terminal,*means more than one window is open),.or blank indicating the current file, a blank, and the file name.* D file-listโ Delete the named files from the menu. If no files are named, the current file is deleted. It is an error toDa modified file, but a subsequentDwill delete such a file.
๐ I/O Commands ๐
* e filenameโ Replace the file by the contents of the named external file. Set dot to the beginning of the file.r filenameโ Replace the text in the range by the contents of the named external file. Set dot.w filenameโ Write the range (default0,$) to the named external file.* f filenameโ Set the file name and print the resulting menu entry.
If the file name is absent from any of these, the current file name is used. e always sets the file name; r and w do so if the file has no name.
< Plan 9-commandโ Replace the range by the standard output of the Plan 9 command.> Plan 9-commandโ Send the range to the standard input of the Plan 9 command.| Plan 9-commandโ Send the range to the standard input, and replace it by the standard output, of the Plan 9 command.* ! Plan 9-commandโ Run the Plan 9 command.* cd directoryโ Change working directory. If no directory is specified,$homeis used.
In any of <, >, | or !, if the Plan 9 command is omitted the last Plan 9 command (of any type) is substituted. If sam is downloaded (using the mouse and raster display, i.e. not using option -d), ! sets standard input to /dev/null, and otherwise unassigned output (stdout for ! and >, stderr for all) is placed in /tmp/sam.err and the first few lines are printed.
๐ฆ Loops and Conditionals ๐ฆ
x/regexp/ commandโ For each match of the regular expression in the range, run the command with dot set to the match. Set dot to the last match. If the regular expression and its slashes are omitted, is assumed. Null string matches potentially occur before every character of the range and at the end of the range.y/regexp/ commandโ Likex, but run the command for each substring that lies before, between, or after the matches that would be generated byx. There is no default regular expression. Null substrings potentially occur before every character in the range.* X/regexp/ commandโ For each file whose menu entry matches the regular expression, make that the current file and run the command. If the expression is omitted, the command is run in every file.* Y/regexp/ commandโ Same asX, but for files that do not match the regular expression, and the expression is required.g/regexp/ commandv/regexp/ commandโ If the range contains (g) or does not contain (v) a match for the expression, set dot to the range and run the command.
These may be nested arbitrarily deeply, but only one instance of either X or Y may appear in a single command. An empty command in an x or y defaults to p; an empty command in X or Y defaults to f. g and v do not have defaults.
๐ Miscellany ๐
kโ Set the current file's mark to the range. Does not set dot.qโ Quit. It's an error to quit with modified files, but a secondqwill succeed. So like... it double-checks you're sure ๐un โ Undo the last n (default1) top-level commands that changed the contents or name of the current file, plus any other file whose most recent change was simultaneous with the current file's change. Successiveu's move further back in time โช. The only commands whereuis powerless arecd,u,q,wandD(sorry bestie, those are permanent). If n is negative,u"redoes," undoing the undo, going forwards in time again ๐ฎ.- (empty) โ If the range is explicit, set dot to the range. If sam is downloaded, the resulting dot gets selected on the screen; otherwise it's printed. If no address is specified (the command is just a newline), dot is extended in either direction to line boundaries and printed. And if dot is thereby unchanged, it gets set to
.+1and printed. Yes queen, keep up ๐ซถ
๐ Grouping and multiple changes ๐
Commands may be grouped by enclosing them in braces {}. Commands within the braces must appear on separate lines (no backslashes required between commands, thank u). Semantically, an opening brace is like a command: it takes an (optional) address and sets dot for each sub-command. Commands within the braces are executed sequentially, but changes made by one command are not visible to other commands (see the next paragraph). Braces may be nested arbitrarily, go off ๐ง
When a command makes a number of changes to a file, as in x/re/c/text/, the addresses of all changes to the file are computed in the original file. If the changes are in sequence, they are applied to the file. Successive insertions at the same address are catenated into a single insertion composed of the several insertions in the order applied. Iconic behaviour honestly โจ
๐ช The terminal ๐ช
What follows refers to behavior of sam when downloaded, that is, when operating as a display editor on a raster display. This is the default behavior; invoking sam with the -d (no download) option gives you access to the command language only.
Each file may have zero or more windows open. Each window is equivalent and is updated simultaneously with changes in other windows on the same file. Each window has an independent value of dot, indicated by a highlighted substring on the display. Dot may be in a region not within the window. There's usually a "current window," marked with a dark border, to which typed text and editing commands apply. Text may be typed and edited as in rio(1); also the escape key (ESC) selects (sets dot to) text typed since the last mouse button hit ๐
The button 3 menu controls window operations. The top of the menu provides the following operators, each of which uses one or more rio-like cursors to prompt for selection of a window or sweeping of a rectangle. "Sweeping" a null rectangle gets a large window, disjoint from the command window or the whole screen, depending on where the null rectangle is ๐ฑ๏ธ
newโ Create a new, empty file.zeroxโ Create a copy of an existing window.resizeโ As inrio.closeโ Delete the window. In the last window of a file,closeis equivalent to aDfor the file.writeโ Equivalent to awfor the file.
Below these 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, unless it's already current, in which case selections cycle through the open windows. If no windows are open on the file, the user is prompted to open one. Files other than sam are marked with one of the characters -+* according as zero, one, or more windows are open on the file. A further mark appears on the file in the current window, and a single quote, ', appears on a file modified since last write. Little status icons, we love to see it ๐
The command window, created automatically when sam starts, is an ordinary window except that text typed to it is interpreted as commands for the editor rather than passive text, and text printed by editor commands appears in it. The behavior is like rio, with an "output point" that separates commands being typed from previous output. Commands typed in the command window apply to the current open fileโthe file in the most recently current window. Main character energy fr fr ๐
โ๏ธ Manipulating text โ๏ธ
Button 1 changes selection, much like rio. Pointing to a non-current window with button 1 makes it current; within the current window, button 1 selects text, thus setting dot. Double-clicking selects text to the boundaries of words, lines, quoted strings or bracketed strings, depending on the text at the click. So intuitive, no notes ๐
Button 2 provides a menu of editing commands:
cutโ Delete dot and save the deleted text in the snarf buffer.pasteโ Replace the text in dot by the contents of the snarf buffer.snarfโ Save the text in dot in the snarf buffer.plumbโ Send the text in the selection as a plumb message. If the selection is empty, the white-space-delimited block of text is sent as a plumb message with aclickattribute defining where the selection lies (seeplumb(7)).lookโ Search forward for the next occurrence of the literal text in dot. If dot is the null string, the text in the snarf buffer is used. The snarf buffer is unaffected.<rio>โ Exchange snarf buffers withrio./regexpโ Search forward for the next match of the last regular expression typed in a command. (Not in command window.)sendโ Send the text in dot, or the snarf buffer if dot is the null string, as if it were typed to the command window. Saves the sent text in the snarf buffer. (Command window only.)
๐ External communication ๐
Sam listens to the edit plumb port. If plumbing is not active, on invocation sam creates a named pipe /srv/sam.user which acts as an additional source of commands. Characters written to the named pipe are treated as if they had been typed in the command window. Slay ๐
B is a shell-level command that causes an instance of sam running on the same terminal to load the named files. B uses either plumbing or the named pipe, whichever service is available. If plumbing is not enabled, the option allows a line number to be specified for the initial position to display in the last named file (plumbing provides a more general mechanism for this ability).
E is a shell-level command that can be used as $EDITOR in a Unix environment. It runs B on file and then does not exit until file is changed, which is taken as a signal that file is done being edited. iykyk ๐
๐ญ Abnormal termination ๐ญ
If sam terminates other than by a q command (by hangup, deleting its window, etc.), modified files are saved in an executable file, $HOME/sam.save. This program, when executed, asks whether to write each file back to a external file. The answer causes writing; anything else skips the file.
Bestie. If sam goes down the hard way, your unsaved stuff is not gone โ it's chilling in $HOME/sam.save. Run it, answer the prompts, and it'll write each file back out. An answer = write it, literally anything else = skip it. ๐
๐ FILES ๐
$HOME/sam.save$HOME/sam.err/bin/samsaveโ the program called to unpack$HOME/sam.save.
๐๏ธ SOURCE ๐๏ธ
/src/cmd/samโ source for sam itself/src/cmd/samtermโ source for the separate terminal part/bin/B/bin/E
๐ SEE ALSO ๐
ed(1), sed(1), grep(1), rio(1), regexp(7).
Rob Pike, ``The text editor sam''.
And that's a wrap on the SAM(1plan9) page, bestie โ this whole thing lives in the Plan 9 manual, section 1, and it's stamped with its own little SAM(1plan9) footer, date and all. Go edit something iconic ๐
โจ
๐ฆ where this page comes from
- debian bookworm: 9base 1:6-13 ยท Lucent
- debian trixie: 9base 1:6-14 ยท Lucent
- ubuntu jammy: 9base 1:6-11 ยท Lucent
- ubuntu noble: 9base 1:6-14 ยท Lucent
- ubuntu questing: 9base 1:6-15
- ubuntu resolute: 9base 1:6-15build1 ยท Lucent
- ubuntu stonking: 9base 1:6-15build1 ยท Lucent
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms