* [9fans] A learners sam cheat sheet
@ 2021-08-05 16:16 revcomninos
2024-08-22 3:17 ` [9fans] " kalterdev
0 siblings, 1 reply; 3+ messages in thread
From: revcomninos @ 2021-08-05 16:16 UTC (permalink / raw)
To: 9fans
[-- Attachment #1: Type: text/plain, Size: 6054 bytes --]
Below is my learners cheat sheet of sam commands which I am enclosing should find it helpful. I welcome any suggestions and/or corrections. These have been put together mostly from the standard resources, but there are gaps in my knowledge.
$ - end of line/file
$-/vi/ -- search from end of the file backwards
0,$ t "peter.c" 0 -- copy dot from 1 file to another
0 <date -- insert date at the top of the file
* 0 or more
0/regexp/ locates the first match of the expression in the file. (The form 0;// sets dot unnecessarily.)
0/vi/+-p -- search for the string "vi" and print the whole line
0/vi/ -- search from the 1st line forwards
-/^/+#10 -- go to 10th column of current line
1 <date -- replace first line with date
a1+- (a1-+) selects the line containing the end (beginning) of a1.
. any character
. | awk '!seen[$0]++' -- delete non-juxtaposed duplicate lines in dot
, | awk '!seen[$0]++' -- delete non-juxtaposed duplicate lines in whole file
[a-z] class a through to z
B < echo *.org -- One can load multiple files by using < as in:
B < grep -l vi * will load only those files that contain the string "vi"
{} Braces group commands
< cat /home/red/bin/lucy.id
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
double space selection -- x/0 a/0
< echo "Hello world" -- insert ascii code at the current position
, -- equivalent to 0,$
e replaces current file with one specified
f changes the filename
f <nl? -- set the current filename to null
| frm -- format selected text
g/regexp/ command -- If dot contains regexp, run command
indent selection 1 tab -- x/^/a/ /
. means that it is the current file
+ means the file has a window open
- means the file has been read by sam
* means the file is open in more than one instance
' means the loaded file differs from the file on disk
[ˆn] any char but n
/.+\n/ finds lines with text between lines
n gives a LIST of files
/.*\n/ -- matches single whole lines
[nm] n or m
| pipe dot through command
-/.PP/,/.PP/- highlight current paragraph in an nroff doc
p -- prints contents of dot
= -- prints value of dot
.,/regexp/ extends dot.
./regexp/// finds the second following occurrence of the expression
r emacs.ms is equivalent to <cat emacs.ms
< replace dot by stdin of command
(re) tag pattern
r replaces dot in the current file with the contents of the file specified
! run the command
> send dot to stdin of command
,|sort -- sort the file
, > spell -- check spelling
# substitute #'th tagged pattern
,t "junk.c" 0 -- copy current file to start of junk.c
|tr a-z A-Z -- change case
,| uniq -- delete juxtaposed duplicates blank lines
/vi/+- m 0 moves the line containing "vi" to the top of the file
/vi/ or +/vi/ -- search forward
/vi/+- t 0 copies the next line with vi to the top of the file
/vi/+/vi/ -- search for the second occurrence of "vi" following dot
v/regexp/ command -- If dot does not contain, run command
,>wc -l -- count lines in file
,>wc -- word count of current document
w writes the whole file to disk or parts of a file
x/$^/!# -- comment a line
,x/ +$/d
.x/'+$/i/ --color-auto/
x/$^/!# ,x/^$\n/d
x/$^/!# ,x/[0-9]/d
x/^/.,/0d -- strip C comments from selection
,x/^/a/ / -- indents blocks of text with a tab stop.
,x/[a-zA-Z0-9]+/-#0;+#1| tr a-z A-Z -- capitalise every word (slow)
,x/[‘‘’’“”‘’]/c/"
X/\.c$/ ,x/variable/+-p -- find all uses of a variable in the C source files
,x/‘‘|’’/c/"/ -- change smart quotes to curly
x cmd - set dot and run command on each matching line
,x/ +/d
,x/\*/d
,x/^ +/d
,x/\*/d -- delete all asterisks
,x/^ /d -- deletes the indents
X D -- remove out all up to date files
,x/^ +/d -- Remove the space at the beginning of the line
,x g/^$\n/d -- delete blank lines
,x/good/ c/bad/
,x g/vi/p - idem
,x/^http.[^,]*?/d
,x/^http.[^,]*?/d -- search for "http...," and delete
,x/\n+/a/\n/ -- finds every occurrence of 1 or more (+) newlines (\n) and appends a new line.
,x/.*\n/ g/ed/ v/red/ p -- find ed but not red
,x/(.+\n)+/ g/It was the best of times/ p
,x /.*\n/ g/vi/ p - break file into lines, if lines contain "vi, print
,x/\n\n\n+/c/\n -- idem
.x/Peter/d -- search dot for 'Peter' and delete 'Peter'
X/regex/b -- switch to a loaded file with a regex
X/regexp/ command -- Run command on files whose menu line matches
x/regexp/command - set dot and run command on each match
X/\.sam$/,x/the/+-p -- search multiple files for text and print the results
.x s/.*/echo "&."/
,x s/good/bad/
,x s/\.\.\n/g -- break sentences into new lines
,x s/\\p/\n\n/g
,x s/\\s/\n\n/g
,x/<string>/|tr a-z A-Z
x/^<tab>/d -- remove 1 tab of indent from selection
,x/teh/c/the/
,x/^TODAY$/<date -- replace today with output of date
,x/\\v|\\c|\\s1|\\q1/d
,x/vi/c/vi{TM}/ -- change vi to vi{TM}
,x/vi/+-p -- print lines that contain the string vi
,x/vi//{TM}/d - find the {TM} that follows every occurrence of vi $ delete
,x/vi/x/E/c/e/ - change all the capital "E"s in "vi" to lower case "e"s
X/.*/,x/<cr>/d -- strip <cr> from all files
X ,x/(.+\n)+/ g/account/p
,y/best/ x/(.+\n)+/g/It was the best of times/p
Y/regexp/ command -- As X but select unmatched files
y/regexp/ command -- as x but select unmatched text
,x[a-zA-Z]+/{
g/fred/ v/...../ c/jim/
g/jim/ v/..../ c/fred/
} -- swap fred for jim in file
,x/Emacs|vi/{
g/Emacs/ c/vi/
g/vi/ c/Emacs/
}
Go from search results in sam window to document:
,x g/<string>/{
p
}
,| ssam -f your_script
As for comments in scripts, if you use ssam(1) to run them the easiest way
would be starting the lines you want to be ignored with a character
that isn't a sam command - this will work because ssam sends stderr to /dev/null.
------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/Tc2ebea6bfef26073-M5318338af59b6b616cd4210c
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription
[-- Attachment #2: Type: text/html, Size: 8962 bytes --]
^ permalink raw reply [flat|nested] 3+ messages in thread
* [9fans] Re: A learners sam cheat sheet
2021-08-05 16:16 [9fans] A learners sam cheat sheet revcomninos
@ 2024-08-22 3:17 ` kalterdev
2024-08-22 9:00 ` Mathieu Bivert
0 siblings, 1 reply; 3+ messages in thread
From: kalterdev @ 2024-08-22 3:17 UTC (permalink / raw)
To: 9fans
[-- Attachment #1: Type: text/plain, Size: 324 bytes --]
> # substitute #'th tagged pattern
Are there any examples? I can't find any in Pike's papers.
------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/Tc2ebea6bfef26073-M8aa6c884325788677721755e
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription
[-- Attachment #2: Type: text/html, Size: 1177 bytes --]
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [9fans] Re: A learners sam cheat sheet
2024-08-22 3:17 ` [9fans] " kalterdev
@ 2024-08-22 9:00 ` Mathieu Bivert
0 siblings, 0 replies; 3+ messages in thread
From: Mathieu Bivert @ 2024-08-22 9:00 UTC (permalink / raw)
To: 9fans
[-- Attachment #1: Type: text/plain, Size: 1431 bytes --]
Hi,
I think it's a typo in the cheat sheet; at least for s///,
\i is the correct syntax to reference "tagged patterns":
% echo foo bar | ssam 's/([a-z]+) ([a-z]+)/\2 \1/'
bar foo
I lack familiarity with sam/sam's language to be certain,
but I doubt it'd make much sense to have it for other
commands; sam(1) describes it for s/// only.
From a quick glance, there seems to be other "typos" in
the cheat sheet[0]:
s/"([ˆ"]*)"/‘‘1’’/ replace "hello" with ‘‘hello’’ in selection
/(.+0+/ matches blocks of text separated by blank lines
Cheers,
[0]: http://9p.io/sources/contrib/steve/doc/sam-refcard.pdf
On Thu, Aug 22, 2024 at 5:18 AM <kalterdev@gmail.com> wrote:
> # substitute #'th tagged pattern
>
>
> Are there any examples? I can't find any in Pike's papers.
> *9fans <https://9fans.topicbox.com/latest>* / 9fans / see discussions
> <https://9fans.topicbox.com/groups/9fans> + participants
> <https://9fans.topicbox.com/groups/9fans/members> + delivery options
> <https://9fans.topicbox.com/groups/9fans/subscription> Permalink
> <https://9fans.topicbox.com/groups/9fans/Tc2ebea6bfef26073-M8aa6c884325788677721755e>
>
------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/Tc2ebea6bfef26073-Mb5cced754d2a118bfb58d449
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription
[-- Attachment #2: Type: text/html, Size: 2603 bytes --]
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2024-08-22 9:01 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-05 16:16 [9fans] A learners sam cheat sheet revcomninos
2024-08-22 3:17 ` [9fans] " kalterdev
2024-08-22 9:00 ` Mathieu Bivert
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).