119

If I open a file with vim dir/to/my/file.c how can I easily open the directory dir/to/my in vim's filebrowser?

ljk
  • 354
  • 1
  • 9
JtR
  • 1,899

11 Answers11

141

Try :Ex[plore]. From :help Ex:

:Explore will open the local-directory browser on the current file's directory (or on directory [dir] if specified). The window will be split only if the file has been modified, otherwise the browsing window will take over that window. Normally the splitting is taken horizontally.

innaM
  • 10,302
  • Make sure you set nocompatible on your .vimrc or somewhere else. If you open vim in compatible mode (e..g, vim -u NONE), even if you set nocompatible later, netrw plugin won't be loaded and you won't be able to run :Ex. The following setting might be necessary, too: filetype plugin on. More details here: https://vi.stackexchange.com/questions/29343/why-isnt-netrw-explore-loaded-when-starting-vim-using-vim-u-none. – awvalenti Mar 20 '24 at 12:49
106

I personally prefer the:

:Sex

command. It does the split window for you, and drops you into the current directory.

:Sex dir/to/my

works too.

Jack M.
  • 3,483
28

You could use:

:e %:h

More info on expansions is here: http://vimdoc.sourceforge.net/htmldoc/eval.html#expand()

Jake
  • 899
  • I love this! You can run this command repeatedly to keep going up one directory. – Flimm Aug 15 '23 at 06:59
  • To turn it into a keyboard shortcut binding to F4, add this to your .vimrc file: noremap <F4> :e %h<cr> – Flimm Aug 15 '23 at 07:00
13

As already mentioned, you can just do ":Explore" and it will open the file explorer for the directory of the current file.

However, if you need to explicitly specify the directory, you can use ctrl+r on the :-command line to read the contents of any register in, and the % register is the current filename, including a path if necessary. So, just do :e ctrl+r% then backspace over the filename, and press enter.

There are other ways to do it, but this is the method I usually use.

Heptite
  • 19,838
7

For opening Windows Explorer showing current file's directory :

:!start explorer /select,%:p

(Source : http://vim.wikia.com/wiki/Open_Windows_Explorer_showing_directory_of_current_buffer )

Excellll
  • 12,717
DomCha
  • 71
3

Just :E should do it. Tested on neovim.

barrrista
  • 1,729
3
:e `dirname %`

dirname expands to the directory in which current file is.

1

if you want to actually get a shell in it you'll need to :lcd %:p:h run :shell and then restore the working directory after you've exited

JonnyRaa
  • 213
  • 1
  • 3
  • 10
1

:e dir/to/my/

:e . opens the directory you initially launched vim from.

Vim browser 'c' sets the working directory.

I'm not sure if there is a way to automatically jump to the directory of the file you are editing.

1

If you edit with the pwd command on unix it will open the current directory you are in.

:e `pwd`

This will not work like you want it if you did a cd while in vim.

Dan Walker
  • 9,327
nik
  • 56,306
0

If you want to launch a custom terminal emulator in your current directory's buffer, add something like this to your vimrc:

" Set Vim's current directory to the opened buffer
set autochdir
nmap <leader>k :silent !kitty &<cr>

Replace kitty with your favorite terminal emulator.

See also: