gf to a file in a totally different directory and want to check out that new directory. Is there a way to quickly exit vim and
cd into the directory containing the current buffer?
This generally can't be done, as the current directory is in a way part of the running environment of a running process and a process typically can't change the running environment of its parent process (in other words, Vim can't affect the shell that spawned it.)
(This is similar to why shell scripts generally can't set environment variables on the shell that spawned them, which is also something that comes up quite frequently.)
There are ways you can set up Vim and the shell to actually implement something like this, but you typically need to prepare the shell for it. In general you should replace the way you call Vim with a function that allows the shell to set up a communication channel so that Vim can tell it to change directories.
A little more specifically, you would create a shell function named
vim (and possibly others called
vimdiff, etc. if you want this to work on all ways Vim can be invoked.) This function would set up a way for Vim to send the shell a directory name to
cd into when done. For example, setting up a temporary file somewhere. Then the function would invoke the real Vim, passing it this communication channel (for example, through an environment variable with the path to the file where Vim could write.) Then you could set up Vim mappings, commands or autocmd's that would write a directory name to that file. Once you leave Vim, the shell function would inspect the file to see if Vim wrote to it and then run
cd to move to the appropriate directory.
I find that tends to be pretty clunky, but it's definitely doable.
There are better alternatives though. You can start a new shell from inside Vim, with the
:sh command. When you do so, the new shell will be created under Vim's current directory (see Vim's
:cd command and also the
'autochdir' setting.) Recent versions of Vim can also run a shell inside a Vim window, with the
:term command, and that shell will also be started under Vim's current directory. So rather than trying to affect the shell that launched Vim, you could consider starting a new one from inside Vim?
I there is a mean but it use a plugin, to solve that simply.
If it's only to visualize the current directory, you can use the
:term ls %:p:h
(filtering result %=current buffer, p=path with file name, h= path only) It only display the content though.
You can also use the internal file manager with
%:p:h as a parameter (with
That open the default file manager inside vim in this specific place. With that you can perform some actions (navigate, delete, copy, paste or modify some files)
With lf (file explorer with integrated terminal)
In my case, I use a plugin that integrate
lf (my favorite terminal file explorer) as vim's internal file explorer. Luckily,
lf as integrated terminal and can also lauch some shell (as bash, zsh, etc.)
Even outside of vim, it's a good and customizable tui.
Hope it will help you.