Wikia

Vim Tips Wiki

Search visually

Talk0
1,613pages on
this wiki
Revision as of 06:10, July 13, 2012 by JohnBot (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Tip 1151 Printable Monobook Previous Next

created 2006 · complexity intermediate · author Gerald Lai · version 6.0


To display all the lines where the word under the cursor occurs, simply do in Normal mode: [I

This can be useful to find a count of lines of search occurrences. Each line displayed is numbered. Try it to see for yourself.

In order to jump to the <n>th line of occurrence, do: <n>[<Tab>

This means type in the <n>umber first, hit '[', and then the Tab button. If <n> is not typed, the jump defaults to the line where the first (uncommented) word appears.

The function and mappings below allow for [I and <n>[<Tab> to work in visual mode too, so that the search will be done for the visual highlight.

Place in your vimrc:

nmap <silent>[I :<C-u>cal OSearch("nl")<CR>
nmap <silent>[<Tab> :<C-u>cal OSearch("nj")<CR>
vmap <silent>[I :<C-u>cal OSearch("vl")<CR>
vmap <silent>[<Tab> :<C-u>cal OSearch("vj")<CR>

function! OSearch(action)
  let c = v:count1
  if a:action[0] == "n"
    let s = "/\\<".expand("<cword>")."\\>/"
  elseif a:action[0] == "v"
    execute "normal! gvy"
    let s = "/\\V".substitute(escape(@@, "/\\"), "\n", "\\\\n", "g")."/"
    let diff = (line2byte("'>") + col("'>")) - (line2byte("'<") + col("'<"))
  endif
  if a:action[1] == "l"
    try
      execute "ilist! ".s
    catch
      if a:action[0] == "v"
        normal! gv
      endif
      return ""
    endtry
    let c = input("Go to: ")
    if c !~ "^[1-9]\\d*$"
      if a:action[0] == "v"
        normal! gv
      endif
      return ""
    endif
  endif
  let v:errmsg = ""
  silent! execute "ijump! ".c." ".s
  if v:errmsg == ""
    if a:action[0] == "v"
      " Initial version
      " execute "normal! ".visualmode().diff."\<Space>"
      " Bug fixfor single character visual [<Tab>:
      if diff
        execute "normal! ".visualmode().diff."\<Space>"
      else
        execute "normal! ".visualmode()
      endif
    endif
  elseif a:action[0] == "v"
    normal! gv
  endif
endfunction

ReferencesEdit

CommentsEdit

Around Wikia's network

Random Wiki