LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   VIM question: searching in functions and visual areas (https://www.linuxquestions.org/questions/linux-software-2/vim-question-searching-in-functions-and-visual-areas-928653/)

dedec0 02-10-2012 08:00 AM

VIM question: searching in functions and visual areas
 
How would I search when:

1- I have some visually selected text (on one or more lines) and want to search for it in the rest of the file. Like * and # do for words.

2. I have some visually selected text and want to search within the selected area.

3- I am within a C function and want to search for some text only within the lines it lies.

I found \%>23l and \%<23l, but then I need to look the lines the function begins and ands, anyway. Maybe there is a quicker (automatic) way.

It is easy to visually select the function body, so an answer to question 2 might solve this one too.

chrism01 02-12-2012 07:42 PM

Unfortunately, I don't have the actual answer(s), but have you considered vim macros http://vimdoc.sourceforge.net/htmldoc/usr_10.html#10.1

dedec0 02-29-2012 01:19 PM

Quote:

Originally Posted by chrism01 (Post 4600880)
Unfortunately, I don't have the actual answer(s), but have you considered vim macros http://vimdoc.sourceforge.net/htmldoc/usr_10.html#10.1

I don't know much about macros in Vim, I used just the simplest ones. I imagined that it could be done, but without a clue on how it would be done.

Tinkster 02-29-2012 01:51 PM

Quote:

Originally Posted by dedec0 (Post 4598959)
How would I search when:
2. I have some visually selected text and want to search within the selected area.

That's easy ... select your block, press ESC

The carry on with
/\%Vpattern



Cheers,
Tink

dedec0 02-29-2012 02:04 PM

Quote:

2- I have some visually selected text and want to search within the selected area.
Somehow I missed \%V from pattern.txt help. Answering myself, do:

- visually select the area
- press ESC
- now, make any normal search but including \%V on its pattern and results will be within that visual area

Example: /printf\%V

Quote:

1- I have some visually selected text (on one or more lines) and want to search for it in the rest of the file. Like * and # do for words.
Check this tip 171 for Vim in Wikia.

It is a plugin (quote: "complexity: basic", LOL) to make searching visually selected things with *, #, n and N.

Save the code below in a file ~/.vim/plugin/vsearch.vim or $HOME/vimfiles/plugin/vsearch.vim (Win). Check its features!
Code:

" Features
" - Press * to search forwards for selected text, or # to search backwards.
" - As normal, press n for next search, or N for previous.
" - Handles multiline selection and search.
" - Whitespace in the selection matches any whitespace when searching (searching for "hello world" will also find "hello" at the end of a line, with "world" at the start of the next line).
" - Each search is placed in the search history allowing you to easily repeat previous searches.
" - No registers are changed.
" - A global variable (g:VeryLiteral) controls whether selected whitespace matches any whitespace (by default, VeryLiteral is off, so any whitespace is found).
" - Type \vl to toggle VeryLiteral to turn whitespace matching off/on (assuming the default backslash leader key).
" - When VeryLiteral is off, any selected leading or trailing whitespace will not match newlines, which is more convenient, and avoids false search hits.


" Search for selected text.
" http://vim.wikia.com/wiki/VimTip171
let s:save_cpo = &cpo | set cpo&vim
if !exists('g:VeryLiteral')
  let g:VeryLiteral = 0
endif

function! s:VSetSearch(cmd)
  let old_reg = getreg('"')
  let old_regtype = getregtype('"')
  normal! gvy
  if @@ =~? '^[0-9a-z,_]*$' || @@ =~? '^[0-9a-z ,_]*$' && g:VeryLiteral
    let @/ = @@
  else
    let pat = escape(@@, a:cmd.'\')
    if g:VeryLiteral
      let pat = substitute(pat, '\n', '\\n', 'g')
    else
      let pat = substitute(pat, '^\_s\+', '\\s\\+', '')
      let pat = substitute(pat, '\_s\+$', '\\s\\*', '')
      let pat = substitute(pat, '\_s\+', '\\_s\\+', 'g')
    endif
    let @/ = '\V'.pat
  endif
  normal! gV
  call setreg('"', old_reg, old_regtype)
endfunction

vnoremap <silent> * :<C-U>call <SID>VSetSearch('/')<CR>/<C-R>/<CR>
vnoremap <silent> # :<C-U>call <SID>VSetSearch('?')<CR>?<C-R>/<CR>
vmap <kMultiply> *

nmap <silent> <Plug>VLToggle :let g:VeryLiteral = !g:VeryLiteral
  \\| echo "VeryLiteral " . (g:VeryLiteral ? "On" : "Off")<CR>
if !hasmapto("<Plug>VLToggle")
  nmap <unique> <Leader>vl <Plug>VLToggle
endif
let &cpo = s:save_cpo | unlet s:save_cpo


dedec0 02-29-2012 02:07 PM

Quote:

Originally Posted by Tinkster (Post 4615305)
That's easy ...

It is, I was missing it all the time... thanks, anyway.


All times are GMT -5. The time now is 08:17 PM.