Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vim: replace all characters up to a given token

Using vim I would like to replace all characters up to a certain one with another character, say a blank space - without affecting the layout/number of characters in the line. Here's an example:

Before:

real(kind=R12), intent(out) :: my_var

After replacing , intent(out) with blanks (i.e. starting from ,, and going up to )):

real(kind=R12)              :: my_var

I know about r to replace one character, and about nr to replace n characters, but I would like to know whether I can accomplish my task without first having to count the characters I want to replace.

Thanks a lot for your replies!

like image 945
canavanin Avatar asked Jul 30 '12 15:07

canavanin


People also ask

How do I change occurrences in Vim?

Press y to replace the match or l to replace the match and quit. Press n to skip the match and q or Esc to quit substitution. The a option substitutes the match and all remaining occurrences of the match. To scroll the screen down, use CTRL+Y , and to scroll up, use CTRL+E .

How do you replace a whole word in Vim?

To quickly change a word you can use cw , caw or ciw . Use c$ or just C to quickly change from the cursor to the end of a line, cc to change an entire line, or cis for a sentence. The standard change word command requires you to type cw , then a new word, then press Escape.

Which command is used to replace a character in vi editor?

8.3 Replace Command (r, R) The lower-case "r" command replaces the single character with the new character you key in. To accomplish this exchange, move the cursor so it sits upon the character to be replaced, press the "r" key, then press the key you want to see on the screen. VI will make the exchange.

How do I find special characters in Vim?

^ * $ \ ? ) have special significance to the search process and must be “escaped” when they are used in a search. To escape a special character, precede it with a backslash ( \ ). For example, to search for the string “anything?” type /anything\? and press Return.


2 Answers

Visual mode is probably the shortest way here:

vt:r 
  • v enter visual mode
  • t: select till :
  • r (note space after r) replace selected region with spaces.
like image 80
Thor Avatar answered Sep 20 '22 18:09

Thor


In command mode type 'df?' to delete up to that (?) character. Then 'i' to go back to insert.

For example if the following sentence is in your view:

The wizard quickly jinxed the gnomes before they vaporized.

and you enter dfs

You will be left with:

 before they vaporized.
like image 22
Quinn Carver Avatar answered Sep 18 '22 18:09

Quinn Carver