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.

This may sound trivial but, on more than one occasion, I have found myself having forgotten which file in vim I have open (e.g. when I am looking through different log files and such) and the only way I knew how to find out was to close the file and look in the command history for the most recent command.

Is there a command within vim to tell you which file you currently have opened without exiting the program or the file you have opened (e.g. :<which_file_cmd>?)

share|improve this question
4  
If you didn't call vim with so many args, use :args. –  galegosimpatico Dec 12 '13 at 17:30
add comment

9 Answers 9

up vote 29 down vote accepted

In addition to uprego's answer, you can press Ctrl+G (in normal mode) to get the current buffer's name as well as the total number of lines in it and your current position within it.

Update

As per rxdazn's comment, you can press 1 before Ctrl+G to get the full file path. If you press 2, you get the full file path and the buffer number you currently have open (useful when you have opened multiple files with vim).

share|improve this answer
    
Like a lot of vim commands this also applies to the less command which I use when viewing files as opposed to editing. –  jmathew Dec 12 '13 at 21:56
9  
also, pressing 1 before Ctrl+G gives you the file's full path –  rxdazn Dec 13 '13 at 1:57
add comment

When in vim I always use the :ls command.

Example

:ls
  1 %a   "blah.txt"                         line 1

Where blah.txt is the file's name.

share|improve this answer
add comment

When vim is not called with many args, :args is helpful.

share|improve this answer
4  
The other problem with this is that if the file was opened after vim was launched, it won't be listed. –  Patrick Dec 12 '13 at 17:57
add comment

If you can have a default vim configuration file, you can add a statusline to always have file details available. I understand that if you're constantly remoting into different servers this won't be much help. Here's my slightly complex one pulled from my .vimrc.

" %F(Full file path)
" %m(Shows + if modified - if not modifiable)
" %r(Shows RO if readonly)
" %<(Truncate here if necessary)
" \ (Separator)
" %=(Right align)
" %l(Line number)
" %v(Column number)
" %L(Total number of lines)
" %p(How far in file we are percentage wise)
" %%(Percent sign)
set statusline=%F%m%r%<\ %=%l,%v\ [%L]\ %p%%

" Change the highlighting so it stands out
hi statusline ctermbg=white ctermfg=black

" Make sure it always shows
set laststatus=2

And it looks like this in MacVim (but works in terminals as well). enter image description here

As always you can do :help statusline to get some more options and details. And checkout Learning Vim the Hard Way ch 17 for a good explanation of statusline.

share|improve this answer
    
I always use statusline as well. +1 –  Rob Dec 13 '13 at 15:46
add comment
:f

gives the name of file currently open.

share|improve this answer
1  
brilliant and very minimalist –  amphibient Dec 18 '13 at 16:51
add comment

There's also :echo @%, which gives you the name of the file relative to the current directory.

share|improve this answer
add comment

There countless ways to get this kind of info, but if you want something so that you only have to press a key and the filename will be displayed then insert the following into your ~/.vimrc:

map <F4> <ESC>:file<CR>

This says,

bind the `F4' key to the following sequence: Escape-key, :file, Carriage-Return (enter-key)

And the file command, as is probably apparent, returns the name of the current file along with other info.

You could also setup a status-line so that you wouldn't even have to do anything but look.

To keep the status-line always insert the following into your ~/.vimrc:

set laststatus=2

This tells ViM to always display a status-line, other options are 0, for never, and 1 for only when you have two or more windows open in ViM.

share|improve this answer
add comment

I usually just type :e and this shows me the full file path and number of lines and characters too.

Caveat: it reopens the file from disk essentially, so it's most useful when the file is already saved.

:help :e says:

                          *:e* *:edit*
Edit the current file.  This is useful to re-edit the
current file, when it has been changed outside of Vim.
This fails when changes have been made to the current
buffer and 'autowriteall' isn't set or the file can't
be written.
share|improve this answer
add comment

I my vimrc I have this mapping

nnoremap <leader>pfn :echo expand('%:p')<CR>

So if I type ,pfn, or "print file name", it echos the entire file path.

share|improve this answer
add comment

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.