0

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?

Folaht
  • 1,053
  • 1
  • 13
  • 32
  • Why do you insist on using a text editor for that? – romainl Jun 18 '22 at 11:22
  • 1. It's a text which I want to edit, so a text editor command script seems like the logical choice. 2. It fits snugly into the larger shell script I made and 3. This particular line seemed too complicated to me to do this line using sed. What do you suggest I use? – Folaht Jun 18 '22 at 18:10
  • Let me rephrase #3, One particular line seemed too complicated to me to do using sed, which is incrementing the port number for all those copies. – Folaht Jun 18 '22 at 18:18

1 Answers1

0

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
Folaht
  • 1,053
  • 1
  • 13
  • 32