How to run a series of vim commands from command prompt

后端 未结 5 1455
野的像风
野的像风 2021-01-30 16:17

I have four text files A.txt, B.txt, C.txt and D.txt I have to perform a series of vim editing in all these files. Currently how I am doing is open each files and do the same vi

5条回答
  •  面向向阳花
    2021-01-30 16:50

    Try the following syntax:

    ex foo.txt <<-EOF
      g/^\s*$/d
      %s/^/[/
      %s/\(.*\)\(\s\+\)\(.*\)/\3\2\1
      %s/$/]/
      %s/design/test/
      wq " Update changes and quit.
    EOF
    

    The ex command is equivalent to vim -E. Add -V1 for verbose output.

    Alternative one-liner syntax is for example:

    ex +"g/^\s*$/d" +"%s/^/[/" +"%s/design/test/" -cwq foo.txt
    

    To load commands from the file, use -s cmds.vim.

    You can also use shebang for Vim to parse the file from the argument.

    For more examples, see:

    • How to edit files non-interactively (e.g. in pipeline)?
    • BashFAQ/021

提交回复
热议问题