Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

After some searching, I got to know :echo @% display's the current file-name in the bottom line of vim-screen.

I would like to dump the file-name(with and without full path) into the contents of the file without leaving vim.

Is there any way to do this?

share|improve this question

4 Answers 4

up vote 6 down vote accepted

I'm sure there are other ways to do this... but the main command you want is :r[ead] which inserts the output of a command into the buffer.

So, to insert just the file name:

:r! echo %

And, to include the full path:

:r! echo %:p

For more info:

:help read
:help filename-modifiers 
share|improve this answer

As can be seen in :h registers the "% register contains the actual file name. The :pu[t] command inserts the content of a register into the text.

So, to insert the actual filename you can type either of these, in command mode:

:put %
"%p

To insert the filename with the full path, type in command mode:

:put "%:p"

More info:

:h pu[t]

By typing "rp you can paste the contents of register 'r'.

share|improve this answer

A simple way is to run:

!!echo %
  • !! is replacing the current line with the result of the command following it.

  • % is replaced by the name of the edited file in the command so this will effectively insert that name in the edited file.

The filename is the one you passed to the vi(m) command and might contain a relative or absolute path. Should you want to strip it and only retain the file name, run

!!echo $(basename %)
share|improve this answer
    
Make sense, done. –  jlliagre Dec 4 '12 at 16:49

The current filename is in the "% register, so you can insert it (while in insert mode) with <c-r>%; the full path can be inserted with <c-r>=expand("%:p"). You could make a macro of it if you use it often. For more, similar tricks, see :h expand and :h "=.

share|improve this answer
    
Maybe I'm misreading that, but isn't <c-r> mapped as the redo command? –  jonyamo Dec 4 '12 at 15:38
4  
In insert mode, <c-r> specifies that the next character is a register. –  Kevin Dec 4 '12 at 15:43
    
Righto, thanks. Totally forgot to switch to insert mode... –  jonyamo Dec 4 '12 at 15:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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