How to save Ctrl+S to Vim?

Asked

Viewed 1,969 times

16

When I use Vim in the terminal Ctrl+S, or the terminal hangs or something else strange happens.

How to squeeze Ctrl+S is already almost an instinct to save, how to map this shortcut to save the file?

4 answers

25


Ctrl-S is a shortcut to enable terminal scroll-lock since terminals were slower in displaying characters than operators in typing them. To turn off the scroll-lock, simply press Ctrl-Q.

As the need is to use this key combination in Vim, it is necessary to configure the terminal to disable scroll-lock on some terminals, such as the xterm. This can be done by adding the option allowScrollLock: "false" in the archive ~/.Xresources.

After that, just configure Vim (file ~/.vimrc) with map!:

map! <C-s> <ESC>:w<CR>

Note: if it is necessary to map to all Vim modes, it is necessary to use the map! and the map, the first is used for Insert and Command Line modes, the second for Normal, Visual/Selection and Pending Commands modes.

  • Yeah, I was able to disable the scroll-lock, but Ctrl-S is still unsaved. You were able to make it work there?

  • I edited the answer to include the map, saved in Normal and Visual/Selection modes.

  • Dude, this isn’t working yet, in any mode. It’s working for you to save with Ctrl-S?

  • Yes, here it works with both map[! ]. I am using Vim 7.4 in Debian Testing.

  • I came 7.4 tbm, on Ubuntu here. Boy, how strange. Here he is functioning as an Esc. I do the sequence <C-S>:q and it gives "No write Since last change".

  • Strange, it should work. the only configs I’ve changed in mine .vimrc were: map! <C-S> <ESC>:w<CR> and map <C-S> <ESC>:w<CR>. and everything worked as expected in every way.

  • Oops! I cleaned the vimrc and left only these commands and now it worked!

  • Should have some mocoziando plugin to stop. Beauty, Answer accepted -- thanks! =)

Show 3 more comments

5

I think the problem is that Ctrl+S is the shortcut of the "Stop" command, which is why it is locking your terminal. You have already tried using the "stty" command" (http://www.lehman.cuny.edu/cgi-bin/man-cgi?stty+1) to configure the terminal?

I can’t test here, but it seems to me that one possibility is to create a script that does something like:

stty -ixon
vim
stty ixon

According to the stty documentation, the Ixon option does the following:

 ixon (-ixon)          Enable  (disable)  START/STOP   output
                       control.  Output is stopped by sending
                       STOP control character and started  by
                       sending the START control character.
  • That, stty -ixon disables the Ctrl-S scroll-lock that Bruno mentioned in his answer. Just need to see how to map the shortcut to save. Thanks! =)

  • +1 for the tip of stty -ixon -- I’m using this to disable the scroll lock.

  • Cool! I’m glad you were of some help. :)

1

I would use :inoremap <c-s> <c-o>:update<CR><CR>

1

In Vim you save like this (only need the ESC if you are not in command mode):

ESC:wENTER

  • 1

    Yes, I know how to save in vim. The question is how to map Ctrl-S for also save instead of messing up the terminal.

  • IS, nmap is the first idea, but only works in normal mode, right? I wanted it to work in insertion mode too.

  • 1

    I thought you were asking how you saved in the vim, but apparently you know more than I do :-) What I would do is use the shortcut vim style really. You have to enter "came mode" to use the vim... @Alice

  • Heheh! The problem is muscle memory, Ctrl-S is almost automatic.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.