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?

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
Diego
  • 16,830
  • 8
  • 34
  • 46

3 Answers3

76

Alternatively to :tabe %:h, there is also

:Texplore

or simply :Te

François
  • 7,988
  • 2
  • 21
  • 17
  • 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 Apr 12 '11 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 Sep 27 '13 at 11:07
  • update: it looks like the bug has been fixed as of vim 7.4.827 – François Mar 22 '16 at 15:59
  • exactly what I was looking for. :Te - such and elegant command. – Kartik Podugu Sep 14 '22 at 06: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.

falstro
  • 34,597
  • 9
  • 72
  • 86
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.

Yodi S.
  • 106
  • 1
  • 5