Update current working directory after vim netrw exit
Asked Answered
R

4

5

Here's the scenario.
I am on the unix command line (in home directory). I want to browse the directory through

$ vim . 

thus opening the vim netrw.
Now I am browsing the directory using the netrw.
What I want here is that when I exit vim netwr, I want my previous current working directory (in this example the home directory) to now become the directory I was previously in vim netrw.
Example:
step 1. now in home directory
step 2. vim . (thus opening vim netrw)
step 3. go to any directory (~/my/other/folders)
step 4. :q (to exit vim)
step 5. (here, I want my previous directory to now become ~/my/other/folders

any ideas on how to do it? I was thinking of doing something in .vimrc but I dunno how. Been into google search, but found nothing valuable.

Rump answered 18/3, 2013 at 2:58 Comment(0)
O
3

A possible solution would be to change the current work directory while in netrw by pressing c, and spawn a new shell from the folder you're in by issuing :shell

So it would look like:

vim .
Navigate to the desired folder...
c
:shell

And there you are in a shell in the current folder you were in netrw.

And when you exit that shell, you fall back to where you were in netrw and can continue using the explorer.

Occultation answered 29/4, 2018 at 18:40 Comment(0)
C
2

I don't think it's possible at all. Every command executed via system('command') or :!command is executed through a subshell, not through the shell that started Vim so I don't see how you could alter the host shell in any way.

But I smell an XY problem here. What is your goal?

Do you want to be able to execute some commands on the files you just edited and you want to be in their directory? If so, do you know about :sh? :!command?

Do you want a "graphical" file explorer for your shell? If so, do you know vifm? Ranger? Midnight Commander?

Cabin answered 18/3, 2013 at 6:8 Comment(6)
uhm, I actually wants to browse through using ascii-graphical way instead of doing `cd ' (pressing tabs). So I was thinking if I could browse through the netrw and exit in the shell with the new directory.Rump
This is text book XY problem, then. You want a text-based file explorer but you ask for a workaround to a bad idea that can't solve your initial problem in the first place. Just try the file explorers I've linked to in my answer. My favorite is vifm, FWIW.Cabin
uhm, yes. I later realize it's an XY problem after all. My bad. You may now vote down my entry. My apology.Rump
I end up reading meta.stackexchange.com/questions/66377/what-is-the-xy-problem for the first time.Rump
No, I won't downvote your question, but you should probably upvote and tick my answer as the programs I've listed solve your underlying problem (and the answer itself introduced you to the dreaded XY problem ;-)).Cabin
The original question still stands though - did you manage to use another shell based file explorer, to change your current directory? If so, which one? I want to do a simliar thing (output the selected filename to stdout)..... unix.stackexchange.com/q/171907/22734Projectionist
D
2

To add to Wadih's answer, you can put this in your .vimrc file:

let g:netrw_keepdir=0

This means the working directory will be automatically updated and you don't need to press c each time.
So after navigating to a folder in netrw, all you need to do is do this:

:sh

And this opens the terminal in the current folder.

From the netrw help file:

By default, g:netrw_keepdir is 1. This setting means that the current directory will not track the browsing directory. (done for backwards compatibility with v6's file explorer).

Setting g:netrw_keepdir to 0 tells netrw to make vim's current directory track netrw's browsing directory.

Dryclean answered 8/12, 2019 at 22:35 Comment(0)
L
0

You can copy the current Netrw path using:

:norm 3G5|"+y$

then quit [SHIFT+Z+Q], and change to it cd [CTRL+V][ENTER].

For this to work, you also need to setup (n)vim to not clear the clipboard after leaving.

P.S. I learned what an XY problem is hands-on here.

Likelihood answered 27/2 at 14:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.