aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2019-05-25 17:09:01 +1200
committerTom Ryder <tom@sanctum.geek.nz>2019-05-25 17:09:01 +1200
commit940b67ef1dbd7d86874102e38de661aacbfd65a2 (patch)
tree062736772969ff5be65b1a127844e5e397244887
parent03dd986199202016bf8aaad972f31627ba5e47d0 (diff)
downloadvim-regex-escape-940b67ef1dbd7d86874102e38de661aacbfd65a2.tar.gz
vim-regex-escape-940b67ef1dbd7d86874102e38de661aacbfd65a2.zip
Switch to escape() method
Much neater than substitute().
-rw-r--r--autoload/regex_escape.vim19
1 files changed, 9 insertions, 10 deletions
diff --git a/autoload/regex_escape.vim b/autoload/regex_escape.vim
index 6acff6b..b57392a 100644
--- a/autoload/regex_escape.vim
+++ b/autoload/regex_escape.vim
@@ -1,10 +1,9 @@
-" Define Vim pattern character classes of characters that should have an
-" escape character added before them to make them literal, for use in
-" substitute()
+" Define characters that should be scaped to make them literal, for use as the
+" second parameter to escape()
let s:classes = {
- \ 'bre': '[][\.*?^$]',
- \ 'ere': '[][\.*?^$+{}()/]',
- \ 'vim': '[][\.*^$~]'
+ \ 'bre': '\.[]*^$?',
+ \ 'ere': '\.[]*^$?+{}()/',
+ \ 'vim': '\.[]*^$~'
\ }
" This function does the actual translation, defined as 'operatorfunc' for the
@@ -45,12 +44,12 @@ function! regex_escape#Operatorfunc(type) abort
" Get the corresponding character class
let class = s:classes[flavor]
- " Perform the substitution on the unnamed register's contents, inserting a
+ " Perform the escaping on the unnamed register's contents, inserting a
" backslash before every instance of any character in that class
- let @@ = substitute(@@, class, '\\&', 'g')
+ let @@ = escape(@@, class)
- " Paste our substituted changes back in over the top of the previously
- " selected text, by reselecting it before the paste
+ " Paste our escaped changes back in over the top of the previously selected
+ " text, by reselecting it before the paste
silent normal! gvp
" Restore contents of the unnamed register and the previous values of the