简体   繁体   中英

Vim: Mapping a key sequence that works in command-line mode

noremap <C-S> :update<CR>      
vnoremap <C-S> <ESC>:update<CR>
inoremap <C-S> <ESC>:update<CR>

These are the binds that I have set to get Vim to respond to a Ctrl+S by saving the buffer. I don't really use this, but this is very convenient for that short period of weaning on/off or interacting with other editors which we are all occasionally forced to put up with, and which invariably only provide that single bind for committing changes.

Anyway, there are times, such as when Vim is in command-line mode (entering : command or / search) when pressing Ctrl+S will fail, even if I use a bind like this:

noremap! <C-S> <ESC>:update<CR>

It basically quits the command entry (apparently sending off the command or search, which is undesired also, even when I bind the <ESC> !) and fails to run :update .

How can I make it work if I leave Vim in command line mode but still use the bind?

The solution is to use Ctrl+C to force the cancellation of whatever's entered. This successfully issues the following :update .

cnoremap <C-S> <C-C>:update<CR>

A word of warning: You will need to configure your terminal/shell settings to ensure that Ctrl+S does not send a "XOFF" signal which makes it temporarily stop accepting keys (which is then subsequently switched off with Ctrl+Q). This is a one-time config thing that probably is sufficient by configuring on either the shell or terminal client.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM