canavanin
canavanin

Reputation: 2709

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!

Upvotes: 27

Views: 15340

Answers (4)

Quinn Carver
Quinn Carver

Reputation: 605

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.

Upvotes: 6

idbrii
idbrii

Reputation: 11916

I know about r to replace one character

Did you know that R will keep you in that replace mode? So you could hit R and then hold Space until you've replaced everything you want.

However, I'd still go with Thor's answer. Visual mode allows you to use the efficient text navigation methods in vim without having to count out characters.

But if you disagree, there's always EasyMotion.

Upvotes: 3

Thor
Thor

Reputation: 47099

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.

Upvotes: 43

Libby Eichelberger
Libby Eichelberger

Reputation: 37

You can use regular expression here (use (.*?) to reference all values up to a token).

For instance:

The regex: (.*?)foo will get rid of everything up to foo.

Upvotes: 2

Related Questions