Live data from Hacker News

Use Vim Inside a Unix Pipe Like Sed or AWK

blog.robertelder.org

21–30 of 49 posts

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

#21
post #4

I have to say the only time I ever pipe directly to vim in a real world scenario is when I want to see a diff in color: diff file1 file2 | vim - Gives you the diff but colorized with Vim. Handy little trick.

Or just: vim -d file1 file2.

This brings up a side by side comparison of the documents. This is different than the pure diff.

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

#23
post #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…

That bugged me too, although it was such a fun article I didn't want to complain. :-)

What he really means is that his first snippet is the identity function. Like x*1=x or x+0=x. Whatever it gets, it passes along unmodified. I think it's actually kind of nice that he started with that. Sometimes it is not easy to figure out how to do, for instance with XLST.....

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

#24
post #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…

I suppose you're correct about the terminating newline case, in that the newline is only added once. I'm not sure what you'd call it, but I'm talking about it as if it were a kind of idempotent 'base case'. For example:

If you do

    echo -en "" | ( (vim - -esbnN -c 'w!/dev/stderr|q!' >/dev/null) 2>&1) | xxd
versus

    echo -en "" | ( (vim - -esbnN -c 'w!/dev/stderr|q!' >/dev/null) 2>&1) | ( (vim - -esbnN -c 'w!/dev/stderr|q!' >/dev/null) 2>&1) | xxd
These will the same output, so it is idempotent. However, if you build upon that and do

    echo -en "" | ( (vim - -esbnN -c '%!xxd' -c 'w!/dev/stderr|q!' >/dev/null) 2>&1) | xxd
versus

    echo -en "" | ( (vim - -esbnN -c '%!xxd' -c 'w!/dev/stderr|q!' >/dev/null) 2>&1) | ( (vim - -esbnN -c '%!xxd' -c 'w!/dev/stderr|q!' >/dev/null) 2>&1) | xxd
The result is not the same, and therefore not idempotent if you plan to add extra -c commands.

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

#25
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 philosophers? The only one I've read in any depth is Aristotle, and while he is clearly very smart, I agreed with essentially none of it.

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

#27
post #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…

That bugged me too, although it was such a fun article I didn't want to complain. :-) What he really means is that his first snippet is the identity function. Like x*1=x or x+0=x. Whatever it gets, it passes along unmodified. I think it's actually kind of nice that he started with that. Sometimes it is not easy to figure out how to do, for instance with XLST.....

Yeah, an identity function is probably what I should be calling it instead.

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

#28
post #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…

[deleted]

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

#29
post #25

Earlier quoted context omitted.

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 philosophers? The only one I've read in any depth is Aristotle, and while he is clearly very smart, I agreed with essentially none of it.

https://en.wikipedia.org/wiki/Lorem_ipsum

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

#30

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'

And further shorten that to

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

Post reply on HN