Search code examples
bashvimterminalexex-mode

How to jump to a string in vim ex mode using a bash script


I want to move to the word "internal" in a text file "file1" and delete everything from there to the end of file from within a bash script. This is my vim command sequence :

/internal
execute "normal !dG"
w

When I save this in a text file and pass it to vim from the command line in ex mode, everything works fine. But when I try to do this from the terminal command prompt from outside vim :

echo "/internal | execute "normal! dG" | w" | vim -e file1

it doesn't work. I cat out file1 and I find that it hasn't changed. The text file file1 looks like this:

one 
one 
one 
one 
one 

internal

two
two
two
two
two

What am I doing wrong when trying to work from the terminal command prompt ?


Solution

  • For /internal | exec(..., all the string will be interpreted by /. You can write /internal on its own line. Like this:

    [STEP 101] $ cat file
    1
    2
    foo
    3
    4
    [STEP 102] $ printf 'norm gg\n/foo\n.,$p|q\n' | vim -e file
    foo
    3
    4
    [STEP 103] $
    

    Or use the search() function:

    [STEP 104] $ echo 'exec("norm gg") | call search("foo") | .,$p | q' | vim -e file
    foo
    3
    4
    [STEP 105] $