0

I'm somewhat new to VIM and am playing with ways to open files (OS X).

Upon returning to VIM after temporarily exiting into shell (ctrl+z, followed by fg), I think it would be convenient if VIM set its current working directory to wherever I was when I returned.

Is this possible? I do know how to change the working directory manually from within VIM.

thank you,

StayCool
  • 520
  • 6
  • 17
  • 1
    I don't think it can be done easily because we generally can't ask the shell for its new `$PWD` from the foregrounded program. But… Vim already comes with a file explorer and the wildmenu and so on so what exactly are you trying to do? – romainl Oct 21 '14 at 16:29
  • For the symetric question: "Set shell directory to the vim's pwd when you pressed , see: https://stackoverflow.com/q/12239952/254487 – Tinmarino Nov 22 '17 at 22:02

1 Answers1

2

Check if your version of Vim has support for client-server functionality: :echo has('clientserver')

If so, you can write an alias to tell Vim to change its cwd:

In your .bashrc:

alias fg="$VIM --remote-send ':cd $PWD<CR>'; fg"

Where $VIM is vim or gvim or macvim.

You need the colon and the <CR> because you're sending Vim keystrokes rather than commands (<CR> is a special notation for "Enter")

I'm not sure whether --remote-send works when Vim is suspended - this approach might be better if you use something like screen or tmux to run vim and your shell at the same time.

Otherwise, it's a bit trickier.

There's a :shell command that is similar to suspending and resuming, but I'm assuming it forks a child process instead of returning you to Vim's parent process. If you're ok with that, there's a ShellCmdPost autocmd you can attach to to load information. You can use that in association with an alias that writes the $CWD to a file to load the required directory and change to it.

In your .bashrc:

alias fgv="echo $PWD > ~/.cwd; exit"

In your .vimrc:

autocmd ShellCmdPost * call LoadCWD()
function! LoadCWD()
    let lines = readfile('~/.cwd')
    if len(lines) > 0
        let cwd = lines[0]
        execute 'cd' cwd
    endif
endfunction

Looking through the list of autocommands, I was not able to find any that detect when Vim has been suspended and has just been resumed. You could remap ctrl-z to first set a flag variable then do an unmapped ctrl-z. Then write a shell script like before that writes its $CWD to a specific file. Then you can set up an autocmd to watch that file for modification and in the handler, check the flag variable, and if it's set, reset it, read the file, and change to that directory. A bit complex, but it would work.

That would look like this. You'll have to load the file when you start Vim so that it can monitor it for changes. You may want to set hidden so that you can keep the buffer open but hide it.

In your .bashrc:

alias fg="echo $PWD > ~/.cwd; fg"

In your .vimrc:

set hidden
edit ~/.cwd
enew
nnoremap <C-Z> :let g:load_cwd = 1<CR><C-Z>
autocmd FileChangedShell ~/.cwd call LoadCWD()
function! LoadCWD()
    if g:load_cwd
        let g:load_cwd = 0
        let lines = readfile('~/.cwd')
        if len(lines) > 0
            let cwd = lines[0]
            execute 'cd' cwd
        endif
    endif
endfunction