Skip to content

Instantly share code, notes, and snippets.

@jtratner
Created May 4, 2012 06:39
Show Gist options
  • Save jtratner/2592636 to your computer and use it in GitHub Desktop.
Save jtratner/2592636 to your computer and use it in GitHub Desktop.
Checks your vim regexp escapes -- save yourself tons of hassle!
command! -range -nargs=* CheckRegexpEscapes :call CheckRegexpEscapes(<f-args>)
" call with either no arguments or a:1 for escape character +
" an argument for each character to be escaped
" e.g. CheckRegexpEscapes('\\','(',')','_') ->
" checks that you put in '\\' behind each of '(',')','_',')'
" and prompts you to change it
" always asks to substitute
function! CheckRegexpEscapes(...)
" NonEscapedCharRegexp:
" takes in an escape character (note: '\' must be inputted as '\\') and
" a character to be checked for an escape and returns a regexp string to do
" so.
" String \1 character preceding char \2 char or regexp string
function! NonEscapedCharRegexp(escape,char)
return '\([^'.a:escape.']\)\{1,1}\('.a:char.'\)'
endfunction
if a:0 <= 0
let charlist = ['(',')','{','|']
echo charlist
let escapechar = '\\'
elseif a:0 == 1
let charlist = ['(',')','{','|']
let escapechar = a:1
else
let charlist = a:000
let escapechar = a:1
endif
" fix '\' so that it is escaped in the regexp
if escapechar == '\'
let escapechar = '\\'
endif
" set up the pieces, range, end, etc
let rangesearch = ':'.a:firstline.','.a:lastline.'s'.'/'
let endstr = '/\1'.escapechar.'\2/gc'
for i in range(0,len(charlist)-1)
try
let theregexp = rangesearch.NonEscapedCharRegexp(escapechar,charlist[i]).endstr
exe theregexp
" catch pattern not found errors
catch /^Vim\%((\a\+)\)\=:E486/
echo "no unescaped ".charlist[i]." . Good job!"
endtry
endfor
echo "Phew! Glad that's over!"
endfunction
@jtratner
Copy link
Author

jtratner commented May 4, 2012

Ironically, I kept screwing up the regexp's for this function as I was writing it. Now that won't be a problem and I'm very happy about it :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment