Wikia

Vim Tips Wiki

Changes: Easy "Search Only In a Visually Selected Range"

Edit

Back to page

(Insert TipProposed template + minor manual clean)
(Change <tt> to <code>, perhaps also minor tweak.)
 
Line 100: Line 100:
 
==Sample usage==
 
==Sample usage==
 
After visually selecting a range, press <Esc> and then press:
 
After visually selecting a range, press <Esc> and then press:
*<tt><Leader>/</tt> to search forward for a string
+
*<code><Leader>/</code> to search forward for a string
*<tt><Leader>?</tt> to search backward for a string
+
*<code><Leader>?</code> to search backward for a string
*<tt><Leader>n</tt> for the next occurence
+
*<code><Leader>n</code> for the next occurence
*<tt><Leader>p</tt> for the previous occurence of the search
+
*<code><Leader>p</code> for the previous occurence of the search
   
 
where the Leader key by default is backslash.
 
where the Leader key by default is backslash.
   
 
==Advantages==
 
==Advantages==
*Remembers the visually slected region so can do a <tt><Leader>n</tt> or <tt><Leader>p</tt> again at any time in normal mode.
+
*Remembers the visually slected region so can do a <code><Leader>n</code> or <code><Leader>p</code> again at any time in normal mode.
*Keeps your regular "<tt>/</tt>" search different from the visually selected one, so can mix your n/p and <Leader>n and <Leader>p shortcuts.
+
*Keeps your regular "<code>/</code>" search different from the visually selected one, so can mix your n/p and <Leader>n and <Leader>p shortcuts.
   
 
==Disadvantage==
 
==Disadvantage==
If you want normal searches ("<tt>/</tt>") and visually selected searches to use the same key ('/', 'n', 'p'), then you will not be able to use this script.
+
If you want normal searches ("<code>/</code>") and visually selected searches to use the same key ('/', 'n', 'p'), then you will not be able to use this script.
   
 
==Comments==
 
==Comments==

Latest revision as of 08:21, July 14, 2012

Recently created tip

We have not yet decided whether to keep this tip as its own page or merge it somewhere else. If you have a suggestion on the tip content, please edit this page or add your comments below (do not use the discussion page).

Please discuss whether to keep this as a new tip, or whether to merge it into an existing tip, on the new tips discussion page.
created January 17, 2012 · complexity basic · author Dinshaw H Dastoor · version 6.0

Very often, we want to search for a symbol (say a local variable) only in a specified range of lines that are visually selected for example a function or in general any visual region so as to confine our search.

Contents

Already existsEdit

A tip of this nature already exists: Search only over a visual range.

This tip is slightly different than the above because, here you visually select a range of lines, then press Esc and run a shortcut to search.

How to installEdit

Please source the following lines into your .vimrc file:

let g:sbr = 1
let g:sbc = 1
let g:ser = line("G")
let g:sec = col("G")
let g:searchstr = "searchfirst"
let g:sdir = "f"

function GetBlockInput()
  let g:searchstr = input('find what?:')
  let g:sbr = line("'<")
  let g:sbc = col("'<")
  let g:ser = line("'>")
  let g:sec = col("'>")
endfunction

function FindFirstRegion()
  call GetBlockInput()
  call FindNextRegion()
endfunction

function FindFirstRegionBack()
  call GetBlockInput()
  call FindNextRegionBack()
endfunction

function FindNextRegionWork()
  let l:fout = 0
  let l:fin = 0
  let l:crw = line(".")
  let l:ccl = col(".")
  let l:frw = -1
  let l:fcl = -1
  if g:sdir == "f"
    let l:flags = "w"
  else
    let l:flags = "wb"
  endif
  call search(g:searchstr, l:flags)
  let l:srw = line(".")
  let l:scl = col(".")
  while l:srw != 0 && (l:srw != l:frw || l:scl != l:fcl)
    if (l:srw == g:sbr && l:scl >= g:sbc) || (l:srw == g:ser && l:scl <= g:sec) || (l:srw > g:sbr && l:srw < g:ser)
      let l:fin = 1
      break
    else
      if l:fout == 0
        let l:fout = 1
        let l:frw = l:srw
        let l:fcl = l:scl
      endif
    endif
    call search(g:searchstr, l:flags)
    let l:srw = line(".")
    let l:scl = col(".")
  endwhile
  if l:fout == 1 && l:fin == 0
    execute "normal " . l:crw . "G"
    execute "normal " . l:ccl . "|"
  endif
endfunction

function FindNextRegion()
  let g:sdir = "f"
  call FindNextRegionWork()
endfunction

function FindNextRegionBack()
  let g:sdir = "b"
  call FindNextRegionWork()
endfunction

nnoremap <Leader>/ :call FindFirstRegion()<CR>
nnoremap <Leader>? :call FindFirstRegionBack()<CR>
nnoremap <Leader>n :call FindNextRegion()<CR>
nnoremap <Leader>p :call FindNextRegionBack()<CR>

Sample usageEdit

After visually selecting a range, press <Esc> and then press:

  • <Leader>/ to search forward for a string
  • <Leader>? to search backward for a string
  • <Leader>n for the next occurence
  • <Leader>p for the previous occurence of the search

where the Leader key by default is backslash.

AdvantagesEdit

  • Remembers the visually slected region so can do a <Leader>n or <Leader>p again at any time in normal mode.
  • Keeps your regular "/" search different from the visually selected one, so can mix your n/p and <Leader>n and <Leader>p shortcuts.

DisadvantageEdit

If you want normal searches ("/") and visually selected searches to use the same key ('/', 'n', 'p'), then you will not be able to use this script.

CommentsEdit

Around Wikia's network

Random Wiki