简体   繁体   English

Vim:映射在命令行模式下可用的键序列

[英]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. 这些是我设置的绑定,让Vim通过保存缓冲区来响应Ctrl + S。 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: 无论如何,有些时候,例如当Vim处于命令行模式(输入:命令或/搜索)时,按Ctrl + S将会失败,即使我使用这样的绑定:

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 . 它基本上退出了命令条目(显然是发送命令或搜索,即使我绑定了<ESC>也是不希望的),并且无法运行:update

How can I make it work if I leave Vim in command line mode but still use the bind? 如果我将Vim留在命令行模式但仍使用绑定,如何使它工作?

The solution is to use Ctrl+C to force the cancellation of whatever's entered. 解决方案是使用Ctrl+C强制取消所有输入内容。 This successfully issues the following :update . 这可以成功发出以下: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). 提示:您需要配置终端/外壳程序设置,以确保Ctrl + S不会发送“ XOFF”信号,这会使其暂时停止接受键(然后通过Ctrl + Q关闭)。 This is a one-time config thing that probably is sufficient by configuring on either the shell or terminal client. 这是一次性配置,通过在Shell或终端客户端上进行配置可能就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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