79

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>?)

amphibient
  • 12,472
  • 18
  • 64
  • 88

9 Answers9

93

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).

Joseph R.
  • 39,549
32
:f

gives the name of file currently open.

mtk
  • 27,530
  • 35
  • 94
  • 130
30

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.

slm
  • 369,824
9

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

41754
  • 95
  • 5
    The other problem with this is that if the file was opened after vim was launched, it won't be listed. – phemmer Dec 12 '13 at 17:57
4

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.

jmathew
  • 2,988
  • 2
  • 16
  • 11
4

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.

Philip
  • 141
3

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

kojiro
  • 4,644
1

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.

Alexej Magura
  • 4,466
  • 7
  • 26
  • 39
1

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.