Wikia

Vim Tips Wiki

Fast jump to line that matches a regular expression

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

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

created 2005 · complexity basic · author Ivan Tishchenko · version 5.7


I often use the following sequence of commands (g/pattern/# lists each line containing pattern, with line numbers):

:g/pattern/#
[Here I look through the list and choose the line I want.]
[For example, if the line number is 123, I enter:]
:123

I do it so often, that I decided to write the following command. It does the following.

:[range]GJ[ump][!]/pattern/

Syntax is exactly as in :global except that you must NOT specify any commands after /pattern/. You may also use some other delimiter in place of "/", for example, ",".

It prints out numbered list of lines matching the /pattern/ (or not matching, if you add '!'). Then it allows you to enter the number of line you need and moves cursor to that line.

Here is the command.

command! -nargs=* -bang -range=% GJump <line1>,<line2>call GJump('<bang>',<q-args>)
function! GJump(bang, rex) range abort
  let lines = ''
  let i=1
  exe a:firstline ',' a:lastline 'g'.a:bang.a:rex.'echo substitute(strpart(i+1000000,1),"^0\\|\\(0\\@<=0\\)"," ","g") getline(".")|let i=i+1|let lines=lines.(line(".")+1000000)'
  if i==1
    return
  endif
  while 1
    let nmr = input('Type in selected number, or press Enter to exit: ')
    if nmr == ""
      return
    endif
    let nmr = nmr+0 " Make string be number.
    if nmr>0 && nmr<i
      break
    endif
  endwhile
  let nmr = strpart(lines,(nmr-1)*7,7)
  let nmr = nmr-1000000
  silent exe nmr
endfunction

CommentsEdit

This duplicates behavior of the :ilist command, though I think your approach saves a keystroke or two.

:ilist /public

The above will display every line that matches the pattern 'public' (all public methods, for example). The matching line's linenumber will be to the left of the displayed line. You can then type ':<linenumber><CR>' to go to the appropriate line.

You can abbreviate :ilist to :il so you could enter :il /public.


However, :ilist searches the current file, and also all included files. This is not what I want, and is too slow.

Also, the tip makes selecting the wanted line a lot easier.


To put the output of :g/<re>/p or any Ex-command into a register, do the following:

:redir @a> | exe '<Ex-cmd that you want to capture output goes here>' | redir END

Now the output of the command is in register "a". Place in buffer where you want it with "ap.

The reason for wrapping the command in quotes and exe-ing it is because some commands, like :global, see the following | as part of the command, so the redirection doesn't behave properly. :help :redir :help :bar


Around Wikia's network

Random Wiki