19

Can I set something like ctrl+s to save the file?

I don't want to use :w every time.

DJMcMayhem
  • 17,581
  • 5
  • 53
  • 85
Canux
  • 303
  • 2
  • 4

3 Answers3

21

First of all stop using :w to save. Use :update instead. :update writes the file to disk only when there are changes. So, it could be really helpful especially if the file is huge.

Now to map <C-S> to :update here is how you do it

nnoremap <C-S> :update<cr>

You may also want to have this mapping in Insert mode too. For that use this

inoremap <C-S> <Esc>:update<cr>gi

This mapping goes normal mode, executes the :update command and then leaves you back where you are in insert mode.

So, that is how you remap a key. But I have :update mapped to zz in my vimrc file. zz is far more easy to hit than Ctrl + S. Also, mapping Ctrl + S can cause issues in Unix terminals as pointed out in the comments by @Sato Katsura.

So if you wish to use zz, you can add that as follows

nnoremap zz :update<cr>
Durga Swaroop
  • 1,059
  • 6
  • 17
  • 4
    The second mapping does not always leave your cursor in the same place! What if you're on the first column? Use gi instead of a, or better yet, use <C-o> instead of <esc>. – DJMcMayhem Jul 22 '16 at 05:10
  • 3
    Except on the vast majority of UNIX terminals Ctrl-S is the stop character. You can use stty to disable it, but then you won't be able to pause a program's output. Remapping Ctrl-S and Ctrl-Q is a really bad idea, about as bad as remapping Esc. – Sato Katsura Jul 22 '16 at 06:19
  • 2
    @SatoKatsura Yeah. That's why I don't use Ctrl - S. I use zz for saving. – Durga Swaroop Jul 22 '16 at 08:18
  • @DJMcMayhem Thanks for pointing that. I changed a to gi. But I am not really sure what <C-o> does. Can you explain why that is better than <Esc> ? – Durga Swaroop Jul 22 '16 at 08:20
  • 1
    What I'm saying is, your answer is incomplete without a warning about terminal modes. It simply won't work on UNIX without stty adjustments. – Sato Katsura Jul 22 '16 at 08:29
  • @SatoKatsura Added that in the answer. Since I have never used a stty terminal before I am not aware of these issues. – Durga Swaroop Jul 22 '16 at 09:13
  • stty is a command that sets terminal parameters. It's all explained in the wiki. – Sato Katsura Jul 22 '16 at 09:23
  • 3
    @DurgaSwaroop Yes, sure! <C-o> is extremely useful for mappings. It means "Leave insert command to do a single normal mode command". – DJMcMayhem Jul 22 '16 at 14:20
  • There's also a question about it btw: Mapping Ctrl+s does not work – Martin Tournoij Jul 22 '16 at 17:47
  • @Canux Yeah. zz is really easy to hit. It has become a reflex for me these days. – Durga Swaroop Jul 25 '16 at 10:18
  • As others suggested, <C-o> leaves the Insert mode (switches to the Normal mode), but after you do one command, it returns back. You might end up at a different position, if you want to (<C-o>^), but I don't see a problem with :update in this regard. In other words, you might want to do inoremap <C-s> <C-o>:update<CR>. – x-yuri Dec 12 '21 at 13:18
4

Yes, that's actually set up in the mswin.vim file that distributed with Vim 6.2 and above for windows.

It contains the following mappings:

" 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>

See Vim Wikia page on mapping Ctrl-S for this and other possible mappings.

Herb
  • 1,213
  • 1
  • 14
  • 28
0

With ideaVim pluging on WebStorm and prettier configured to format on save (not through File Watchers), you must use :w instead of :update.

D. Ben Knoble
  • 26,070
  • 3
  • 29
  • 65
  • 2
    Welcome to Vim! Thanks for you answer. Since the set of answers are continuously changing it would be good if you could make you answer non depending on other answer. Would it be possible to edit your answer such that it provide a full solution to the original question? – Vivian De Smedt Oct 30 '23 at 13:44