Vim Tips Wiki
(Move categories to tip template)
m (Remove link to 116 which will be deleted; replace with 9; add category)
Line 8: Line 8:
 
|version=6.0
 
|version=6.0
 
|rating=20/8
 
|rating=20/8
|category1=
+
|category1=Searching
 
|category2=
 
|category2=
 
}}
 
}}
Line 75: Line 75:
   
 
==References==
 
==References==
  +
*[[VimTip9|Displaying a variable/macro definition]]
*[[VimTip116]]
 
*[[VimTip563]]
+
*[[VimTip563|List lines with keyword and prompt for jump]]
 
*{{help|tag=%5B%5B|label=[[}}
 
*{{help|tag=%5B%5B|label=[[}}
 
*{{help|:ilist}}
 
*{{help|:ilist}}
Line 82: Line 82:
   
 
==Comments==
 
==Comments==
 
----
 

Revision as of 08:41, 18 May 2008

Tip 1151 Printable Monobook Previous Next

created February 24, 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 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

References

Comments