vim: goto to insert mode after :write
I want to return to insert mode after writing (see: else
)
function! SaveOrSaveAsInInsert()
if @% == ""
:browse saveas
else
:w
"//TODO: Return to insert mode
endif
endfunction
Tried with :w<CR>a
but does not work.
Thanks!
Edit
@Randy Morris, commented using startinsert
which works. On the other hand :h inserting-ex
says:
NOTE: These commands cannot be used with |:global| or |:vglobal|. ":append" and ":insert" don't work properly in between ":if" and ":endif", ":for" and ":en开发者_开发问答dfor", ":while" and ":endwhile".
So my question is am I stuck with startinsert
i
equivalent, or is there a way to get around with an a
behavior? Or should I stick with imap <C-s> <Esc>w<CR>a
and no filename testing?
inoremap <expr> <c-s> "\<esc>:" . (@% == "" ? "browse saveas" : "w") . "\<cr>gi"
I use gi
to get back to insert mode. And use an expression mapping to determine if the file has been saved or not.
I'm using this in my .vimrc so that ctrl-s saves and returns to what ever mode I was in previously. It might help you figure it out. ;-)
" Use CTRL-S for saving, also in Insert mode
noremap <C-S> :update<CR>
vnoremap <C-S> <C-C>:update<CR>
inoremap <C-S> <C-O>:update<CR>
Got it working by
:inoremap <silent> <C-S>
<Esc>:if expand("%") == ""<CR>:browse saveas<CR>:else<CR>:w<CR>:endif<CR>a
You can define a pseudo-noop mapping that will work for all modes:
function s:Save()
update
return ""
endfunction
inoremap <expr> <C-s> <SID>Save()
nnoremap <expr> <C-s> <SID>Save()
<...>
精彩评论