I have this \(\w[^\w\u370-\u3ff\u1f00-\u1fff]*\)\([\u370-\u3ff\u1f00-\u1fff][^\w]*[\u370-\u3ff\u1f00-\u1fff]\)\([^\w]*\w\), which I need to improve, but it is getting uncomfortably complicated; is it possible to define shorthands for expressions that are reused, such as \u370-\u3ff\u1f00-\u1fff? or is there some other way to make this easier to work with?

  • 2
    The Perl way would be to use (?<group-num>). Looking through :h regex, there's no Vim equivalent. How are you using this pattern? You could build it from variables, depending on how you use it. – muru Feb 23 '16 at 1:17
  • @muru, how can I reference variables in a / search? if I :let pat = "testpattern" and /pat(), vim searches for "pat()", not "testpattern". – Toothrot Feb 23 '16 at 7:54
  • I am using the pattern to enclose Greek passages in \gr{…}. :%s//\1\\gr{\2}\3. I need to improve it to include cases where a line begins or ends with Greek, and maybe where Gr. spans over a few lines. – Toothrot Feb 23 '16 at 7:58
  • I seem to recall that Vim doesn't support ranges for multibyte characters. I may be wrong though; perhaps ask on vim_dev. – Sato Katsura Feb 23 '16 at 15:17
  • @SatoKatsura, I read that too, but it does work. – Toothrot Feb 23 '16 at 15:55

You can define a variable holding your pattern like this: :let g:pat='...' (Note the use of single quotation marks, to prevent side-effects).

Then you can search for that variable like this: :%s/<C-R>=g:pat<cr>/replacement_part/g This is described at :h c_CTRL-R_= or possibly easier: :exe '%s/'.g:pat.'/replacement/g' (you could also wrap this in a printf() call)

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.