Skip to content
0

文章发布较早,内容可能过时,阅读注意甄别。

Vim Challenge 01 ​

This example explains how to run Normal mode commands in the Command-line mode. Particularly, how to back out from the Insert mode into the Normal mode.

Vim Challenge ​

To edit the content of line 2-5 into the same format as that of line 1 in the configuration file below.

Before edit:

lua
	{src = "https://github.com/vague2k/vague.nvim"},   -- line 1
  "https://github.com/stevearc/oil.nvim",            -- line 2
  "https://github.com/echasnovski/mini.pick",        -- line 3
  "https://github.com/neovim/nvim-lspconfig",        -- line 4
  "https://github.com/chomosuke/typst-preview.nvim", -- line 5

After edit:

lua
	{ src = "https://github.com/vague2k/vague.nvim" },           -- line 1
	{ src = "https://github.com/stevearc/oil.nvim" },            -- line 2
	{ src = "https://github.com/echasnovski/mini.pick" },        -- line 3
	{ src = "https://github.com/neovim/nvim-lspconfig" },        -- line 4
	{ src = "https://github.com/chomosuke/typst-preview.nvim" }, -- line 5

Solution 1 ​

The Substitute Command :s/\(.*\),/{src = \1},/

Solution 2 ​

The Command-line Mode :'<'>norm _i{src = ^[f,i}

I saw Sylvan Franklin achieved the same result by running a command :'<'>norm _i{src = ^[f,i} after the visual selection of line 2-5, but the same trick did not work on my Windows machine. Any ideas why the difference?

The command :'<'>norm _i{src = ^[f,i} failed on your Windows machine because of how the special Escape character, represented as ^[, is typed into the command line.

Let's break down the command and then explain the difference.

Explanation of the Command ​

:'<'>norm _i{src = ^[f,i}

This is a powerful way to automate Normal mode keystrokes across multiple lines. Here's what each part does:

  • :'<'>: This is the range that Vim/Neovim automatically inserts when you are in Visual mode and press :. It means "for every line in the visual selection."
  • norm: This is the normal command. It executes the keystrokes that follow as if you were typing them in Normal mode on each line of the specified range.
  • _: In Normal mode, this command moves the cursor to the first non-whitespace character of the line.
  • i{src = : The i enters Insert mode, and then the literal text {src = is typed.
  • ^[: This is the crucial part. This is not a caret ^ followed by a bracket [. It is a single control character that represents the Escape key. Its purpose here is to exit Insert mode and return to Normal mode so the next command (f,) can be executed.
  • f,: Back in Normal mode, this command moves the cursor forward to the next comma (,) on the line.
  • i}: The i enters Insert mode again, this time placing the cursor just before the comma. The } character is then inserted.

The sequence effectively says: "For each selected line, go to the beginning, insert {src = , press Escape, find the comma, and insert } right before it."

The Reason for the Difference on Windows ​

The problem lies in how you physically type the ^[ (Escape) character into the command line.

  • On Linux and macOS: You typically press Ctrl + V, and then press Esc. The Ctrl+V command tells the terminal to "insert the next character literally," which allows you to input the actual escape character

  • On Windows: The Ctrl + V combination is almost universally reserved for the "Paste" command. When you press it, Neovim (or the underlying terminal) intercepts it as a paste command, not as the "insert literal" command

The correct key combination to insert a literal character in Vim/Neovim on Windows is usually Ctrl + Q

How to Fix It ​

To make the command work on your Windows machine, you would visually select the lines and then type the command, but when you get to the part where you need the ^[ character, you should:

  1. Press Ctrl + Q
  2. Then press the Esc key

This will correctly insert the ^[ character into your command line, and the rest of the command will execute as intended.

最近更新