2

I have a file, e.g. ~/abc def.mp3 that I want to open from within a Vim buffer that already contains these lines.

If there were no whitespaces in the filename, I could simply open the file with the standard application for that file type by having the cursor on its name somewhere and pressing gx. How could I make this work with a file that includes whitespaces?

I already tried to visually select it and press gx (with or withouth including a \ before the whitespace, surrounding double quotes etc.) but can't figure it out.

3
  • 4
    Visual selection + gx works for me (a line containing ~/foo bar.txt, with visual selection from ~ to the final t, as opposed to a selection to the end of line, pressing gx opens gvim with the appropriate file). – muru May 26 '16 at 0:01
  • 1
    @muru: Hm, 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 press gx 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... – cbaumhardt May 26 '16 at 0:22
  • If you cant open filename with spaces, You can escape spaces in filename by typing \ before each space – SibiCoder May 26 '16 at 2:09
0

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

  1. There is only one file path on a given line and
  2. There is no other text after the file path on that line
  3. 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 :

  1. All file paths are absolute and begin with a /
  2. There is no / in the text before the absolute path
  3. 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.

2
  • 1
    Welcome to Vi and Vim! You may want to consider using nnoremap – D. Ben Knoble Aug 18 '20 at 19:35
  • 1
    Thanks @D.BenKnoble Updated the answer with your suggestion. – Sarfraaz Ahmed Aug 19 '20 at 17:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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