1

I'm using this plugin to rename files, but I was wondering if it's possible to just specify a name change. For example, if I rename a file that's not in the :pwd and I don't specify its path, it will get moved to the current dir.

I'm using this mapping: nnoremap <F6> :Rename

So maybe I can put some variable after the :Rename command that will place the current file's relative path and name?

Thanks!

6
  • Is there a reason you can't use mv from outside of VIM?
    – MDMarra
    Aug 17, 2010 at 17:58
  • Because I don't want to switch and then re-open the file in vim
    – Ivan
    Aug 17, 2010 at 18:25
  • 2
    You could do it within Vim with :!mv <oldname> <newname>. In fact, in your case you could execute :!mv % %:h/newname and Vim would expand % to the current file name and %:h to the parent directory of the current file name when the command is executed. See :help filename-modifiers.
    – garyjohn
    Aug 17, 2010 at 18:40
  • That is cool. Is there a way to include the modifier in a mapping? <C-R>%:n just appears to take % into account and :n is printed literally.
    – Ivan
    Aug 17, 2010 at 19:50
  • 2
    If you want to expand %:h (that's an h, not an n) before the command is executed, use <C-R>=expand("%:h")<CR>. The = refers to the expression register (See :help c_CTRL-R_=) which prompts for an expression. An expression may be a function call; expand() is a function that expands its argument. (See :help expand().) The <CR> terminates the expression so that its value is placed where you typed the Ctrl-R. Your mapping could then be something like nnoremap <F6> :!mv % <C-R>=expand("%:h")<CR>/. Note that I chose to not expand the first % until the command is executed.
    – garyjohn
    Aug 17, 2010 at 20:27

1 Answer 1

3

Vim keeps the current file name in the % register. You can insert the value of a register into the command line by typing Ctrl-R followed by the name of the register. Therefore, you should be able to type <F6> Ctrl-R % (without the intervening spaces) and see something like this on the command line:

:Rename /full/path/to/the/current/file

You can then edit the last component of the full path to change the name.

Note that Vim expands % to the full path name only if the current working directory is different from the file's parent directory. Otherwise, Vim expands % to just the file name.

See also

:help quote%
:help c_CTRL-R
2
  • Sweet! Any way of putting that in nnoremap? I tried nnoremap <F6> :Rename <Ctrl>R % but that just prints it literally instead of simulating the key presses.
    – Ivan
    Aug 17, 2010 at 18:21
  • Nevermind, it's <C-R>%
    – Ivan
    Aug 17, 2010 at 18:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .