From 5c81308422ffdabce506e084e83fa883be535a1c Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Wed, 5 Feb 2020 21:48:51 +0100 Subject: Add backward search mappings Allow searching backward as well as forward. Add new functions specific to each search direction. Thanks to 'mattn' (https://www.reddit.com/user/mattn/) for describing how to pass variadic arguments to a Vim function: https://www.reddit.com/r/vim/comments/3761po/vimscript_question_passing_arguments_from_a/crk07lz/ --- plugin/searchop.vim | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/plugin/searchop.vim b/plugin/searchop.vim index ecfe2a1..a0b9835 100644 --- a/plugin/searchop.vim +++ b/plugin/searchop.vim @@ -1,5 +1,8 @@ -nnoremap z/ :set opfunc=Searchg@ -vnoremap z/ :call Search(visualmode(), 1) +nnoremap z/ :set opfunc=SearchForwardg@ +vnoremap z/ :call SearchForward(visualmode(), 1) + +nnoremap z# :set opfunc=SearchBackwardg@ +vnoremap z# :call SearchBackward(visualmode(), 1) function! Search(type, ...) @@ -16,7 +19,17 @@ function! Search(type, ...) let @/ = substitute(escape(@@, '\'), '\n', '\\n', 'g') call histadd('/', @/) + let @@ = user_unnamed_register +endfunction + +function! SearchForward(type, ...) + call call('Search', [a:type] + a:000) + call feedkeys('n', 't') +endfunction - let @@ = user_unnamed_register +function! SearchBackward(type, ...) + call call('Search', [a:type] + a:000) + + call feedkeys('N', 't') endfunction -- cgit v1.2.3