基本對映(方便快捷方式)的插圖

在大多數文字編輯器中,儲存當前文件的標準快捷方式是 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 可能無法按預期工作。解決方案不屬於本文件的範圍,但可以在此處找到。