5

There is a file with name ./folderA/folderB/file..

And there is text:

./folderA/folderB/file.:abcdefg

When I move the cursor on letter B and type <c-w>f, it says

E447: Can't find file "./folderA/folderB/file" in path

Note that the file name is file.. There is a dot at the end of the file name but no extension name.

How can I open such a file? I have many files with similar names, so I hope to minimize keystrokes. I'm using Ubuntu.

4

Another option is to set includeexpr. From :help includeexpr:

                                                *'includeexpr'* *'inex'*
'includeexpr' 'inex'    string  (default "")
                    local to buffer
                    {not in Vi}
                    {not available when compiled without the
                    |+find_in_path| or |+eval| features}
    Expression to be used to transform the string found with the 'include'
    option to a file name.  Mostly useful to change "." to "/" for Java:
            :set includeexpr=substitute(v:fname,'\\.','/','g')
    The "v:fname" variable will be set to the file name that was detected.

    Also used for the |gf| command if an unmodified file name can't be
    found.  Allows doing "gf" on the name after an 'include' statement.
    Also used for |<cfile>|.

    The expression will be evaluated in the |sandbox| when set from a
    modeline, see |sandbox-option|.

    It is not allowed to change text or jump to another window while
    evaluating 'includeexpr' |textlock|.

The "Also used" paragraph is what will be useful here. When you do a gf and vim can't find the file, it places the name it tried to read in v:fname and evaluates the expression in includeexpr. it then tries to read a file with the resulting name. So if you do a:

:set includeexpr=v:fname.'.'

then do a gf when on file., vim tries to read file, which fails. It then evaluates v:fname.'.', with v:fname set to file, which just appends a period to the contents of v:fname and returns file.. vim then tries to read that, which succeeds.

Note that vim will still try to read file, so if you have both file and file., vim will read file, but I'd argue having both file and file. is just asking for trouble. :-)

If you only need to do this for certain file types, you could place it in an autocmd:

autocmd BufNewFile,BufRead *.dots set includeexpr=v:fname.'.'

where you only want to have vim look for file. in files ending in .dots (for example).

6

From :help gf:

Trailing punctuation characters ".,:;!" are ignored.

So I suppose the dot at the end is not considered as part of the filename but as punctuation.

I don't know if it can be done with fewer keystrokes, but to open this file while the cursor is on B, I would type:

viWF.<c-w>f

  • viW = visually select characters inside big word
  • F. = move the end of the selection to the previous dot
  • <c-w>f = open file whose path is visually selected in a split

To save some keystrokes you could also define a mapping :

nnoremap <leader>wf viWF.<c-w>f

Then type <leader>wf while the cursor is on the file path you described.

  • 1
    @user3813057 You could also set includeexpr. In my .vimrc I have set includeexpr=substitute(v:fname,'[-:]\\d\\+[-:]\\?$','',''), which strips trailing numbers when a gf fails (for dealing with things like error messages in the form <file>-<line>: error). You could do something similar here. – blm Dec 20 '15 at 19:58
  • @blm If you have the time, you should post it as an answer, maybe explaining a little how it works. It probably deserves its own answer. – saginaw Dec 20 '15 at 20:21
  • Ok, turned it into an answer. – blm Dec 20 '15 at 22:01
1

The accepted answer is very, very good for a detailed, script-oriented way to accomplish a micro-optimization of a corner case, but many people in the same scenario don't want to be forced into a vimrc customization and those people usually also wouldn't have "many, many files" where saving two keystrokes is important and they still want a general and simple way to accomplish what you're describing. So the simple and obvious solution for those people, and perhaps for you as well, is Vim's Visual Mode gf.

Do a Visual Mode select of the text representing the weird file name, for example let's say with vww or v$, then type gf. Done. Visual Mode gf exists to cover the situation you're describing. Sure it changes a 2 keystroke combination into a 4 or 5 one, but it also works perfectly and visually and it works for this case as well as other similar cases, like let's say spaces in the file name.

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.