Vi 替换命令
使用 vi,如何将当前行号替换到当前行中? 例如,如果光标位于第 10 行,我想将数字 10 放在该行的某处。
Using vi, how do I substitute the current line number somewhere into the current line? For example, if the cursor is on line 10, I would like to put the number 10 somewhere on that line.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您想要实际插入行号,而不仅仅是将其显示在页边距中吗? 如果是这样 -
这会将其附加到行尾。 将 $ 替换为 ^ 以添加到该行前面。
You want to physically insert the line number, not just display it in the margin? If so -
This will append it the the end of the line. Replace $ with ^ to prepend to the line.
首先:以下假设您使用的是 vim,而不是 vi 或类似工具或 Heirloom 项目 vi。
这里有几个选项:
首先,如果您要使用
:s
,则将光标放在您选择的行上:其中
=
表示实际上按 Ctrl-R 然后输入等号。 提示将会改变,您可以输入:
然后按回车键,如
所示。 此时您将回到您的:s
插入行号的命令。 您可以在插入模式下执行相同的操作
以及(
=line(".")
)。其次,您可以使用
\=
在正则表达式中插入行号同样的方式,但这在插入模式或其他地方不起作用。
最后,如果您希望在插入和命令行模式下使用 Ctrl-A 插入当前行号,这些功能会很有用:
不要将静音属性添加到
cmap
行:它会禁止将该行输出到您正在键入的命令中,直到您退格为止。First: the following are assuming you are using vim, not vi or a workalike or the Heirloom project vi.
Here are a couple of options:
First, if you're going to use
:s
, then put the cursor on your chosen line and:where
<C-R>=
means actually hit Ctrl-R then type an equal sign. The promptwill change and you can enter:
and then hit return, as indicated by
<CR>
. At this point you'll be back at your:s
command with the line number inserted. You can do the same trick in insert mode
as well (
<C-R>=line(".")<CR>
).Second, you can use
\=
to insert the line number in a regular expression in thesame way, but this doesn't work in insert mode or in other places.
Finally, these are useful if you want Ctrl-A in insert and command line mode to insert the current line number:
don't add the silent attribute to the
cmap
line: it inhibits output of the line into the command you're typing until you backspace.我尝试过
,但这将文本替换为我在每行上发出命令 (1) 时所在的行号,而不是为每行使用不同的数字。
以下内容对我有用。
g/^/exec
在与正则表达式/^/
匹配的每一行(即每一行)上执行给定的命令。 然后,表达式line(".")
在正在处理的行的范围内进行计算,而不是在输入命令时范围内的行。I tried
but this replaced the text with the number of the line that I was on when I issued the command (1) on every line, instead of using a different number for every line.
The following worked for me.
The
g/^/exec
executes the given command on every line that matches the regex/^/
(i.e. every line). The expressionline(".")
is then evaluated in the scope of the line being worked on, and not the line in scope when the command is typed in.