Skip to content

03.Insert Mode

Tip 13. Make Corrections Instantly from Insert Mode

If we make a mistake while composing text in Insert mode, we can fix it immediately. There's no need to change modes. Besides the backspace key, we can use a couple of other Insert mode commands to make quick corrections.

如果出错的位置在单词的开头。专业的打字员会建议先删除整个单词,然后再重新输入一遍。

In Insert mode, the backspace key works just as you would expect: it deletes the character in front of the cursor. The following chords are also available to us:

KeystrokesEffect
<C-h>Delete back one character (backspace)
<C-w>Delete back one word
<C-u>Delete back to start of line

These commands are not unique to Insert mode or even Vim. We can also use them in Vim's command line as well as in the bash shell.

Tip 14. Get Back to Normal Mode

Insert mode is specialized for one task—entering text—whereas Normal mode is where we spend most of our time (as the name suggests). So it's important to be able to switch quickly between them. This tip demonstrates a couple of tricks that reduce the friction of mode switching.

KeystrokesEffect
<Esc>Switch to Normal mode
<C-[>Switch to Normal mode
<C-o>Switch to Insert Normal mode

Insert Normal mode is a special version of Normal mode, which gives us one bullet. We can fire off a single command, after which we'll be returned to Insert mode immediately. From Insert mode, we can switch to Insert Normal mode by pressing <C-o> (:h i_CTRL-O).

The zz command redraws the screen with the current line in the middle of the window.

In Insert mode, press <C-o>zz to redraw the screen, for the current line to be in the middle of the screen.

Tip 15. Paste from a Register Without Leaving Insert Mode

Vim's yank and put operations are usually executed from Normal mode, but sometimes we might want to paste text into the document without leaving Insert mode.

Practical Vim, by Drew Neil Read Drew Neil's

  • yt, 复制光标至指定字符之间的内容(默认去往 0 号寄存器吗?)
  • jA<space> 下移一行、去至行尾、键入空格一枚
  • <C-r>0 粘贴 0 号寄存器中的内容至光标

The general format of the command is <C-r>{register}, where {register} is the address of the register we want to insert (see :h i_CTRL-R).

The <C-r><C-p>{register} command is smarter. It inserts text literally and fixes any unintended indentation (see :h i_CTRL-R_CTRL-P).

Tip 16. Do Back-of-the-Envelope Calculations in Place

The expression register allows us to perform calculations and then insert the result directly into our document. In this tip, we'll see one application for this powerful feature.

表达式寄存器 expression register =

md
6 chairs, each costing $35, totals $

在插入模式,可以通过 <C-r>= 来获取表达式寄存器的内容,语法结构是 <C-r>=<expr><CR>

问题:如何在 Vim 和 Neovim 内快速地插入日期和时间?

Neovim

lua
-- Insert date and time under Windows OS
keymap.set("ia", "dt()", "<C-r>=strftime('%a %Y-%m-%d %H:%M:%S %z')<CR>")

根据 nvim_set_keymap 官方文档:

  • "ia" for abbreviation in Insert mode
  • "ca" for abbreviation in Cmdline mode
  • "!a" for abbreviation in both modes
  1. "ia" 表示插入模式下的缩写
  2. 左手边缩写
  3. 右手边实际执行代码:
    • 通过 <C-r>= 调用表达式寄存器
    • strftime('%a %Y-%m-%d %H:%M:%S %z') 对应的即是表达式中的内容,此处其实是调用了 strftime() 函数
    • <CR> 回车键对表达式完成解析、生成内容、插入文档

扩展阅读

  • See :h vim.keymap.set
  • See :h nvim_set_keymap

Tip. 17 Insert Unusual Characters by Character Code

Vim can insert any character by its numeric code. This can be handy for entering symbols that are not found on the keyboard.

<C-v>{code} and <C-v>u{code}

  • <C-v>065 A
  • <C-v>u00bf

See :h i_CTRL-V_digit

If you want to find out the numeric code for any character in your document, just place the cursor on it and trigger the ga command.

KeystrokesEffect
<C-v>{123}Insert character by decimal code
<C-v>u{1234}Insert character by hexadecimal code
<C-v>{nondigit}Insert nondigit literally
<C-k>{char1}{char2}Insert character represented by {char1}{char2} digraph

Tip. 18 Insert Unusual Characters by Digraph

While Vim allows us to insert any character by its numeric code, these can be hard to remember and awkward to type. We can also insert unusual characters as digraphs: pairs of characters that are easy to remember.

<C-k>{char1}{char2}<C-k>?I<CR> ¿

:h digraphs-default:h digraphs:h digraph-table

Tip 19. Overwrite Existing Text with Replace Mode

Replace mode is identical to Insert mode, except that it overwrites existing text in the document.

md
Typing in Insert mode extends the line. But in Replace mode
the line length doesn't change.
  • R
  • gR
  • r{char}
  • gr{char}

Vim has a second variant of Replace mode. Virtual Replace mode is triggered with gR and treats the tab character as though it consisted of spaces.

Vim also provides a single-shot version of Replace mode and Virtual Replace mode. The r{char} and gr{char} commands allow us to overwrite a single character before switching straight back to Normal mode (:h r).