基本映射(方便快捷方式)的插图

在大多数文本编辑器中,保存当前文档的标准快捷方式是 Ctrl + S (或 macOS 上的 Cmd + S )。

默认情况下,Vim 没有此功能,但可以对其进行映射以简化操作。在 .vimrc 文件中添加以下行将完成这项工作。

nnoremap <c-s> :w<CR>
inoremap <c-s> <c-o>:w<CR>

nnoremap 命令映射 Ctrl + s 到:w(将当前内容写入文件)命令,而 inoremap 命令映射 Ctrl + S 到:w 命令并返回到插入模式(<c-o> 进入正常模式,一个命令然后返回插入模式,不改变像 <esc>:w<cr>a 这样的其他解决方案无法保证的光标位置)。

同样的,

" This is commented, as Ctrl+Z is used in terminal emulators to suspend the ongoing program/process.
" nnoremap <c-z> :u<CR>

" Thus, Ctrl+Z can be used in Insert mode
inoremap <c-z> <c-o>:u<CR>

" Enable Ctrl+C for copying selected text in Visual mode
vnoremap <c-c> <c-o>:y<CR>

PS:但是必须注意的是,在使用 ssh(或 PuTTY)时,Ctrl + S 可能无法按预期工作。解决方案不属于本文档的范围,但可以在此处找到。