3

If a path is written in form filepath:line (i.e. python exception), I want gf to jump straight to the line.

1

2 Answers 2

7

What you want already exists: :help gF.

Note that gF behaves like gf in the absence of a line number so you can default to gF if you don't want to think too much about which command to use in which case.

--- EDIT ---

Note that Python exceptions are actually in the form:

File "/path/to/file", line 10, in <module>

where the " and the , between the filename and line 1 trip Vim's algorithm. gF works fine for /path/to/filename:10 but not for every scenario.

4
  • Since : is part of ìsfname` on Windows (looks like a bug), to make it working you have to remove it first :set isfname-=: Commented Dec 8, 2022 at 9:03
  • 2
    This is not a bug, because you may have file paths with a drive letter Commented Dec 8, 2022 at 11:05
  • Thanks @Christian but then the solution should include mention the setting change to meet the original requirement. Commented Dec 8, 2022 at 14:52
  • @VivianDeSmedt, the suggestion of removing : from isfname is not a good idea, for the reason given by @ChristianBrabandt. It could be done temporarily in a wrapper but that's really not the spirit of this answer.
    – romainl
    Commented Dec 8, 2022 at 15:41
0

This is the implementation. I haven't completed it for vmap, though not complicated.

I didn't check it is really a line number, so be careful.

function! DoGF()
    let f=expand('<cfile>')
    let arr=split(f,':')
    if len(arr)>1
        if  filereadable(expandcmd(arr[0]))==0
            echoerr "File not exists"
            return
        endif
        :exe ':e '.arr[0]
        :exe ':'.arr[1]
    else
       norm! gf 
    endif
endfunction

nmap gf :call DoGF()<CR>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.