'Easiest way to test vim regex?
I'm trying to write a vim syntax file, one problem I'm dealing with is vim's regex syntax is different from most other languages(I think the one called "perl compatible regex", Python, JavaScript etc. uses mostly the same regex syntax) and so I can't use regex testers like regexpal.
What is the easiest way to try vim regexes ? Ideally what I want is two splits, in one I'll write vim regex, and the other one will have the text, and matched parts should be highlighted.
Thanks in advance.
EDIT: I added a modified version of Ingo Karkat's answer to my .vimrc
:
nnoremap <F5> mryi":let @/ = @"<CR>`r
This version also puts cursor back where it was when pressed F5 with the cost of losing mark r
.
Solution 1:[1]
Do this in Vim. For syntax files, you often deal with start="^begin"
stuff. Yank the regexp via yi"
, then highlight it via:
:let @/ = @"
Of course, if you need this often, bind this to a mapping (and :set hlsearch)
, like:
:nnoremap <F5> yi":let @/ = @"<CR>
Solution 2:[2]
Use vim
to test the regex. By typing /
in vim
you can search for stuff and the search uses regex.
For help on syntax have a look on vimregex
Solution 3:[3]
We can access Vim's registers in Ex mode or in searching mode by pressing Ctrl+R and then the register you want to paste.
In your situation we can use this to copy the text we want to test and then paste this in our search.
yi"/Ctrl+R0CR
Explanation:
yi" -> Yank inside of double quotes "
/ - > Open the search field
Ctrl+R0 -> paste the last yanked text
Solution 4:[4]
Ideal solution:
https://github.com/bennypowers/nvim-regexplainer
But it seems that it only work on javascript regex, not vim's, so far.
Nice website (but no vim's regex engine)
Convert magic to very magic (to avoid backslashits):
https://github.com/sisrfeng/VimRegexConverter
Split to pieces of strings by hand:
if file_name =~# '\v^(' .. '[\%#~]' .. '|' .. '\.?[/\\]\a+:' .. '|' .. '^\d+' .. ')$'
vs
if file_name =~# '^\.\=[\\/]\|^\a\+:\|^[%#~]\|^\d\+$'
Solution 5:[5]
You can also try RegExr It's a good tool to learn RegEx and try them in real time.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Ingo Karkat |
Solution 2 | Matti Lyra |
Solution 3 | |
Solution 4 | |
Solution 5 | Abhi Sood |