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.
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.
It is also possible to select words by indexing through the array associated with a given substitution. The words are counted from zero.
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.
When the operand is a !?-style history substitution, there’s a special operator, :%, for selecting the particular word that triggered the match:
Each of the three symbolic word selectors, ^, $ and %, can be used anywhere a decimal word number would be acceptable. For example:
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.”
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.
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:
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.
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.
Variable substitution
History recall
Substitution modifiers
Pathname editing
Order of evaluation
Tutorial: The history mechanism
Tutorial: Variable substitutions
Tutorial: Command substitution