[SOLVED] How to make Vim (scripts) write to stdout? (or, how to use Vim as a filter program?)
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Why use vi(m), an editor when it comes down to it, in the first place? Other, better tools are available for filtering (sed and awk to name just 2). Vi(m) looks like the wrong tool for the job in this case.
I'm sure that all the actions done in the vi(m) script can also be done with other, more suitable tools.
Vim is scriptable. Why not use it? Because it is an editor and I'm more familiar with its commands than with awk? I think not.
Vim looks like the wrong tool? Why? The example command I gave was just to show the chained use.
I know how to do everything I need now, in Vim. I only stuck in the final part, the standard output... since the problem is this simple, I didn't even bother to comment what "processing" I'm doing with the Vim script, it is irrelevant.
@schneidz
I'm not trying to filter out things, in this sense. I'll use Vim to make transformations on text and give the output to another command. I can do these transforms in Vim, but not in other programs (although I know how to use, a bit, a few).
Vim is scriptable. Why not use it? Because it is an editor and I'm more familiar with its commands than with awk? I think not.
Scripted vim is sed. Most of the commands used in vim are designed for use with a file going backward/forward in direction - scripting an edit is not a stream.
Quote:
Vim looks like the wrong tool? Why? The example command I gave was just to show the chained use.
vim has at its base the editing commands of sed. sed is a much smaller and faster working program than vim.
Quote:
I know how to do everything I need now, in Vim. I only stuck in the final part, the standard output... since the problem is this simple, I didn't even bother to comment what "processing" I'm doing with the Vim script, it is irrelevant.
@schneidz
I'm not trying to filter out things, in this sense. I'll use Vim to make transformations on text and give the output to another command. I can do these transforms in Vim, but not in other programs (although I know how to use, a bit, a few).
Vim is scriptable. Why not use it? Because it is an editor and I'm more familiar with its commands than with awk? I think not.
Vim looks like the wrong tool? Why? The example command I gave was just to show the chained use.
I know how to do everything I need now, in Vim. I only stuck in the final part, the standard output... since the problem is this simple, I didn't even bother to comment what "processing" I'm doing with the Vim script, it is irrelevant.
@schneidz
I'm not trying to filter out things, in this sense. I'll use Vim to make transformations on text and give the output to another command. I can do these transforms in Vim, but not in other programs (although I know how to use, a bit, a few).
vim is the wrong tool for the job. Why? Well, you just started a thread about one of the reasons.... This tool is not meant to be used in this way.
(s)ed is what is mostly used by vi(m). Why not take the jump and start using sed?
Btw: There is a command that lets you define an editor to be used in a pipe: vipe (from the moreutils package). Never used it myself and I don't know if any (automated) vim scripting is allowed/possible.
All right, it's nice to discuss these things, but it doesn't answer the question. If it is *possible* to do it with Vim, I want to find out how.
If someday after learning to work better with other tools I laugh at this, well, there is nothing wrong with it.
@jpollard
Your definition of "right tool" is not like mine, for this situation.
Even the "base of Vim" being the same commands sed has, Vim has more, agree?? They're different. Or else, there should be a tool to convert Vim editing scripts to sed scripts available...
Quote:
Quote:
make transformations on text and give the output to another command
Which is exactly what sed is for.
Not only sed, but a gazillion programs (read text on stdin, write *anything* to stdout). It was a very loose definition.
I just played around a bit with vipe (see post #6) and with a little trickery you can achieve what you are after (command | <vim+script> | command).
vipe expects the EDITOR variable to be set to your editor (EDITOR='vim'). You can add the -s testScript.vim part, i.e.: EDITOR='vim -s testScript.vim'.
The following can now be done:
Code:
command | vipe | command
WARNING: Other commands might also look at the EDITOR variable and will start behaving unexpected when the -s part is added!!
This will set the EDITOR variable for use by vipe only:
I don't think it's possible to "stream" input into vim, but you CAN use it in scripts. You can supply it with ex-mode commands, which it will execute directly on the given file without opening up the buffer visually.
You just need to use the -e option for ex mode, and either the -c option to pass commands directly as arguments or the -s option to control it through stdin (see the help documentation for details).
And the "file" can be a process substitution or named pipe (or just use the 'r !' ex command, for that matter), so you can operate on the output of commands too.
As an example, the following code will strip ascii color codes from the output of ls:
Code:
printf '%s\n' '%s/\e[[][0-9;]\+m//g' '%p' | vim -es <( ls --color=always )
Check the web for lots more on what vim can do. This wiki, for example: http://vim.wikia.com/
Edit: One more hint for you. I recently discovered that there's a 'norm' command, which lets you execute normal mode commands in ex mode, giving you even more power.
In any case though, I'd recommend using ed instead for much of this. It operates similarly to how vim does above, in a more limited, but lighter fashion.
And now I heard about vipe and moreutils package. Really good!
@unSpawn
Quote:
Heh, that reminded me I used to script Vi before I learned to use other tools ;-p
LOL, I feel that's exactly the situation I'm in!
My situation don't need efficiency. So, no worry about the bloat of Vim in the face of sed or awk.
That pipe.vim script is very cool! It can make you feel like a filter program...
Tested pipe.vim (ugly source, Perl does, eek!), but works charmly!
-------------------
I only like to think of using sed when I can easily think of the "editing" in one pass. Moving around with SED, multiline spaces, etc... gets too ugly for me - maybe for the lack of practice. It is a matter of time, anyway. I'm confident that I can do it with awk, after I learn it. Not now...
... @jpollard
Your definition of "right tool" is not like mine, for this situation.
Even the "base of Vim" being the same commands sed has, Vim has more, agree?? They're different. Or else, there should be a tool to convert Vim editing scripts to sed scripts available...
Where do you thing vi/vim started from? vi was just a front end to ed, which was a file level sed rather than a stream sed (you could go back to the beginning of a file).
There are more commands... but they are also aimed at a terminal and generate escape sequences, highlighting, backlighting, terminal dependent tabbing, cursor movement, restarting edits, undoing edits, ... None of which work when there is no terminal for such interactive functions, and thus unusable.
Quote:
Not only sed, but a gazillion programs (read text on stdin, write *anything* to stdout). It was a very loose definition.
Yup. Things that vi/vim isn't designed for and is not very good at.
Thanks, [b]David the H.[b]. I didn't had the time to read your post last week. You pointed some nice things. I have used Vim in scripts before, but not using Ex mode like you showed.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.