Rakesh
Rakesh

Reputation: 283

Vim search and replace query

I have a regular expression like this:

:%s/pattern/        pattern/gc

As you can see in the replacement text I want eight whitespace characters initially. I don't want to type eight white spaces every time. Is there a more elegant way of doing this?

Upvotes: 4

Views: 737

Answers (3)

kev
kev

Reputation: 161964

You can use printf('%*s', 8, ''):

A field width or precision, or both, may be indicated by an asterisk '*' instead of a digit string. In this case, a Number argument supplies the field width.

:%s/pattern/\=printf('%*spattern', 8, '')/gc

Upvotes: 3

Ingo Karkat
Ingo Karkat

Reputation: 172768

If you're after conserving keystrokes, you can initialize a named register with the replacement text:

:let @a = repeat(' ', 8)

Then, when building your substitution command, you insert the register contents via CTRL-R + {register-name}:

:%s/pattern/<C-R>a&/gc

For further simplification, I have referred to the search pattern in the replacement string via &.

Upvotes: 3

user679560
user679560

Reputation: 59

you can repeat some vi commands by entering a numberbefore the command, for example: 8iA would insert 8 A's (so replace that with space).

Upvotes: 1

Related Questions