如何使用变量在正常模式下使用`nvim -e file -c`命令?

发布于 2025-02-08 16:46:12 字数 1610 浏览 2 评论 0 原文

我有一个要从此操作的配置文件..

输入文件

["12000xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx12000",["127.0.0.1:12000"]]

..到以下内容:

输出文件

[
  "12000xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx12000",
  [
    "127.0.0.1:12000",
    "127.0.0.1:12001",
    "127.0.0.1:12002",
    "127.0.0.1:12003",
    "127.0.0.1:12004",
    "127.0.0.1:12005",
    "127.0.0.1:12006",
    "127.0.0.1:12007",
    "127.0.0.1:12008",
    "127.0.0.1:12009",
    "127.0.0.1:12010",
    "127.0.0.1:12011",
    "127.0.0.1:12012",
    "127.0.0.1:12013",
    "127.0.0.1:12014",
    "127.0.0.1:12015"
  ]
]

因此我创建了一个运行的脚本文件 nvim -e -c'命令'-c ..etc。 一个特定的-c行包含以下内容:

run.sh

...
HOST_IP=127.0.0.1
HOST_PORT=12000
NUM_NODES=15
...
/usr/bin/nvim -e output.txt \
...
-c 'exe "norm /\"127.0.0.1:12000\"\n$a,\<ESC>yy15p14\n$x"' \
...

,我想将其转换为

run.sh.sh

...
HOST_IP=127.0.0.1
HOST_PORT=12000
NUM_NODES=15
...
/usr/bin/nvim -e output.txt \
...
-c 'exe "norm /\"${HOST_IP}:${HOST_PORT}\"\n$a,\<ESC>yy${NUM_NODES}p$(($NUM_NODES-1))\n$x"' \
...

但我不能这样做,因为字符将被读取字面上地。

我也不熟悉这种编辑风格 作为我唯一可以在使用(n)vim -e -c'命令'的信息,它在一个页面上: 但这可能是这样做的最好方法。

这里有人有经验吗 谁能告诉我如何使用(n)vim在 VIM可执行命令在正常模式下?

I've got a config file that I want to manipulate from this..

Input file

["12000xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx12000",["127.0.0.1:12000"]]

..to the following:

Output file

[
  "12000xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx12000",
  [
    "127.0.0.1:12000",
    "127.0.0.1:12001",
    "127.0.0.1:12002",
    "127.0.0.1:12003",
    "127.0.0.1:12004",
    "127.0.0.1:12005",
    "127.0.0.1:12006",
    "127.0.0.1:12007",
    "127.0.0.1:12008",
    "127.0.0.1:12009",
    "127.0.0.1:12010",
    "127.0.0.1:12011",
    "127.0.0.1:12012",
    "127.0.0.1:12013",
    "127.0.0.1:12014",
    "127.0.0.1:12015"
  ]
]

So I've created a script file that runs a nvim -e -c 'command' -c ..etc.
and one particular -c line contains the following:

run.sh

...
HOST_IP=127.0.0.1
HOST_PORT=12000
NUM_NODES=15
...
/usr/bin/nvim -e output.txt \
...
-c 'exe "norm /\"127.0.0.1:12000\"\n$a,\<ESC>yy15p14\n$x"' \
...

And I want to turn that into

run.sh

...
HOST_IP=127.0.0.1
HOST_PORT=12000
NUM_NODES=15
...
/usr/bin/nvim -e output.txt \
...
-c 'exe "norm /\"${HOST_IP}:${HOST_PORT}\"\n$a,\<ESC>yy${NUM_NODES}p$(($NUM_NODES-1))\n$x"' \
...

But I can't do that as the characters will be read literally.

I'm also not very familiar with this style of editing
as the only info I could find on using (n)vim -e -c 'command' is on a single page:
https://blog.robertelder.org/use-vim-inside-a-unix-pipe-like-sed-or-awk/
but it's probably the best way to do this.

Does anyone here have experience with this
and can anyone tell me how one can use (n)vim to use variables in
vim executable commands in normal mode?

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

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

发布评论

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

评论(1

枯寂 2025-02-15 16:46:12

我发现一个人可以使用-es选项
为了从多行脚本编写正常模式命令。
不确定,但是我认为&lt;&lt; - 中的额外负数
使脚本读取shell脚本变量。

/usr/bin/nvim -es output.txt <<-EOF
:set expandtab
:set shiftwidth=2
:let g:lastcount=${HOST_PORT}
:fun PlusPlus()
let l:count=g:lastcount
let g:lastcount+=1
return l:count
endfun
:s/\[\([^][]*\)/[\\r\1\\r/g|s/]/&\\r/
/${HOST_IP}:${HOST_PORT}
:norm \$a,
:norm yy${NUM_NODES}p
:norm ${NUM_NODES}\$x
:norm gg
:%s/${HOST_IP}:${HOST_PORT}/\=printf('${HOST_IP}:%d', PlusPlus())
:norm gg=G
:wq!
EOF

I discovered that one can use the -es option
in order to write normal mode commands from an multiline script.
Not sure, but I think extra minus in <<-
makes the script read shell script variables.

/usr/bin/nvim -es output.txt <<-EOF
:set expandtab
:set shiftwidth=2
:let g:lastcount=${HOST_PORT}
:fun PlusPlus()
let l:count=g:lastcount
let g:lastcount+=1
return l:count
endfun
:s/\[\([^][]*\)/[\\r\1\\r/g|s/]/&\\r/
/${HOST_IP}:${HOST_PORT}
:norm \$a,
:norm yy${NUM_NODES}p
:norm ${NUM_NODES}\$x
:norm gg
:%s/${HOST_IP}:${HOST_PORT}/\=printf('${HOST_IP}:%d', PlusPlus())
:norm gg=G
:wq!
EOF
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文