6

In writing some Vimscript to try to answer this question, I was exploring ways to "search for a line with pattern P in position X" (P is a search pattern, X is a number).

One way to do this would be to search for (e.g.) ^.....[a-z], where [a-z] is the pattern, and 5 is the position.

But how would I use Vimscript to make ....., from . and 5?

8

Simply use regex syntax for "repeat n times":

/^.\{5}[a-z]

This searches for 5 .s and then an [a-z].

If you just want to repeat a character c n times, simply do:

let dots = repeat('.', 5)
" dots is now '.....'
  • That'll work for my example, but I was hoping for a more general answer which could be used in other kinds of string building – jalanb Feb 3 '15 at 23:37
  • @jalanb You could use repeat; see my edit. – Doorknob Feb 3 '15 at 23:52
4

Vim also has this nice feature /\%5c[a-z] which matches [a-z] only in column 5. You can read about it in more detail in :h \%c. There is also \%v which is to be used for virtual column which is relevant in case of a lot of wrapped lines and it will match the virtual column even for the wrapped lines.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.