How do I open the directory of the current open file?

92

18

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?

JtR

Posted 2009-08-28T21:57:59.047

Reputation: 1 549

Answers

113

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

Posted 2009-08-28T21:57:59.047

Reputation: 9 208

79

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.

Posted 2009-08-28T21:57:59.047

Reputation: 3 133

15You can also use :Vex to open the directory in a vertical split. – Chuan Ma – 2014-06-25T16:56:04.663

3Vex and Sex are beautiful, that's exactly what I'm looking for online, thank you! – Zen – 2014-10-10T13:13:21.300

3And it turns out that Tex was exactly what I was looking for (rather than tabe.) – SpoonMeiser – 2016-04-28T13:42:39.273

3Welp, there goes my virginity. – Mateen Ulhaq – 2017-11-08T08:08:06.990

Why can I never remember this shortcut – Anton Strogonoff – 2018-08-18T06:17:30.400

97This is good but the command is so hard to remember. – Gavin – 2013-06-09T20:24:08.480

17

You could use:

:e %:h

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

Jake

Posted 2009-08-28T21:57:59.047

Reputation: 579

10

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

Posted 2009-08-28T21:57:59.047

Reputation: 16 267

6I know this is way old, but you can also do :e %[tab] which will expand % for you. – Brian – 2011-12-07T15:20:02.360

5

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 )

DomCha

Posted 2009-08-28T21:57:59.047

Reputation: 51

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.

None

Posted 2009-08-28T21:57:59.047

Reputation:

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.

nik

Posted 2009-08-28T21:57:59.047

Reputation: 50 788

1

:e `dirname %`

dirname expands to the directory in which current file is.

Tadeusz A. Kadłubowski

Posted 2009-08-28T21:57:59.047

Reputation: 2 005

0

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

Posted 2009-08-28T21:57:59.047

Reputation: 183