How to run a series of vim commands from command prompt

后端 未结 5 1448
野的像风
野的像风 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:59

    The amount of -c commands directly passed to Vim on the command-line is limited to 10, and this is not very readable. Alternatively, you can put the commands into a separate script and pass that to Vim. Here's how:

    Silent Batch Mode

    For very simple text processing (i.e. using Vim like an enhanced 'sed' or 'awk', maybe just benefitting from the enhanced regular expressions in a :substitute command), use Ex-mode.

    REM Windows
    call vim -N -u NONE -n -es -S "commands.ex" "filespec"
    

    Note: silent batch mode (:help -s-ex) messes up the Windows console, so you may have to do a cls to clean up after the Vim run.

    # Unix
    vim -T dumb --noplugin -n -es -S "commands.ex" "filespec"
    

    Attention: Vim will hang waiting for input if the "commands.ex" file doesn't exist; better check beforehand for its existence! Alternatively, Vim can read the commands from stdin. You can also fill a new buffer with text read from stdin, and read commands from stderr if you use the - argument.

    Full Automation

    For more advanced processing involving multiple windows, and real automation of Vim (where you might interact with the user or leave Vim running to let the user take over), use:

    vim -N -u NONE -n -c "set nomore" -S "commands.vim" "filespec"
    

    Here's a summary of the used arguments:

    -T dumb           Avoids errors in case the terminal detection goes wrong.
    -N -u NONE        Do not load vimrc and plugins, alternatively:
    --noplugin        Do not load plugins.
    -n                No swapfile.
    -es               Ex mode + silent batch mode -s-ex
                    Attention: Must be given in that order!
    -S ...            Source script.
    -c 'set nomore'   Suppress the more-prompt when the screen is filled
                    with messages or output to avoid blocking.
    

提交回复
热议问题