Hamilton LaboratoriesHamilton C shell 2012User guideTutorials

Editing

Oregon Coast

Editing
Previous | Next

Topics

Word selections
Search/replace
Pathname editing
Specialized operations
History edits
See also

Often, the text returned by a history, variable or command substitution isn’t quite what you want. For example, you may want to select only certain words, do a search/replace, or manipulate a filename that’s been returned. The editing facilities provide these capabilities.

The examples in this chapter show a sampling of various combinations of editing commands and types of substitutions. There simply isn’t room to show all the possibilities nor is there really a need to: with two exceptions (:%and :p), any editing command can be applied against any substitution type or against the result of another editing command.

Word selections

Editing modifiers begin with a colon, :, followed by an editing command. You can select words either by number or symbolically: first, last, all, etc. Here are examples of the :*, :$ and :^ modifiers to select all, last, and first argument words, respectively. Notice that any number of editing modifiers may strung together. For example, :*:^ means the first argument word (word 1) of the sequence formed of all the original argument words, i.e., word 2.

233 D% echo Finally, now is the time Finally, now is the time 234 D% echo !!:$ time 235 D% echo !?Fin:*:^ echo now now

It is also possible to select words by indexing through the array associated with a given substitution. The words are counted from zero.

236 D% echo `echo now is the time`:2 the

Ranges of words can also be specified with a hyphen between the beginning and ending word indices. In this context, the minus sign indicates a range, not subtraction.

237 D% set x = now is the time for all good men 238 D% echo $x:1-5 is the time for all

When the operand is a !?-style history substitution, there’s a special operator, :%, for selecting the particular word that triggered the match:

239 D% echo !?Fin:% echo Finally Finally

Each of the three symbolic word selectors, ^, $ and %, can be used anywhere a decimal word number would be acceptable. For example:

240 D% echo !?time:%-$ echo time for all good men time for all good men

Search/replace

A search/replace operation looks for and replaces a simple character string. (For those familiar with such things, it does not use regular expressions.) If the search string isn’t given, the one used last time is used again. If the replace string contains an ampersand, &, that isn’t escaped with the circumflex, it’s expanded into the search string. Here are a few examples. Notice that putting a g at the start of an operation makes it “global.”

241 D% echo !?now:%-$:s/o/O/ echo nOw is the time for all good men nOw is the time for all good men 242 D% !!:gs/t/T/ echo nOw is The Time for all good men nOw is The Time for all good men 243 D% !!:s/nOw is/& really/ echo nOw is The Time for all good men nOw is really The Time for all good men

Pathname editing

Another set of operators allows filenames to be easily manipulated. As with the search/replace operator, pathname editing is normally applied to only the first operand word; if you want the operation performed on all the words, you must make it global. In this example, the :h (head) operator is used, which returns the name of the directory containing the given file.

244 D% echo *\a*.c samples\args.c sh\allocate.c 245 D% set dotc = !$ set dotc = *\a*.c 246 D% echo $dotc samples\args.c sh\allocate.c 247 D% echo $dotc:h samples sh\allocate.c 248 D% echo $dotc:gh samples sh

Specialized operations

Specialized operations are provided for scanning a character string and breaking it up into words and applying quotes around each word.

The :x operator for breaking up a string into words, single-quoting each word, is particularly useful for parsing text read with the getline pseudo-variable, which always returns the line read as a single character string:

249 D% @ data = $< (I typed -->this<-- in) 250 D% echo $#data $data 1 (I typed -->this<-- in) 251 D% set data = $data:x 252 D% echo $#data $data 10 ( I typed -- > this < -- in )

The :q operator pastes single quote marks around every word in the operand list. As we’ll see in the chapter on quoting, this prevents any further wildcard, variable or command substitutions from being done.

253 D% echo sa?pl?s *\a*.c samples samples\args.c sh\allocate.c 254 D% echo !*:q echo 'sa?pl?s' '*\a*.c' sa?pl?s *\a*.c

History edits

For history substitutions, the :p operator can be used to cause the result of the history substitution to be echoed and entered into the history list but for the command not to be executed. This is helpful when you’re trying a complicated edit and not sure if the result is going to be what you want.

255 D% !?Fin:p echo Finally 256 D% !! echo Finally Finally 257 D% _

See also

Variable substitution
History recall
Substitution modifiers
Pathname editing
Order of evaluation
Tutorial: The history mechanism
Tutorial: Variable substitutions
Tutorial: Command substitution

Previous | Next

Getting started with Hamilton C shell

Hamilton C shell, as it first wakes up.

Getting started with Hamilton C shell

A first few commands.

You can set the screen colors to your taste.

You can set the screen colors to your taste.