Vim Tips Wiki
(Change to TipImported template + severe manual clean)
(Move categories to tip template)
Line 9: Line 9:
 
|version=5.7
 
|version=5.7
 
|rating=39/14
 
|rating=39/14
  +
|category1=
  +
|category2=
 
}}
 
}}
 
Last time I often use the following sequence of commands:
 
Last time I often use the following sequence of commands:
Line 84: Line 86:
 
</pre>
 
</pre>
   
Now the output of the command is in register "a". Place in buffer where you want it with "ap.
+
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.
 
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.

Revision as of 05:11, 25 April 2008

Tip 996 Printable Monobook Previous Next

created September 21, 2005 · complexity basic · author Ivan Tishchenko · version 5.7


Last time I often use the following sequence of commands:

g/rex/#
<here I look through the list and choose the line I want, then I type its number as next command>
<some-line-number>

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

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

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

It prints out numbered list of lines matching the /rex/ (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 just hit <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

Comments

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.


Not to mention that :ilist can be abbreviated as :il so you could use:

:il /regexp

You see, :ilist searches not only in current file, but also in all includes. This is too slow in my particular case. And, anyway, I do not need any matches outside the current file, when I use :GJ.


And, if you will read the tip all through, you'll see, that that was annoying necessity of typing :<line-number><cr> which made me design this command. When repeated often, this :<line-number><cr> is quite tiring; imagine that you do that each 1-2 minute, and you'll understand what I mean.


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.

See :help :redir, :help :bar.