Search code examples
vimkeymapping

Vim: Open new explorer tab with the directory of the file you're currently on


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?


Solution

  • :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.