TransWikia.com

`cd` to the directory containing the current file

Vi and Vim Asked on December 31, 2020

Sometimes I 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?

2 Answers

I there is a mean but it use a plugin, to solve that simply.

First approach

If it's only to visualize the current directory, you can use the :term command:

: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 :e, or :tabnew or :sp or :vsp command):

:e %:p:h

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.

Answered by Fabrice Hategekimana on December 31, 2020

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 vi, view, 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?

Answered by filbranden on December 31, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP