This is more of a workaround until gx
is fixed.
I created a map key that will do the job. The trick was to surround the path with "
[ double quotes ]
Disclaimer : This uses open
command that works only on a Mac
There are some gotchas that you need to keep in mind while using this
The assumptions are that
- There is only one file path on a given line and
- There is no other text after the file path on that line
- I have only assumed *nix like paths
Scenario 1 : File name starting after a hypen
Ex : - /path/ to /file with spaces.pdf
I had the above line in my markdown file. So I created a specific map key for this scenario
:nnoremap <C-o> ^wy$:! open "^R""^M^M
- The
^w
takes the cursor to the first word from the beginning of the line
y$
copies the rest of the line to clipboard
! open
: Please make sure you keep that space between !
and open
^R"
inserts the data present in clipboard to the command mode
- Note how the copied text is surrounded by a pair of
"
- This was needed to take care of the spaces in the file path
So, now with the cursor on any character of the file path, pressing <ctrl-o>
opens me the file with the associated application
Scenario 2 : File name starting anywhere in a line
Ex : some text : /path/ to /file with spaces.pdf
Assumption here is that :
- All file paths are absolute and begin with a
/
- There is no
/
in the text before the absolute path
- There is only one
.
in the file name
:nnoremap <C-p> ^f/vf.ey:! open "^R""^M^M
f/
moves the cursor to the first /
on the line
f.
moves the cursor to the first .
within the absolute file path
e
takes the cursor to the end of file path
y
copies the text that is blocked by the v
: visual mode
The rest is same as Scenario 1.
So, now with the cursor on any character of the file path, pressing <ctrl-p>
opens me the file with the associated application
Ofcourse, instead of using <ctrl-o>
and <ctrl-p>
, you can specify map keys that suit you.
gx
works for me (a line containing~/foo bar.txt
, with visual selection from~
to the finalt
, as opposed to a selection to the end of line, pressinggx
opens gvim with the appropriate file).gx
does not work for me when I am in visual mode, I just tried your example with a text file.gf
on the other hand does work. When I pressgx
in visual mode, I get the usual error sound GNOME produces when an operation fails. Any ideas what might cause this behaviour? Perhaps I have to go through my list of plugins...