Run a Vim command from a bash script

6,000

Ok it looks like the EOF was just a lot more sensitive than I excpected

this doesn't work

vim -E -s dummy.out <<-EOF
   :%s/old/new/g
   :%s/old2/new2/g
   :%s/old3/new3/g
   :update
   :quit
EOF

this does

vim -E -s dummy.out << EOF
:%s/old/new/g
:%s/old2/new2/g
:%s/old3/new3/g
:update
:quit
EOF
Share:
6,000

Related videos on Youtube

waldemar_enns
Author by

waldemar_enns

Primarily Informix-4GL Programmer, Linux Bash Programmer and Linux Administrator. Dabbler in Java, Python, HTML, PHP and JavaScript. Expert at breaking everything.

Updated on September 18, 2022

Comments

  • waldemar_enns
    waldemar_enns over 1 year

    This is related to a question I asked earlier but I would like to keep it separate in case anyone else can find use in it.

    I have a command that I am running on a file called dummy.out from the command line which is as followed

    vim -E -s dummy.out <<-EOF
       :%s/old/new/g
       :%s/old2/new2/g
       :%s/old3/new3/g
       :update
       :quit
    EOF
    

    from the command line this is working but when I add it to my bash file bashscript.sh

    #!/bin/bash
    # bash script
    
    var01="start script"
    echo $var01
    
    vim -E -s dummy.out <<-EOF
       :%s/old/new/g
       :%s/old2/new2/g
       :%s/old3/new3/g
       :update
       :quit
    EOF
    

    I get the following error

    warning: here-document at line 7 delimited by end-of-file (wanted 'EOF')
    

    How would I get this command line command to run inside this bash file?

    • wurtel
      wurtel about 9 years
      looks like typical line ending confusion. Did you edit it with a Windows editor?
    • Darek
      Darek about 9 years
      so this script works just fine for me: bash script.sh
  • Dani_l
    Dani_l about 9 years
    is your editor translating tabs to spacres? <<- only works on actual tabs
  • waldemar_enns
    waldemar_enns about 9 years
    It is yes but for this script I decided to leave out tabs all together