Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save commonly used regex patterns in Vim?

Tags:

Is there an easy way to save a commonly used Regex pattern so that I can reuse it between different files? I look through many log files and always need create a mildly complex regex (it's not rocket science but it is a pain to retype) to find errors so it would be good to have a way to recall that without having to save it in a text file and paste it into the search params each time.

like image 637
Maiku Avatar asked Feb 04 '10 16:02

Maiku


People also ask

What regex does Vim use?

vim which translates from PCRE (Perl-compatible regular expressions) to Vim's regex syntax. This will make it more likely for regexes from other environments to work, because PCRE have become the de facto industry standard: e.g. Java's java.


1 Answers

Put:

source ~/.regexlist.vim 

into your vimrc.

Do the search. Decide you might want to use that one again. Type this literally:

:sp ~/.regexlist.vim<CR> olet MyRegExName = '<C-R>/'<ESC> :w<CR> :so %<CR> :q<CR> 

Where <C-R> is CtrlR, <CR> is ENTER and <ESC> is ESC.

Next time you want to use it:

/<C-R>=MyRegExName<CR><CR> 

Ctrl-R is your friend! Ctrl-R followed by / pulls up the last search. Ctrl-R followed by = allows you to enter an expression.

like image 120
DrAl Avatar answered Sep 24 '22 22:09

DrAl