尝试设置宏表达式时出现问题
Trouble when I try to set a macro expression
我有这个文件:
...
stuff
...
1 4 1
1 3 4
1 3 5
1 3 6
2 1 3
我想对这 3 个数字求和并将结果作为第 4 个数字,我想对每一行都这样做。
为此,我这样做:
qq # I register a macro with q letter
"ay # I move to the first line and first number (1) and save it into a
"by # I do the same with number 4
"cy # and for the last number of the line, number 1
esc+i # I go to insert mode and move to the position where I want the sum
ctrl+r+= # to go to the expression mode
ctrl+r+a + ctrl+r+b + ctrl+r+c # to take the numbers from the registers and sum them and I have the correct sum result pasted
esc # to exit from insert mode
q # to save the macro
如果我将它与 @q 一起用于其他行,它不起作用,但如果我使用我在录制宏时使用的同一行来执行它,它就可以工作,并粘贴相同的重复结果:
1 4 11
1 3 4
1 3 5
1 3 6
2 1 3
我做错了什么?
"ay
、"by
和 "cy
自己什么都不做。 y
是一个运算符,所以它需要一个动作。正确的命令是 "ayiw
、"byiw
和 "cyiw
.
- 我不确定你想用
esc+i
实现什么,但它不太可能允许你在最后一个数字之后附加任何内容。
- 无论如何,你的解释中缺少太多步骤,因此很难调试你的宏。
每次击键时,您的录音应该是这样的:
qq " start recording in @q
0 " move the cursor to the first column
"ayiw " yank the word under the cursor to @a
w " move the cursor to the next word
"byiw " yank the word under the cursor to @b
w " move the cursor to the next word
"cyiw " yank the word under the cursor to @c
A<Space> " append a space at the end of the line
<C-r>=<C-r>a+<C-r>b+<C-r>c<CR> " insert the sum of @a, @b, and @c
<Esc> " leave insert mode
q " stop recording
参见 :help 0
、help text-objects
、:help w
和 :help A
。
FWIW,这是一种更具可扩展性和可预测性的方法:
qq
A <C-r>=getline('.')->split(' ')->reduce({ a, v -> a + v })<CR>
<Esc>
q
参见 :help getline()
、:help split()
和 :help reduce()
。
Select 您要求和的行范围和 运行:
:'<,'>s/\v^(\d+ )(\d+ )(\d+)/\=submatch(1) . submatch(2) . submatch(3) . ' ' . (submatch(1) + submatch(2) + submatch(3))
注意:'<,'>
它将由 vim
自动放置
解释:
\v ................. very magic search (avoid many backslashes)
^ .................. start of line
() ................. regex group
\d+ ................ one digit or more
\= ................. expression register
submatch(1) ........ repeats the first regex group
我有这个文件:
...
stuff
...
1 4 1
1 3 4
1 3 5
1 3 6
2 1 3
我想对这 3 个数字求和并将结果作为第 4 个数字,我想对每一行都这样做。
为此,我这样做:
qq # I register a macro with q letter
"ay # I move to the first line and first number (1) and save it into a
"by # I do the same with number 4
"cy # and for the last number of the line, number 1
esc+i # I go to insert mode and move to the position where I want the sum
ctrl+r+= # to go to the expression mode
ctrl+r+a + ctrl+r+b + ctrl+r+c # to take the numbers from the registers and sum them and I have the correct sum result pasted
esc # to exit from insert mode
q # to save the macro
如果我将它与 @q 一起用于其他行,它不起作用,但如果我使用我在录制宏时使用的同一行来执行它,它就可以工作,并粘贴相同的重复结果:
1 4 11
1 3 4
1 3 5
1 3 6
2 1 3
我做错了什么?
"ay
、"by
和"cy
自己什么都不做。y
是一个运算符,所以它需要一个动作。正确的命令是"ayiw
、"byiw
和"cyiw
.- 我不确定你想用
esc+i
实现什么,但它不太可能允许你在最后一个数字之后附加任何内容。 - 无论如何,你的解释中缺少太多步骤,因此很难调试你的宏。
每次击键时,您的录音应该是这样的:
qq " start recording in @q
0 " move the cursor to the first column
"ayiw " yank the word under the cursor to @a
w " move the cursor to the next word
"byiw " yank the word under the cursor to @b
w " move the cursor to the next word
"cyiw " yank the word under the cursor to @c
A<Space> " append a space at the end of the line
<C-r>=<C-r>a+<C-r>b+<C-r>c<CR> " insert the sum of @a, @b, and @c
<Esc> " leave insert mode
q " stop recording
参见 :help 0
、help text-objects
、:help w
和 :help A
。
FWIW,这是一种更具可扩展性和可预测性的方法:
qq
A <C-r>=getline('.')->split(' ')->reduce({ a, v -> a + v })<CR>
<Esc>
q
参见 :help getline()
、:help split()
和 :help reduce()
。
Select 您要求和的行范围和 运行:
:'<,'>s/\v^(\d+ )(\d+ )(\d+)/\=submatch(1) . submatch(2) . submatch(3) . ' ' . (submatch(1) + submatch(2) + submatch(3))
注意:'<,'>
它将由 vim
解释:
\v ................. very magic search (avoid many backslashes)
^ .................. start of line
() ................. regex group
\d+ ................ one digit or more
\= ................. expression register
submatch(1) ........ repeats the first regex group