Vim Tips Wiki
(Insert TipProposed template + manual clean)
(Tweak wording and function, and provide more conventional mappings)
Line 8: Line 8:
 
|version=7.0
 
|version=7.0
 
}}
 
}}
Often a programmer will want to search for something only within a certain program scope. The following function and mappings allow this behavior.
+
Often a programmer will want to search for something only within a certain program scope, for example, within a function. The following code provides that behavior.
   
 
<pre>
 
<pre>
  +
" Search within top-level block for word at cursor.
" Return a string to place at the beginning of a search to limit it to a certain scope
 
  +
nnoremap <Leader>[ "ayiw/<C-R>=ScopeSearch("[[")<CR><C-R>a<CR>
" Pass in a command to jump to the beginning of the scope desired.
 
  +
" Search within current block for word at cursor.
function Scope_search( navigator )
 
  +
nnoremap <Leader>{ "ayiw/<C-R>=ScopeSearch("[{")<CR><C-R>a<CR>
exec ':normal '.a:navigator
 
  +
" Search within current top-level block for user-entered text.
  +
nnoremap <Leader>/ /<C-R>=ScopeSearch("[[")<CR>
  +
 
" Return a string to place at the beginning of a search to limit
  +
" the search to a certain scope.
 
" navigator is a command to jump to the beginning of the desired scope.
 
function! ScopeSearch(navigator)
 
exec 'normal ' . a:navigator
 
let l:s = line(".")
 
let l:s = line(".")
 
normal %
 
normal %
 
let l:e = line(".")
 
let l:e = line(".")
 
normal %
 
normal %
if l:e <= l:s
+
if l:s < l:e
  +
return '\%>' . (l:s-1) . 'l\%<' . (l:e+1) . 'l'
echo "cannot find the searching scope by command " . a:navigator . " and %"
 
  +
endif
return ""
 
 
echo "Cannot find search scope with command " . a:navigator . " %"
else
 
return '\%>'.l:s.'l\%<'.l:e.'l'
+
return ""
 
endfunction
 
endfunction
 
</pre>
 
</pre>
   
  +
With the defaults, the <Leader> key is backslash. With the mappings suggested above, you would put the cursor on a word in a C function, then press:
To start a search within the current block, and allow the user to enter a search term:
 
 
*<tt>\[</tt> to search for the word, but only within the current function.
<pre>nnoremap ? /<C-R>=Scope_search("[{")<cr> " search in the {}</pre>
 
  +
*<tt>\{</tt> to search for the word, but only within the current block (<tt>{...}</tt>).
 
To search for the word under the cursor within the top-level block:
+
*<tt>\/</tt> to search for whatever text you enter, but only within the current block.
<pre>nnoremap ?[[ "ayiw/<C-R>=Scope_search("[[")<cr><C-R>a<cr> " search for the cursor word in a function</pre>
 
 
To search for the word under the cursor within the current block only:
 
<pre>nnoremap ?[{ "ayiw/<C-R>=Scope_search("[{")<cr><C-R>a<cr> " search for the cursor word in a {}</pre>
 
   
  +
This procedure will work for any program, such as C or C++, where a code block starts with '{' and ends with '}', and where a function starts with '{' at the left margin.
These mappings make use of the '''expression register "='''. See {{help|id=quote=}} for details.
 
   
 
==References==
 
==References==
*{{help|id=quote=}}
+
*{{help|id=quote=}} for help on the expression register '='
 
*{{help|tag=%5B{|label=&#91;{}}
 
*{{help|tag=%5B{|label=&#91;{}}
 
*{{help|tag=%5B%5B|label=&#91;&#91;}}
 
*{{help|tag=%5B%5B|label=&#91;&#91;}}
   
 
==Comments==
 
==Comments==
I don't think remapping '?' (search backwards from cursor) is a good idea. Use something else instead, like &lt;leader&gt;? if you really want to use the '?'.
 
   
 
----
 
----
 
[[Category:C]]
 
[[Category:C]]
  +
[[Category:C plus plus]]
 
[[Category:Searching]]
 
[[Category:Searching]]

Revision as of 07:49, 12 January 2008

Proposed tip Please edit this page to improve it, or add your comments below (do not use the discussion page).

Please use new tips to discuss whether this page should be a permanent tip, or whether it should be merged to an existing tip.
created November 12, 2007 · complexity basic · author Anon · version 7.0

Often a programmer will want to search for something only within a certain program scope, for example, within a function. The following code provides that behavior.

" Search within top-level block for word at cursor.
nnoremap <Leader>[ "ayiw/<C-R>=ScopeSearch("[[")<CR><C-R>a<CR>
" Search within current block for word at cursor.
nnoremap <Leader>{ "ayiw/<C-R>=ScopeSearch("[{")<CR><C-R>a<CR>
" Search within current top-level block for user-entered text.
nnoremap <Leader>/ /<C-R>=ScopeSearch("[[")<CR>

" Return a string to place at the beginning of a search to limit
" the search to a certain scope.
" navigator is a command to jump to the beginning of the desired scope.
function! ScopeSearch(navigator)
  exec 'normal ' . a:navigator
  let l:s = line(".")
  normal %
  let l:e = line(".")
  normal %
  if l:s < l:e
    return '\%>' . (l:s-1) . 'l\%<' . (l:e+1) . 'l'
  endif
  echo "Cannot find search scope with command " . a:navigator . " %"
  return ""
endfunction

With the defaults, the <Leader> key is backslash. With the mappings suggested above, you would put the cursor on a word in a C function, then press:

  • \[ to search for the word, but only within the current function.
  • \{ to search for the word, but only within the current block ({...}).
  • \/ to search for whatever text you enter, but only within the current block.

This procedure will work for any program, such as C or C++, where a code block starts with '{' and ends with '}', and where a function starts with '{' at the left margin.

References

Comments