Here's how I restart vim:
$ vim
:q
$ vim
Is there a way to restart vim so that there is no need to type the second vim
in the shell?
Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It only takes a minute to sign up.
Sign up to join this communityTo answer your title question is pretty simple, just add this to your shell aliases (note untested, there may be syntax issues, but I think it gets the point across):
vim() {
while command vim $@; do
done;
}
In other words, run vim in an infinite loop. If you quit with :q, The script will restart vim, and you can break out and really quit by using :cq.
But I think if I just leave you there, it'll be doing you a disservice. I believe what your real question is and what you really wanted to do is that you want to reload vim config from scratch without losing your editing session.
There are two things you need to do to make that happen:
You'll want to save your editing session and reload them later; this will allow you to restore your open buffers and window/tab arrangements. Vim supports the basic mechanism for session saving here using :mksession
, but this builtin functionality is a bit low level for daily use. If you need to do this regularly, you'd want to have a session manager to make this feature actually usable and mostly automatic, try either xolox/vim-session plugin or tpope/vim-obsession.
You probably also want to set persistent undofile, this setting allows you to undo your edits throughout multiple editing session (i.e. over restarts)
:
should work: 0x0.st/zJMq.txt
Sep 6, 2019 at 16:22
Add this in your vimrc:
nno <space>R :<c-u>call <sid>vim_quit_and_restart()<cr>
fu! s:vim_quit_and_restart() abort
if has('gui_running') | echo 'not available in GUI' | return | endif
sil! update
sil call system('kill -USR1 $(ps -p '.getpid().' -o ppid=)')
qa!
endfu
And this in your bashrc:
trap __catch_signal_usr1 USR1
__catch_signal_usr1() { trap __catch_signal_usr1 USR1; vim; }
Pressing SPC R
should make Vim restart.
Works on Vim 8.1.1967 and bash 4.3.48.
If your shell is zsh (version 5.7.1 sure; other versions probably), things get more complicated because of a pitfall.
If you suspend Vim (C-z
, :stop
, :suspend
), after restarting it one or several times, you may not be able to send it back to the foreground, because for some reason the Vim job gets lost (absent from the shell's job table).
It could be a bug in zsh, or a race condition, I don't know. There exists a workaround (restarting from a hook):
restarting_vim=
trap __catch_signal_usr1 USR1
__catch_signal_usr1() {
trap __catch_signal_usr1 USR1
restarting_vim=1
}
__restart_vim() {
emulate -L zsh
if [[ -n "${restarting_vim}" ]]; then
restarting_vim=
vim
fi
}
precmd_functions=(__restart_vim)
but it suffers from another issue; you can't restart several times consecutively, unless you suspend and $ fg
right back in between 2 restarts.
Maybe this could be fixed with an additional workaround, making zsh automatically suspend and $ fg
after every restart via another signal and trap, but it seems awkward.
As long as you never suspend Vim (install :nno <c-z> <nop>
and :vno <c-z> <nop>
to avoid any accident), the Vim mapping + zsh trap works fine (at least in my experience).
I am on macOS with neovim. I will just provide my code:
if exists('g:GuiLoaded')
nmap <leader>rv :!osascript -e 'tell application "Terminal" to do script "sh /users/eyalkarni/vimpy3/vimqt.sh"'<CR>
else
nmap <leader>rv :!osascript -e 'tell application "Terminal" to do script "sh /users/eyalkarni/vimpy3/vimr.sh"'<CR>
endif
g:GuiLoaded
signals neovim-qt. I assume it is VimR otherwise.
The bash file is:
killall nvim-qt; open -a nvim-qt; exit
;
if you do not intend to AND/OR the commands. It certainly matters.
Sep 20, 2019 at 6:10
.vimrc
, you could just do:source $MYVIMRC
. But, as for restarting from within vim... I can't think of anything.vim || vim
. If you:q
then that's it. But if you:cquit
then a new instance of vim will start up right after the first instance exits. ;) I wouldn't actually do this, of course, though it's true that you can have vim exit with an error code...i.e. non-zero...by using:cquit
.:pyx import os; os.execlp('vim', 'vim')
to exec a new instance. It would be really bad to do it in the middle of a session though (ignore modified buffers, leave swap files around, etc.) But then I was thinking, what if this is done from a VimLeave event in an autocmd??? I may actually try this later today and see if it works...