Can vimgrep be used to visually select the current file?

I would like to search with vimgrep only within the visual selection of the current file, not the entire file. Is it possible and how? I could not find something for this case with Google or using vim.

The reason I want this is because I need the result in quicklist ( copen) and :g/FOOwhich shows the corresponding lines below, does not do this job.

+4
source share
3 answers

Yes, you can, since Vim has special regular expression atoms for marker positions, and the beginning and end of visual highlighting are marked with '<and '>. Since there are atoms to include / before / after the label, we need to combine them to cover the entire range of selected lines:

At the beginning of the selection |after the start of the selection and before the end of the selection |at the end of the selection.

To limit the search to the current file, a special keyword is used %.

:vimgrep/\%(\%'<\|\%>'<\%<'>\|\%'>\)FOO/ %
+5
source

You are on the right track with the team :g. The basic idea is to do something like this:

:g/FOO/caddexpr expand("%") . ":" . line(".") .  ":" . getline(".")

Now let's make a team

command! -range -nargs=+ VisualSeach cgetexpr []|<line1>,<line2>g/<args>/caddexpr expand("%") . ":" . line(".") .  ":" . getline(".")

Now you can do :VisualSearch FOOit and it will add the search terms to the list quickfix.

, w/this .

+4

vimrc. * . .

" Visual Mode */# from Scrooloose {{{
function! s:VSetSearch()
let temp = @@
norm! gvy
let @/ = '\V' . substitute(escape(@@, '\'), '\n', '\\n', 'g')
let @@ = temp
endfunction

vnoremap * :<C-u>call <SID>VSetSearch()<CR>//<CR><c-o>
vnoremap # :<C-u>call <SID>VSetSearch()<CR>??<CR><c-o>
+2
source

Source: https://habr.com/ru/post/1524729/


All Articles