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?
Opening a tab Probably the easiest to remember is to run the :tabnew command while in normal mode. This will open a new tab with an empty buffer. If you want to edit a file in the new tab, you can run :tabnew filename and Vim will load the file in the new tab.
The command :Explore opens the file explorer window. Select a file or directory name and press Enter to open that file or directory. (For example :e /home/user displays the contents of that directory.) To return to the explorer window, press Ctrl-^ (usually Ctrl-6).
netrw - the unloved directory browser The netrw plugin normally ships with vim and is the default filebrowser. It gets a bad rap and ships with all kinds of features likes remote editing over SSH and FTP, and anecdotally has many bugs.
Alternatively to :tabe %:h
, there is also
:Texplore
or simply :Te
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With