Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

vim

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?

like image 601
Steven Lu Avatar asked Jul 15 '13 21:07

Steven Lu


1 Answers

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.

like image 145
Steven Lu Avatar answered Sep 21 '22 22:09

Steven Lu