No edit summary |
mNo edit summary |
||
Line 58: | Line 58: | ||
, September 21, 2005 4:24 |
, September 21, 2005 4:24 |
||
---- |
---- |
||
− | I haven't seen a tip this generally useful in quite a while. I've been using g/rex/p for years, but I hadn't thought of using # instead of p. |
||
− | |||
I was happy to see that g/rex/# also works in old vi. |
I was happy to see that g/rex/# also works in old vi. |
||
Line 65: | Line 63: | ||
, September 21, 2005 6:57 |
, September 21, 2005 6:57 |
||
---- |
---- |
||
− | Kool! |
||
− | I'd even forgotten about :g//# have added this to my best tips [[VimTip305]] and have incorporated the function into my .vimrc. |
||
− | |||
Could it be adapted to [I (see :help [I ) ?? |
Could it be adapted to [I (see :help [I ) ?? |
||
Revision as of 22:14, 9 August 2007
Tip: #996 - Fast jump to line that matches a regular expression
Created: September 21, 2005 3:38 Complexity: basic Author: Ivan Tishchenko Version: 5.7 Karma: 39/14 Imported from: Tip#996
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 at least 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 you have this command. Happy vimming!
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)' 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
Add following lines before "while 1" to handle cases when no lines found:
if i==1 return endif
Ivan Tishchenko , September 21, 2005 4:24
I was happy to see that g/rex/# also works in old vi.
jaldri1 at gl dot umbc dot edu , September 21, 2005 6:57
Could it be adapted to [I (see :help [I ) ??
zzapper , September 21, 2005 8:26
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.
none , September 21, 2005 12:41
not to mention that :ilist can be abbreviated as :il so you could use:
:il /regexp
didier.prophete--AT--gmail.com , September 22, 2005 8:10
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.
Ivan Tishchenko , September 26, 2005 3:33
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.
Ivan Tishchenko , September 26, 2005 3:37
Where do you put the function?
jason.hopkins--AT--med.ge.c0m , September 28, 2005 15:13
I put it in my .vimrc.
But I guess there's better ways
zzapper , September 29, 2005 10:13
The better way is to put this function as plugin. Type
:help add-global-plugin
to see, how you could do that.
Ivan Tishchenko , October 3, 2005 8:13
I didn't know about g/re/# Neat!
Is there any way to put the output of a g/ command into a buffer? That's what I really want.
Anonymous , October 20, 2005 9:20
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