23

I'd like to open a new tab with the file explorer set to the directory of the file I have open, instead of the directory I opened vim from. For example,

If I do:

~: cd ~/dir1
~/dir1: vim dir2/dir3/dir4/file1

Then from inside that window I would like to open a new tab with a file explorer currently showing the contents of ~/dir1/dir2/dir3/dir4/ (the directory of the file in the currently active tab).

I have the following mapping set up:

nnoremap <leader>t :tabnew<CR>:Ex . <CR>

But that just opens the file explorer at ~/dir1. How can I edit this key-mapping to pass to :Ex the directory of the file that is currently open?

3 Answers 3

76

Alternatively to :tabe %:h, there is also

:Texplore

or simply :Te

4
  • 1
    For me this doesn't open up the tab in the directory of the file that is currently open. In other words, using my example above this will open a tab and file explorer pointing to ~/dir1
    – Diego
    Commented Apr 12, 2011 at 11:04
  • 2
    Diego: you're right, :Te is in this regard inconsistent with all the other :Explore commands. This looks like a bug, and I have reported it. Sorry for the very late answer :D
    – François
    Commented Sep 27, 2013 at 11:07
  • update: it looks like the bug has been fixed as of vim 7.4.827
    – François
    Commented Mar 22, 2016 at 15:59
  • exactly what I was looking for. :Te - such and elegant command. Commented Sep 14, 2022 at 6:57
18
:tabe %:h

Opens a tab, editing the 'head of current file-name' (% is the name of the current file, : adds modifiers, and h is the basename/head modifier), that is the directory.

1
  • 1
    This is wonderful... I love learning something new about VIM :]
    – Diego
    Commented Apr 8, 2011 at 14:32
1

Alternatively to François 's answer, you can also open new split window, in case you want to do comparison side by side horizontally or vertically by using :Hexplore or :Vexplore respectively.

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