8

Is there any way to make the special symbol % work with path names on the command line? For instance, I'd like to do:

:diffsplit Path\to\other\directory\%

So that I can do a @: to quickly compare files of the same name. However, this just opens up a new buffer called %.

Of course I could do:

:exe 'diffsplit Path\to\other\directory\'.expand('%')

But this is cumbersome as I can't tab complete directories when typing the command.

Is there any other better way to do this?

Here's a related question, but it doesn't address my situation exactly.

Edit: Just to clarify, I would like to be able to re-use the command exactly for different files. So a solution that simply autofills the current file in the command line will not work. The same command needs to work for different file names.

4
  • You can use a cmap for % such that it expands to filename, that might help :D
    – SibiCoder
    Jun 29, 2016 at 17:14
  • This has the same problem that Kent's solution has. As soon as you press %, it will fill to the filename, but then when I want to reuse that line, the old filename will still be present. I would like the % to dynamically insert the current filename, whatever that happens to be at the time without having to retype it.
    – Tumbler41
    Jun 29, 2016 at 18:01
  • 2
    :diffsplit Path/to/other/directory/%:t Jun 29, 2016 at 18:21
  • Wow, I thought I tried that, but you're right that works! Thanks! If you want to put it in an answer I'll accept it.
    – Tumbler41
    Jun 29, 2016 at 18:36

3 Answers 3

10

Special characters (see :h cmdline-special) and filename modifiers (see :h filename-modifiers) are expanded on the command line, so:

:diffsplit Path/to/other/directory/%:t
1

So it turns out that it's a stupid Windows problem. The remedy to this is to simply use forward slashes in the path name. i.e. :diffsplit Path/to/other/directory/% works just fine.

I've remedied this on my system by using the option set shellslash which defaults to using forward slashes even on Windows.

2
  • Yeah, the backslash is used both to escape the special meaning of %, and is the default path separator on Windows. Vim can't tell which you had in mind. Sep 9, 2016 at 7:09
  • See, I thought of that. So I figured that I could just do a directory\\% but that still didn't work. IDK, but I guess it doesn't matter since forward slashes solve the problem.
    – Tumbler41
    Sep 9, 2016 at 14:29
0

I don't know if this is ok for you:

:diffsplit Path/to/somewhere/<c-r>%

Just one more keystroke to your original, the <C-r>, in order to fill the % register value.

2
  • This still doesn't allow for using @: for future files as it "hard codes" the current file name.
    – Tumbler41
    Jun 29, 2016 at 16:24
  • then wrap a custom command like :Mydiff /path/to/there/ so the path could be tab completed. just pass this to your command/function with exec
    – Kent
    Jun 29, 2016 at 16:26

Your Answer

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

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