Using the Sam editor.

Start the Sam text editor by creating a new window in plan 9.

  • Hold down right click on a blank part of the screen and select ‘new’ from the green menu, then right-click and drag the mouse to create a blank window.

You should see term% running in the new window that you created. You want to enable scrolling so that the window will follow the text that you are going to type in, so click the middle mouse-button click and select ‘Scroll’ from the menu. This way the window should scroll as you type in more text.

To start the editor you must type in sam -d. The d option is to disable the mouse to make input only from the keyboard. Once you have done that the editor should be running and the output should look  similar to what is written below.

term% sam -d
-. 

It has begun running and you are ready to enter some text. Adding text is very simple and is most easily done by telling sam that you are going to add some text at the end of the current index ( using the ‘a’ option ) and then typing the text.

a  

Hello World! This is my first text in the sam editor

.

Here the ‘a’ tells sam to add the text before the period, and the dot ‘.’ specifies the end of the selection of our text. These indexes are used to tell sam where to put the text or the command that you are running. Regular expressions, overwriting and copying text from one index to another can be part of the command portion of the statement. To simply print what we have so far you can just enter.

p

And hit enter:

And Sam will print out.

Hello World! This is my first text in the sam editor

To print the first line of the file, type:

1p

Hello World! This is my first text in the sam editor

Or if you want to print everything in a longer file you can specify the start index 0, to the last null line $, and then the command print (‘p’):

0,$p

Hello World! This is my first text in the sam editor

Here the 0,$p command is telling the editor to start at the first line and go to the last line and print. This is a type of addressing. You can run 1,2p or 1,15p or 3,6p to print the selection of lines in the range you are looking for. Occasionally you might accidentally get the range wrong and get an error like:

?address range

Telling you that there isn’t the range that you specified in the selected text. So re-type the range and you will get it right.

eg.

1,2p

Hello World! This is my first text in the sam editor

There are more commands that sam can use at the start of a file, such as ‘c’ to replace,’i’, to insert at the start of the line, ‘a’ to add at the end of the that index. These indexes are usually referred to as the current ‘dot‘. More generally you can think of them as operating on the selection of text that we have created.

Alternative ways to write these commands can be in the syntax command/expression like what is written below.

a/Hello Universe – Appends Hello Universe to the end of dot.

i/Just before Hello  Universe – Inserts string into the start of the dot. 

c/Hello entire universe – Overwrites what’s in dot with “Hello entire universe”

Selecting more than one dot is possible and is easily done with the range commands that we have. So if we wrote all those commands above we might have 3 dot’s to work with and we can set the current dot to 3 for example by typing into the terminal:

sets the current dot to the second ‘dot’

2 

i/Text inserted before dot 2

then

a/Text inserted after dot 2

That inserts those strings at the end of the 2nd dot.

Now we might have:

Hello World! This is my first text in the sam editor

Just before Hello Universe

Text inserted before dot 2

Hello Universe

Text inserted after dot 2

Simple! A full command list is written below so that you can try it on the text that you have already. No changes will be made to the file yet, so you can overwrite and undo as many times as you want. At the end you might like to write the file and that command is simply ‘w‘.

*Image taken from http://plan9.bell-labs.com/sys/doc/sam/sam.html

To run a command on a whole range of text it is possible to select the whole file to do that on, and you simply type in

,x/World/c/Universe

0,$p

Hello Universe! This is my first text in the sam editor

And if you want to change back (in other words undo) then you type in the command:

u

0,$p

Hello World! This is my first text in the sam editor

COMMANDS

You can run eight basic commands on the file that you have in Sam. These are unix commands that allow you to run other commands present in your system. For example using the command > lets you operate on the current text. Using writes to the current text.

For example you are able to count the words in the current text, so you can type in:

1,2>wc

And you will get information like this:

1,2>wc

2             18          87

Word count operates on the current text, in this case it is ( Hello World! This is my first text in the sam editor ).

Alternatively you can run the date command and get some information on the date and time from the system.

!date

Mon MAY   7  02:45:30 EDT 2012

And you may want to write that to the end of the file ( or insert it to the address of dot). So set the address of where you want to write to by typing in our case.

2  (set the address of dot to 2)

2<date  (command date into 2nd address)

0,$p (print the file from 0 to last null line)

And you have written the date to the 2nd address. This will print something like this:

Hello World! This is my first text in the sam editor

Mon MAY   7  02:45:30 EDT 2012

A good outline of many of the features of Sam is located in an HTML version here:

http://plan9.bell-labs.com/sys/doc/sam/sam.html

And a more complicated, but detailed version of how to use Sam, is here.

http://doc.cat-v.org/bell_labs/sam_lang_tutorial/sam_tut.pdf

A quick reference card for Sam is located here:

http://plan9.bell-labs.com/sources/contrib/steve/doc/sam-refcard.pdf

3 thoughts on “Using the Sam editor.

  1. Jonas A says:

    What is you rationale for using ‘sam -d’ vs the default mode with mouse support? I also find ‘ed’ in Unix/Linux handy at times, but Plan 9 was built for using the mouse… No offense, just curious! (BTW, thanks for blogging on Plan 9)

    • iainws says:

      Its totally up to you! However from most of the reading I have done users prefer Acme to write and edit their files. I used the -d option at the time because I was still learning with the mouse and the instructions seem easier if your focus is just on the keyboard. But definitely in other areas of plan 9, mouse support is clearly a winning option.

  2. aliasxerog says:

    damn son, sam -d is hardcore. props to you using is as a beginner.

Leave a reply to iainws Cancel reply