An heavy solution: the sessions
Another possible option is to use the sessions mechanism:
First your vim version has to be compiled with the +mksession
option. (Use :echo has('mksession')
to check that).
Now when you are about to leave vim, use the following command:
:mksession!
This will create (or overwrite thanks to !
) a file named Session.vim in the current directory which will save your current open files, windows layout and cursor position.
Note that you can also give a path as parameter to mksession
to choose where to save and how to name you session file.
Then you can go back to your shell and do whatever you want. When you want to reopen vim with your last edited file you have to use:
$vim -S /path/to/Session.vim
This will reopen the files you where editing with the cursor at the same position
How to shorten it?
If this workflow is good for you you'll probably be able to create the bash alias the most convenient for your use case. Here is an example, maybe you'll want to bend it to your way to use sessions:
You can add this to your .vimrc
:
command! Q mksession! ~/Session.vim | qall
this will allow you to use :Q
to save your session in ~/Session.vim
and quit vim.
As a bash alias you can create :
alias lvim='vim -S ~/Session.vim'
which will reload the session created when you used :Q
in vim.
A much lighter solution: suspend vim
This solution is not suitable if you need to close your shell.
In unix shell you can suspend Vim using Ctrl+z. This will put Vim in background and you'll get acces to your shell again.
In your shell when you need to get Vim back you simply have to use the command:
$ fg
Note That zsh provides convenient mappings to use also Ctrl+z on the shell to get Vim back.
Note2 If you tend to forget that you put vim in back ground you can add these lines to your .bashrc
:
PROMPT_COMMAND='hasjobs=$(jobs -p)'
PS1=$PS1 + '${hasjobs:+\j }'
When you don't have any background job your prompt will stay the same and when you have some background jobs, a count will appear at the end of your prompt (It's up to you to bend it to your preferences). For this trick, credits goes to jw013
!vi
?