Wednesday, May 18, 2011

Re: 'commandline' vs ':e' for jump to line (FEAT. REQ)

On 05/18/2011 03:32 AM, Ben Schmidt wrote:
> Here's a simple solution for .vimrc that might work:
>
> command! -nargs=1 -bang Edit call FileAndLine("e","<bang>",<f-args>)
> command! -nargs=1 -bang Split call FileAndLine("sp","<bang>",<f-args>)
> function! FileAndLine(cmd,bang,arg)
> let file=matchstr(a:arg,'.\{-}\ze:')
> let line=matchstr(a:arg,':\zs\d\+')
> exec a:cmd.a:bang." +".line." ".fnameescape(file)
> endfunction
>
> Then :Edit abc.vim:12 will turn into :e +12 abc.vim and :Split the same,
> but into :sp not :e. Passes on a ! too.
>
> Not tested much, and may have nasty edge cases, but should be a good
> start.

Edge-cases that stick out to me involve non-digit text after the
colon such as "C:\path\to\file.txt",
"resident_evil:afterlife.mov" or "rockets:321blastoff.swf" This
could be remedied by tightening the regexps:

let file=matchstr(a:arg,'^.*\ze:\d\+$')
let line=matchstr(a:arg,'^.*:\zs\d\+')

And there are failure conditions if the filename doesn't contain
a colon, so you might put in a check something like

if file==''
let file=a:arg
endif
if line==''
let line=1
endif

before issuing the :exec call.

-tim


--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: