(Insert TipProposed template + manual clean) |
JohnBeckett (talk | contribs) (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 |
+ | 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. |
||
⚫ | |||
+ | 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> |
||
+ | |||
⚫ | |||
+ | " the search to a certain scope. |
||
⚫ | |||
⚫ | |||
⚫ | |||
let l:s = line(".") |
let l:s = line(".") |
||
normal % |
normal % |
||
let l:e = line(".") |
let l:e = line(".") |
||
normal % |
normal % |
||
− | if l: |
+ | if l:s < l:e |
+ | return '\%>' . (l:s-1) . 'l\%<' . (l:e+1) . 'l' |
||
⚫ | |||
+ | endif |
||
− | return "" |
||
⚫ | |||
− | else |
||
− | + | 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: |
||
⚫ | |||
− | <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>). |
||
− | |||
− | + | *<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> |
||
− | |||
⚫ | |||
− | <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=[{}} |
*{{help|tag=%5B{|label=[{}} |
||
*{{help|tag=%5B%5B|label=[[}} |
*{{help|tag=%5B%5B|label=[[}} |
||
==Comments== |
==Comments== |
||
− | I don't think remapping '?' (search backwards from cursor) is a good idea. Use something else instead, like <leader>? 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).
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
- :help quote= for help on the expression register '='
- :help [{
- :help [[