11

How can I bring up the path to the current file in the command line, so as to type a similar file name? I don't want an abbreviation that would be replaced by the current file name, I want to edit the file name, complete with its directory path. For example, I want to :edit a file with a similar name, or in a similar directory, e.g.

vim src/submodule/frontend/frobnicate2.c

and now I want to edit ../trunk/src/submodule/frontend/frobnicate.c, and I only want to insert the ../trunk and remove the 2, not retype the full path.

I can of course drop back to the shell and use its command history, or I could copy-paste the path through the GUI, but I'm looking for a more efficient way that doesn't require leaving Vim.

4 Answers 4

14

In addition to inserting the % register as you've discovered yourself, you can also use :edit %<Tab>.

From :help cmdline-special:

In Ex commands, at places where a file name can be used, the following
characters have a special meaning.  These can also be used in the expression
function `expand()`.

        %       Is replaced with the current file name.           :_% c_%

So you can, for example, use:

:edit %.orig

to edit file.txt.orig. For your example, you'd type :e ../trunk/%, press <Tab> to get the file name with the relative path, and remove the 2 before pressing <Enter>.

You can add some modifiers to the path to modify it; I found :h to be particularly useful, it gets the "dirname" (or "head" in Vim-speak") of the current path:

:edit file/in/a/deeply/nested/subdirectory/here.txt
:edit %:h/in_same_dir_as_above.txt

You can even use :h multiple times. For example:

:edit %:h:h:h/file.txt

Will open file/in/a/deeply/file.txt

There's also :p which gives you the full path and which could be useful in your case: type :e %:p, press <Tab> and edit the path as desired. You can even use multiple modifiers, eg: :e %:p:h.

See :help filename-modifiers for the full list.

Note: if you <Tab>, Vim will expand the %. If you use <C-d>, Vim won't expand the % (but does substitute the % while completing the path).

5

Oh. Search for “file name” in :help :! It's simple: CTRL-R % to insert the special register %, meaning the current file name.

1
  • 1
    So, building on your answer: to get the full path as the OP requested, you'd do something like Ctrl-r =expand('%:p'). This uses the = register.
    – lcd047
    Commented Jun 3, 2015 at 5:25
3

If you have set your vim path variable, you have the full power of the :find command, namely tab-completion of file names (without the need to specify the directory part!)

In your case, something like

set path=./**

may be all you need for :fin frob<tab> to work. For the gory details, :help find, :help file-searching.

0

Here is a mapping you can try:

cnoremap <a-q> <c-c>:let @a=expand('%')<CR>:<Up><c-r>a

This saves the current command then sets register 'a' to the name of the current file. Then the last command is recalled and appended with the 'a' register contents. Of course if you have something useful in the 'a' register this will overwrite it.

You may want to change the mapped characters to something else. I like to use the Alt key but it doesn't work in some terminals.

As Carpetsmoker pointed out in his answer you can change the function call to

expand('%:p')

to force the full path.

1
  • 1
    This would pointlessly obliterate register a. Try something like this instead: cnoremap <M-q> <C-r>=fnameescape(expand('%:p'))<CR>
    – lcd047
    Commented Jun 3, 2015 at 9:05

Your Answer

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

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