LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices



Reply
 
Search this Thread
Old 02-10-2012, 09:00 AM   #1
dedec0
Member
 
Registered: May 2007
Posts: 57

Rep: Reputation: 3
Question 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.
 
Old 02-12-2012, 08:42 PM   #2
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
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
 
Old 02-29-2012, 02:19 PM   #3
dedec0
Member
 
Registered: May 2007
Posts: 57

Original Poster
Rep: Reputation: 3
Quote:
Originally Posted by chrism01 View Post
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.
 
Old 02-29-2012, 02:51 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Quote:
Originally Posted by dedec0 View Post
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
 
Old 02-29-2012, 03:04 PM   #5
dedec0
Member
 
Registered: May 2007
Posts: 57

Original Poster
Rep: Reputation: 3
Arrow

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
 
Old 02-29-2012, 03:07 PM   #6
dedec0
Member
 
Registered: May 2007
Posts: 57

Original Poster
Rep: Reputation: 3
Quote:
Originally Posted by Tinkster View Post
That's easy ...
It is, I was missing it all the time... thanks, anyway.
 
  


Reply

Tags
vim


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] VIM - Visual Mode problem daghenningsorbo Linux - Software 6 08-08-2011 05:49 PM
Visual mode of VIM jcky Linux - Newbie 2 01-03-2010 05:18 AM
Stupid Question: Microsoft Visual Studio 6.0 (A.K.A. Visual Basic 6) Will it run? S0RD3N Linux - Software 8 05-07-2008 09:42 PM
VIM insert before a visual block... pld Linux - Software 0 06-22-2005 12:36 PM
Using Visual in VIM editor manikantha Linux - General 4 06-10-2005 09:55 AM


All times are GMT -5. The time now is 03:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration