Vi 替换命令

发布于 2024-07-14 13:39:24 字数 63 浏览 7 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

赢得她心 2024-07-21 13:39:24

您想要实际插入行号,而不仅仅是将其显示在页边距中吗? 如果是这样 -

:s/$/\=line(".")/

这会将其附加到行尾。 将 $ 替换为 ^ 以添加到该行前面。

You want to physically insert the line number, not just display it in the margin? If so -

:s/$/\=line(".")/

This will append it the the end of the line. Replace $ with ^ to prepend to the line.

绝不放开 2024-07-21 13:39:24

首先:以下假设您使用的是 vim,而不是 vi 或类似工具或 Heirloom 项目 vi。

这里有几个选项:

首先,如果您要使用 :s,则将光标放在您选择的行上:

:s/texttoreplace/<C-R>=line(".")<CR>/

其中 = 表示实际上按 Ctrl-R 然后输入等号。 提示
将会改变,您可以输入:

line(".")

然后按回车键,如 所示。 此时您将回到您的 :s
插入行号的命令。 您可以在插入模式下执行相同的操作
以及(=line("."))。

其次,您可以使用 \= 在正则表达式中插入行号
同样的方式,但这在插入模式或其他地方不起作用。

最后,如果您希望在插入和命令行模式下使用 Ctrl-A 插入当前行号,这些功能会很有用:

:imap <silent> <C-A> <C-R>=line(".")<CR>
:cmap <C-A> <C-R>=line(".")<CR>

不要将静音属性添加到 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:

:s/texttoreplace/<C-R>=line(".")<CR>/

where <C-R>= means actually hit Ctrl-R then type an equal sign. The prompt
will change and you can enter:

line(".")

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 the
same 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:

:imap <silent> <C-A> <C-R>=line(".")<CR>
:cmap <C-A> <C-R>=line(".")<CR>

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.

千鲤 2024-07-21 13:39:24

我尝试过

:s/texttoreplace/<C-R>=line(".")<CR>/

,但这将文本替换为我在每行上发出命令 (1) 时所在的行号,而不是为每行使用不同的数字。

以下内容对我有用。

g/^/exec "s/texttoreplace/(".line(".")."/"

g/^/exec 在与正则表达式 /^/ 匹配的每一行(即每一行)上执行给定的命令。 然后,表达式 line(".") 在正在处理的行的范围内进行计算,而不是在输入命令时范围内的行。

I tried

:s/texttoreplace/<C-R>=line(".")<CR>/

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.

g/^/exec "s/texttoreplace/(".line(".")."/"

The g/^/exec executes the given command on every line that matches the regex /^/ (i.e. every line). The expression line(".") is then evaluated in the scope of the line being worked on, and not the line in scope when the command is typed in.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文