In the vi or Vim text editor, you can delete all characters on a line up to a specified word by placing the cursor on the line at the point where you wish to start the deletion then hit the
d
key followed by the slash
key followed by the word up to which you wish to remove the characters
on the line. E.g., suppose you have the following line:From the poem If— by Rudyard Kipling (1865-1936)
If you wished to delete all of the text on the line from the word "To" up
until, but not including the word "And" in "And so hold on...", while in command
mode, not insert mode, you could move the cursor to the "T" in "To" and then
hit the d key followed by the
forward slash key (/
) and then type And
(make
sure you use the matching capitalization). The line would then appear as shown
below.
Suppose, instead, you had the cursor at the word "To" as before, but wanted
to delete backwards through the word "If", i.e., all the way to the beginning
of the line, instead. You could then hit the d key while in command
mode, then hit the question mark (?
) key and type If
.
You would then have the text below.
For the backwards deletion, the word you type after the question mark is
included in the deletion. In this case, if you wished to delete
backwards to the beginning of the line, you could also have hit the
d
key followed by the ?
key and then hit the
caret
(^
) key, which represents the beginning of the line. Likewise,
you could hit the dollar sign ($
) key to delete forward
to the end of the line, which it represents, or you could just hit the
D
key, instead of the lower-case "d" to delete from the
current cursor position to the end of the line.