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.

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

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?

6 Comments

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.
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.
uhm, yes. I later realize it's an XY problem after all. My bad. You may now vote down my entry. My apology.
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/22734
|
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.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.