I had the exact same desire as you: I wanted to write normal vim commands and have those edit a pipeline stream. I was disappointed by the difficulty of using normal vim in this way (so many flags!), as provided by the default vim options.
Therefore, I wrote a small wrapper on vim to do it: https://github.com/MilesCranmer/vim-stream/. It's called vims
.
The heart of the wrapper is the following command:
vim - -nes "${vim_cmds[@]}" -c ':q!' | tail -n +2
Using this, with the command generation I do, it works out to be quite concise, and because vim is a modal editor, I made vims
a modal stream editor. There are three main modes which are useful for with pipeline-editing mentality:
[-t|--turn-off-mode] - (Default mode)
Every string is a vim command line, e.g., '%g/foo/d' deletes
all lines with foo.
[-e|--exe-mode] - Translated to '%g/$1/exe "norm $2"', see examples below
[-s|--simple-mode] - Like exe-mode, but runs on 1st line only
Watch this:
$ echo "Hello World" | vims -s "ea Beautiful"
Hello Beautiful World
So, e
goes to the end of Hello
, then a
starts appending.
Something fancier, in exe-mode, to comment out lines containing my_bad_var
, and delete the preceding line.
cat my_script.cpp | vims -e 'my_bad_var' 'I//\<esc>kdd'
Which translates to vims '%g/my_bad_var/exe "norm I//\<esc>kdd"'
- the I
being the command
to start insert at the start of the line, and //
being the comment sequence.
\<esc>kdd
pushes the escape key, moves up a line, then deletes the line.
I have 11 total examples on the repo, I encourage you to check them out. Again, this is running on top of vim, it's just a way to make vim more like sed for stream input (but preserving all your beloved vim macros!)
|
character pipes the output of one command to another.wea-this
is not a command. Piping cannot be used to achieve what you're looking to do either; it'll cause Vim's stdin to be the left hand application, not the keyboard. If you want to get stdin into a buffer in vim, start it with a minus for the filename:echo Hello, world! | vim -
.