Easiest way to test vim regex?
Asked Answered
D

4

13

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.

Directly answered 24/1, 2013 at 10:29 Comment(1)
Correction: that clobbers the mark r, not the register r.Romelda
J
10

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>
Jalopy answered 24/1, 2013 at 10:38 Comment(0)
W
7

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

Willi answered 24/1, 2013 at 10:31 Comment(0)
D
6

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

Devilry answered 28/1, 2013 at 13:52 Comment(1)
Reference see https://mcmap.net/q/107776/-how-to-select-between-brackets-or-quotes-or-in-vim for meaning of i" (notice i and " are not disjoint commands).Bankroll
R
0

Ideal solution:

enter image description here 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)

enter image description here

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\+$'
Rhodonite answered 16/4, 2022 at 3:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.