Thursday, July 18, 2013

Re: regex variable

On 09:42 Thu 18 Jul , shawn wilson wrote:
> Is there a way of using a variable in a regex?
>
> I'm constantly looking for the first octet of IP addresses, so I do:
> /[^0-9\.]10\.
>
> What I've been doing is hitting / and up arrow and then edit but if I
> could store [^0-9\.] in something easily accessed, that would save
> some time.

Hi,

You can set:
:let ip='[^0-9\.]
and then use:
/^r=ip<cr>
(i.i. hit '/' for search, the CTRL+r, then =, then write the variable name and
then hit <cr>). See ":help c^r=" for the expression register.

But maybe more useful is make a mapping:
cmap <Leader>i ['0-9\.]

or even better allow for this map only in search:
cmap <expr> <Leader>i (getcmdtype() == '/' ? "['0-9\\.]" : '\i')

I assumed here that your <Leader> key is set to '\', otherwise you just
have to modify the '\i'.

Best regards,
Marcin

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment