If a path is written in form filepath:line
(i.e. python exception), I want gf
to jump straight to the line.
2 Answers
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.
-
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 -
2This 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
:
fromisfname
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.– romainlCommented Dec 8, 2022 at 15:41
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>