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