5

I have a few files that I have an annoyingly repetitive group of VIM commands to apply to. They are basic VIM commands just to delete some lines which are not supported by the process I need the files for.

Say I have a file called postgresdb.out

On this file I will

vim ./postgresdb.out 

Then from inside VIM I will

:%s/old/new/g 

and then

:g/deletethis/d 

followed by

:%s/\n\n//g 

then save the file and go about my day

I would like to know if it is possible to write a bash script that can do this process instead of retyping all the time

4
  • What is :%s/\n\n//d supposed to do? If I try it I get Trailing characters as the error message. So I think you either mean g or no modifier; or just do :g/^$/d. That said, I'd do this in perl using the -i and -pe options.
    – wurtel
    CommentedFeb 18, 2015 at 8:11
  • :%s/\n\n//d the d is a typo I fixed it aboveCommentedFeb 18, 2015 at 8:37
  • 1
    If you must use Vim, look at Ex mode: superuser.com/questions/22455/…CommentedFeb 18, 2015 at 8:59
  • jasonwryan that is part one of the answer I was looking for, running vim with -E and EOF now to put it into a bash scriptCommentedFeb 18, 2015 at 9:13

2 Answers 2

4

Alternatives

Unless you really need special Vim capabilities, you're probably better off using non-interactive tools like sed, awk, or Perl / Python / Ruby / your favorite scripting language here.

That said, you can use Vim non-interactively:

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 -i NONE -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 -i NONE -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. -i NONE Ignore the |viminfo| file (to avoid disturbing the user's settings). -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. 
    2

    automatic edition should be done using sed(1) (see man sed )

    The commend your are looking for are

     sed -i -e s/old/new/g -e /deletethis/d -e '/^$/d' postgresdb.out 

    I am not sure what you expect with :%s/\n\n//d.

    where

    • -i means edit in place (usually sed will output edition do standard output)
    • -e ... do the edition/deletion
    • -e '/^$/d' shoudl delete empty lines
    2
    • :%s/\n\n//d the d is a typo I fixed it aboveCommentedFeb 18, 2015 at 8:34
    • sed didn't operate newline(s) \n\n (exept very new versions with option -z). If you'd like to remove empty lines try wurtel recipie /^\s*$/d
      – Costas
      CommentedFeb 18, 2015 at 8:50

    You must log in to answer this question.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.