# Sam Cheat Sheet from 9fans

Thread: `[9fans] Revised sam cheat sheat`

By: `revcomninos`


x cmd - set dot and run command on each matching line

. -- apply command to dot

, -- apply command to whole document

< replace dot with std input of command

! run the unix command

> send dot to std input of command

0<date -- insert date

>spell -- check spelling

>wc -l -- count just the lines

>wc -- do a line, word and character count

|tr a-z A-Z -- change case

,x/".+"/ y/"/ | tr [:lower:] [:upper:]


,x/".+"/ y/"/ | tr a-z A-Z

-- to capitalise all letters inside quotes

s/.*/*&*/ -- enclose phrase in asterisks

|fold -s -- break up/format long lines

|fmt -- idem

|sort -- sort the file

|uniq -- delete juxtaposed duplicate lines

|awk '!seen[$0]++' -- delete non-juxtaposed duplicate lines

,x/\n+/a/\n/ -- finds every occurrence of 1 or more (+) newlines (\n) and appends
a new line.

/vi/ or +/vi/ -- search forward

0/regex/ -- search from beg. of file forwards

0/regex/+-p -- search for a string and select the whole line that contains it

/vi/+- m 0 selects and moves the next line containing "vi" to the top of the file

/vi/+- t 0 selects and copies the next line with "vi" to the top of the file

$/regex/ -- search from end of the file backwards

Search for a string, and then go from search results in sam window to document:

,x g/hello/{




0,$ t "peter.c" 0 -- copy contents of one to another

, and ; selects whole document

, equivalent to 0,$

0;5 and 0,5 - selects 1st five lines

-/^/+#10 -- go to 10th column of current line

B < echo *.org -- One can load multiple files by using <

B < grep -l vi * will load only those files that contain the string "vi"

D is the complement of B. It eradicates the file from sam's memory but does not
delete the file from disk! Without a name argument removes current file

e replaces current file with one specified

f changes the filename

f <nl? -- set the current filename to null

n gives a list of read and loaded files

{} Braces group commands

[ˆn] any char but n

[nm] n or m

w writes the whole file, or parts of a file to disk

< cat /home/red/bin/songs -- insert contents of a file

r /home/red/bin/songs -- idem

r replaces dot in the current file with the contents of the file specified

< /home/red/bin/songs -- insert contents of a script

> /home/red/bin/pass google -- output contents of script to sam window

< echo "Hello world" -- insert ascii code at the current position

x/^/a/ / -- indent the selected text by 1 tab. x means apply to all lines in the

sam terminal information


. indicates the current file

+ the file has a window open

- the file has been read by sam

* the file is open in more than one instance

' the loaded file differs from the file on disk

/.+\n/ -- finds lines with text

/.*\n/ -- matches single whole lines

0/Ben/,/Son/ -- find a range of text

.,/regexp/ -- selects text FROM dot to the regex

./regexp// -- selects the second occurrence of a regex

./regexp// -- selects the third occurrence of a regex

/vi/+/vi/ -- search for the second occurrence of "vi" following dot

p -- prints contents of dot in sam terminal

= -- prints current value of dot in sam terminal

,t "junk" 0 -- copy current file to start of file "junk"

Perform conditional ACTIONS on lines:

g/regexp/ command -- g = if. If dot contains regexp, run command


,x g/vi/p -- if a line has "vi" print the line in sam window

v/regexp/ command -- If dot does not contain, run a command

,x/.*\n/ g/ed/ v/red/ p -- find lines with ed but not red

,x/".+"/ y/"/ -- search for text in quotes but do not show lines with "y"

,x/".+"/ x/"/ -- search for text between quotes and only show the quotes

,x /.*\n/ g/vi/ p - break file into lines, if lines contain "vi, print

,x/ +$/d -- delete empty whitespace at end of every line

,x/'+$/i/ --color-auto/ -- insert the phrase "--color-auto" into every line that
ends in 1 or more '

.x/Peter/d -- search dot for 'Peter' and delete 'Peter'

,x/\\v|\\c|\\s1|\\q1/d -- multiple deletions

,x/\*/d -- delete all asterisks

,x/^ /d -- delete the indents

,x/^ +/d -- delete all blank spaces at beginning of lines

,x/^$\n/d -- delete all blank lines

,x/\n\n\n+/c/\n -- change multiple blank lines into 1 blank line

,x/[0-9]/d -- delete lines with numbers

,x/^http.[^,]*?/d -- search for "http...," and delete

C-i -- create a tab stop of 8 spaces

,x/^/a/ / -- indents text with a tab stop.

x/^ /d -- remove 1 tab of indent from selection

,x/[‘‘’’“”‘’]/c/" -- change all smart quotes into straight ones

,x/".+"/ x/"/ c/'/ -- change double quotes (that surround one or more characters)
to single quotes

,x/good/ c/bad/ -- change "good" to "bad" on every line


,x/Emacs/x/E/c/e/ - change all the capital "E"s in "Emacs" to lower case "e"s

X -- is an iterator that works on files not lines

X D -- remove out all up to date files

X/regexp/ command -- Run command on files whose menu line matches

X/questions/D -- close file called "questions"

X/\.sam$/,x/the/+-p -- search multiple files for text and print the results

X/.*/,x/<cr>/d -- strip <cr> from all files

X ,x/(.+\n)+/ g/account/+-p -- look through every file, and every line, and if any
line contains "account" print it out in sam window

s/.*/& Hello/ -- add " Hello" to the end of line


The y command *excludes from consideration* in the seach that follows.

,y/ben/ x/(.+\n)+/g/Directory details/p

Y/regexp/ command -- Exclude the file named by "regex" from consideration.

.x g/fred/ v/........./ c/jim/ -- Look through dot. If dot contains "fred" change
it to "jim" provided it is not "fred....."


g/fred/ v/...../ c/jim/

g/jim/ v/..../ c/fred/


In the above command braces {} allow for multiple commands. The command says:
search every line that contains 1 or more letters, if the line contains "fred"
change it to "jim" and if "jim" change it to fred, provided "fred" is not "fred."
and "jim" not "jim."

g - "if"

v" - "if not, unless the regex is"

,| ssam -f your_script -- apply a sam script