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, samterm copies leading white space on the current line to the new line.
  • -d โ€” Do not download the terminal part of sam. Editing will be done with the command language only, as in ed(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 -r option.
  • -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; #0 is the beginning of the file.
  • n โ€” Line n; 0 is 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 the k command 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 address a2 evaluated starting at the end of a1.
  • a1-a2 โ€” The address a2 evaluated looking in the reverse direction starting at the beginning of a1.
  • a1,a2 โ€” The substring from the beginning of a1 to the end of a2. If a1 is missing, 0 is substituted. If a2 is missing, $ is substituted.
  • a1;a2 โ€” Like a1,a2, but with a2 evaluated 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.

  • c
  • i โ€” Same as a, but c replaces the text, while i inserts before the range.
  • d โ€” Delete the text in the range. Set dot.
  • s/regexp/text/ โ€” Substitute text for the first match to the regular expression in the range. Set dot to the modified range. In text the character & stands for the string that matched the expression. Backslash behaves as usual unless followed by a digit: \d stands for the string that matched the subexpression begun by the d-th left parenthesis. If s is followed immediately by a number n, as in s2/x/y/, the n-th match in the range is substituted. If the command is followed by a g, as in s/x/y/g, all matches in the range are substituted.
  • m a1
  • t a1 โ€” Move (m) or copy (t) the range to after a1. 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 that sam also has in its menu. The list may be expressed <Plan 9 command in which case the file names are taken as words (in the shell sense) generated by the Plan 9 command.
  • * B file-list โ€” Same as b, 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 to D a modified file, but a subsequent D will 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 (default 0,$) 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, $home is 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 โ€” Like x, but run the command for each substring that lies before, between, or after the matches that would be generated by x. 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 as X, but for files that do not match the regular expression, and the expression is required.
  • g/regexp/ command
  • v/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 second q will succeed. So like... it double-checks you're sure ๐Ÿ’…
  • u n โ€” Undo the last n (default 1) 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. Successive u's move further back in time โช. The only commands where u is powerless are cd, u, q, w and D (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 .+1 and 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 in rio.
  • close โ€” Delete the window. In the last window of a file, close is equivalent to a D for the file.
  • write โ€” Equivalent to a w for 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 a click attribute defining where the selection lies (see plumb(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 with rio.
  • /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