同一行有多个命令

发布于 2024-09-09 03:31:04 字数 106 浏览 2 评论 0原文

我一直在尝试找到能让我在 Vim 中的同一行上运行多个命令的东西,类似于在 *nix 系统中使用分号分隔命令或在 Windows 中使用 & 。有办法做到这一点吗?

I've been trying to find something that will let me run multiple commands on the same line in Vim, akin to using semicolons to separate commands in *nix systems or & in Windows. Is there a way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(8

べ繥欢鉨o。 2024-09-16 03:31:05

一个 | 栏将允许您执行此操作。来自 :help :bar

'|' 可用于分隔命令,因此您可以在一个命令中给出多个命令
线。如果您想在参数中使用 '|',请在其前面添加 '\'

示例:

:echo "hello" | echo "goodbye"

输出:

hello
goodbye

注意:您可能会发现您的 ~/.vimrc 不支持映射 |\|.在这些情况下,请尝试改用

A bar | will allow you to do this. From :help :bar

'|' can be used to separate commands, so you can give multiple commands in one
line. If you want to use '|' in an argument, precede it with '\'.

Example:

:echo "hello" | echo "goodbye"

Output:

hello
goodbye

NB: You may find that your ~/.vimrc doesn't support mapping |, or \|. In these cases, try using <bar> instead.

楠木可依 2024-09-16 03:31:05

(回车/回车)放在命令之间和之后。例如:

map <F5> :w<CR>:!make && ./run<CR>

不要使用 |,因为:

  • 如果在某些命令后面使用 |,某些命令会出现问题

  • | 在配置文件中不能一致工作,请参阅 :help map_bar

Put <CR> (Carriage Return/Enter) between and after commands. For example:

map <F5> :w<CR>:!make && ./run<CR>

Don't use | because:

  • Some commands have problems if you use | after them

  • | does not work consistently in configuration files, see :help map_bar

摘星┃星的人 2024-09-16 03:31:05

您可以定义一个执行命令的函数。

function Func()
     :command
     :command2 
endfunction

并将其放置在例如您的 vimrc 中。运行该函数

exec Func()

You could define a function that executes your commands.

function Func()
     :command
     :command2 
endfunction

And place this in, for example, your vimrc. Run the function with

exec Func()
尐籹人 2024-09-16 03:31:05

vim 中的命令分隔符是 |

The command seperator in vim is |.

随梦而飞# 2024-09-16 03:31:05

认为这可能会帮助某人尝试在链中进行替换,但

由于 comment

%s/word/newword/ge | %s/word2/newword2/ge

失败,您可以使用e 标志用于在找不到字符串时忽略错误。

Thought this might help someone trying to do substitutions in a chain and one fails

from a comment

%s/word/newword/ge | %s/word2/newword2/ge

You can use the e flag to ignore the error when the string is not found.

情深已缘浅 2024-09-16 03:31:05

我总是使用 ^J 通过按 Ctrl+vCtrl+ 来分隔多个命令j。

I've always used ^J to separate multiple commands by pressing Ctrl+v, Ctrl+j.

月依秋水 2024-09-16 03:31:05

您可以创建一个新文件,并在其上写入命令。然后:so %,表示源当前文件。

You can create a new file, and write your commands on it. Then :so %, which means source current file.

家住魔仙堡 2024-09-16 03:31:05

某些命令不允许通过 | 分隔。

幸运的是,您可以将它们包装在 execute 中:

execute 'echo "hello"' | execute 'echo "goodbye"'

输出:

hello
goodbye

Some commands don't allow separation via |.

Luckily, you can wrap them up in an execute:

execute 'echo "hello"' | execute 'echo "goodbye"'

Output:

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