Live data from Hacker News

Use Vim Inside a Unix Pipe Like Sed or AWK

blog.robertelder.org

11–20 of 49 posts

Re: Use Vim Inside a Unix Pipe Like Sed or AWK

#11
post #10

This post hints toward vim's history: the ed family of editors. http://blog.sanctum.geek.nz/actually-using-ed/ http://blog.sanctum.geek.nz/using-more-of-ex/ A big use case for them was using them as scriptable editors in pipelines, and a lot of vim's commands are inherited from them. The diff(1) utility actually has an option to emit ed script, allowing files to be patched in ed pipelines: http://www.gnu.org/software…

It's common for people today to think of ancient Greeks as being very unsophisticated and primitive, but then when you start to read what ancient philosophers wrote you think "Wow, these guys really had things figured out, maybe better than we do now." I kind of feel the same way comparing modern GUI applications to ancient editors like ed.

Re: Use Vim Inside a Unix Pipe Like Sed or AWK

#12

Yikes, we can shorten that a bit. :-) function vimify() { (vim - -esbnN -c $@ -c 'w!/dev/fd/3|q!' >/dev/null) 3>&1 } $ echo ' Change Me ' | vimify 'norm vitxiOK I Will' (Don't forget '-c' if you have more than one transform.)

And further shorten that to

echo 'Change Me' | vimify 'norm vitcOK I Will'

Re: Use Vim Inside a Unix Pipe Like Sed or AWK

#14

Yikes, we can shorten that a bit. :-) function vimify() { (vim - -esbnN -c $@ -c 'w!/dev/fd/3|q!' >/dev/null) 3>&1 } $ echo ' Change Me ' | vimify 'norm vitxiOK I Will' (Don't forget '-c' if you have more than one transform.)

For robustness sake, you may change $@ with "$*".

Re: Use Vim Inside a Unix Pipe Like Sed or AWK

#17
Vim is actually a relatively bad filter (some reasons are mentioned in the caveats section of the article). I explicitly designed my vis editor[1] in such a way that it can be used as an interactive filter. The following works as expected, use :wq to write to stdout:

    printf "foo\nbar\nbaz\n"  | vis - | sort

dvtm uses this mechanism to implement its copy mode.

I also recently integrated sam's structural regular expression based command language into the editor. This might be useful for people who want the power of stream editors but with instant visual feedback.

However keep in mind that this feature is relatively new thus likely still contains some bugs ...

[1] https://github.com/martanne/vis

Re: Use Vim Inside a Unix Pipe Like Sed or AWK

#18

Vim is actually a relatively bad filter (some reasons are mentioned in the caveats section of the article). I explicitly designed my vis editor[1] in such a way that it can be used as an interactive filter. The following works as expected, use :wq to write to stdout: printf "foo\nbar\nbaz\n" | vis - | sort dvtm uses this mechanism to implement its copy mode. I also recently integrated sam's structural regular express…

Thanks for your efforts with the Vis editor...

You can do this with any other editor using "vipe" from "moreutils" package. It just opens $EDITOR with stdin input (put to a tempfile) and then reads the tempfile and dumps it in stdout.

    printf "foo\nbar\nbaz\n"  | vipe - | sort  # invokes $EDITOR

Re: Use Vim Inside a Unix Pipe Like Sed or AWK

#19
post #10

This post hints toward vim's history: the ed family of editors. http://blog.sanctum.geek.nz/actually-using-ed/ http://blog.sanctum.geek.nz/using-more-of-ex/ A big use case for them was using them as scriptable editors in pipelines, and a lot of vim's commands are inherited from them. The diff(1) utility actually has an option to emit ed script, allowing files to be patched in ed pipelines: http://www.gnu.org/software…

It's common for people today to think of ancient Greeks as being very unsophisticated and primitive, but then when you start to read what ancient philosophers wrote you think "Wow, these guys really had things figured out, maybe better than we do now." I kind of feel the same way comparing modern GUI applications to ancient editors like ed.

Which is why I have been learning and using emacs daily for a few years now, and it's amazing how much more powerful it is than I ever seem to fully understand. I'll think "now I have it all like I want it", and then a few months later I learn about a way to do X.

Especially as a sysadmin, I spend so much time in a ssh cli, that part of my reasoning was "I want to be able to do all my normal tasks without the gui." Gmail, news, and rss in gnus, irc in erc, org-mode (loving export to latex for reports), eww for browsing, and who knows whats next. Elixir and go modes are improving too, and my general productivity due to staying mostly inside a single ecosystem has really improved.

Another reason I have done this though, is I feel like it's less about gui vs text, and much more about FOSS vs proprietary. In a few years when everyone has an iBrain with Apple (NSA) inside, I intend to have a MEmacs brain that I have control over.

I think RMS will be vindicated in history as a man far ahead of his time.

Re: Use Vim Inside a Unix Pipe Like Sed or AWK

#20
Why is this article talking about idempotence? I don't see why that matters in the slightest for a unix pipe. Idempotence only matters if you're going to take the output of the pipe and run it back through the pipe again.

There's a bit later on about idempotence and newlines, but I don't see what that has to do with idempotence. Vim will append a trailing newline if the file doesn't have one, sure, but if I pass the output back through Vim again, it won't append another trailing newline, it'll just preserve the existing one, so if the vim command you run is idempotent then the whole thing will still be idempotent.

I'm guessing here that the author is simply confused about what idempotence means. Because of the newline bit, I'm going to guess that what the author means by "idempotence" is "any text that the vim filter isn't supposed to modify will be left exactly as found in the input", but that's not what idempotence is, and I'd also say that that property is pretty much a given for any kind of text transformation used in a pipe, because if it doesn't hold then why are you even using that tool?

Post reply on HN