76

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

2

9 Answers 9

92

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

2
  • 1
    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, 2013 at 21:56
  • 20
    also, pressing 1 before Ctrl+G gives you the file's full path
    – rxdazn
    Dec 13, 2013 at 1:57
31
:f

gives the name of file currently open.

1
  • 1
    brilliant and very minimalist
    – amphibient
    Dec 18, 2013 at 16:51
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.

0
8

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

1
  • 4
    The other problem with this is that if the file was opened after vim was launched, it won't be listed.
    – phemmer
    Dec 12, 2013 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.

1
  • I always use statusline as well. +1
    – Rob
    Dec 13, 2013 at 15:46
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.

3

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

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.

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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