save in insert mode (VIM)

35

8

I am new to vim,

Usually, I find myself wanting to save and stay in insert mode, rather than hit escape, :w and back into insert mode.

markiv

Posted 2009-12-26T20:32:56.070

Reputation:

2I think it should stay here. Most vim questions are asked on SO, and by starting to divide them amongst sites accomplishes nothing except giving a user more work in terms of searching for them in all three sites. Vim is also an editor more often found at programmer's then average(or super:) users's machines. – Rook – 2009-12-26T23:26:54.103

I agree. SO has 1,199 questions tagged vim. SU has 106. – zen – 2009-12-28T22:11:55.757

Answers

39

Ctrl+O, then :w and Enter.

Juliano

Posted 2009-12-26T20:32:56.070

Reputation: 2 492

@Idigas: why is that? – akira – 2010-06-01T19:47:14.753

2It is the same number of key strokes, but seems not very vimish. – Seamus Connor – 2010-08-16T18:32:53.963

Yup, that's how I'd do it too. – David Wolever – 2009-12-26T20:40:15.027

2Which is actually more complicated than the usual way, IMHO. – Rook – 2009-12-26T20:41:43.477

32

You could add an alias like inoremap <F3> <c-o>:w<cr> in you .vimrc.

Tordek

Posted 2009-12-26T20:32:56.070

Reputation: 421

4

Without getting into a debate about the right or the wrong way, if you use a graphical Vim like MacVim or GVim, you should be able to use Cmd-s or Ctrl-s for saving. I can say that it works on OS X with MacVim and Cmd-s and so you can save your stuff in the insert mode.

Ambidextrous

Posted 2009-12-26T20:32:56.070

Reputation: 181

2

I cannot add comment to the @Tordek's answer, due to lack of reputation :) Therefore complementing his answer:

inoremap <F2> <C-\><C-o>:w<CR>

Adding <C-\> will prevent moving cursor one character left.

Vladimir Bauer

Posted 2009-12-26T20:32:56.070

Reputation: 121

2

Apart from that suggestion which Juliano mentioned (which should be taken out and shot <- the suggestion, not Juliano), it really is THE WRONG WAY TO DO IT. It would be actually more "normal" to try and do everything from command mode, instead of insert. But using them interchangeably is the usual vim's way and if one doesn't like it one should really stay clear of vim.

If you really don't like not being able to save from I mode, may I suggest an editor called Cream - it is a variant of vim, modified in a way that emulates nowadays's editors (so to say, it stays in I mode all the time, and has usual windows shortcuts).

Rook

Posted 2009-12-26T20:32:56.070

Reputation: 21 622

1While I agree on the general rule, I don't think it applies to this case. If you're just writing a long text and want to save often, mapping some key to <C-o>:w<CR> ain't really that bad. – Matteo Riva – 2009-12-26T20:47:53.840

@Michael Krelin ... - I agree. But he said he wants insert mode, so there it is. Personally, I thought at first about suggesting Notepad2 (or some other particular flavour) but that would've just been downvoted as non answerin gthe question. – Rook – 2009-12-26T23:22:16.823

@kemp - No. If you're writing a long text and you want to save often you use vim's capabilities in terms of: backup, writebackup and swapfile (you can :help any of them for details). Vim is -the way it is-, and although I (like all others) like a bit of customization now and then, trying to avoid several modes is just wrong. And it will not end well. Better to use something else then. After all, there are some pretty powerful alternative editors nowadays. – Rook – 2009-12-26T23:24:52.593

well, I do not see how his use of insert mode (all of us use it!) implies his desire to spend the rest of his vim session therein. I personally have a mapping for <F2> in both modes just because ever since I had crazy keyboard back in late 80s I can't unlearn hitting <F2> once in a while when editing ;-) – Michael Krelin - hacker – 2009-12-27T00:26:25.287

@Michael Krelin - read his question again, then. I do not know how this can be understood, except exactly as such. "Usually I find myself wanting to save and stay in insert mode, rather than hit escape key, :w and back into insert mode. ". He wants to stay in insert mode. I'm not blaming him, just saying ... <above ...> – Rook – 2009-12-27T00:35:01.317

4I read it as "I do not want to leave insert mode just to save file". – Michael Krelin - hacker – 2009-12-27T10:40:39.693

lawwwl a vim-vs-[editor] war! – rascher – 2009-12-27T18:16:35.220

@Michael Krelin - I, on the other hand, read it as it is written. In any case, this discussion is beginning to bore me, so let's knock it off. The OP's got all the answers he wanted, ... however he wants to use it, I don't care either way :-/ – Rook – 2009-12-27T19:00:13.850