How to make Vim (scripts) write to stdout? (or, how to use Vim as a filter program?)
Hi
This sounds like a simple idea, but I'm having no luck. The basic idea is to use vim as a filter program, like sort is. I made a script for vim, to be executed and the results written on stdout. My intended command line should be something like: Code:
ls -l | vim -s testScript.vim - | cat I know how, but I didn't want to use temporary files, e.g.: Code:
ls -l > /tmp/file; vim -s testScript.vim /tmp/file; cat /tmp/file| ... |
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. |
what are you trying to filter out.?
are you trying to recreate grep for learning purposes ? |
@druuna
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). |
Quote:
Quote:
Quote:
|
Quote:
(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:
|
@dedec0: You cannot dot it with using just vim.
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 This will set the EDITOR variable for use by vipe only: Code:
command | EDITOR='vim -s script.vim' vipe | command |
why dont you explain what you are trying to achive (and provide a before and after picture) and maybe someone will provide a solution.
|
Heh, that reminded me I used to script Vi before I learned to use other tools ;-p
Quote:
|
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 ) 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. How to use ed: http://wiki.bash-hackers.org/howto/edit-ed http://snap.nlc.dcccd.edu/learn/nlc/ed.html (also read the info page) Notice that ed can also operate on the output of commands directly, by using them in place of a filename, prepended with "!". Of course if you want to get really fancy, switch to perl or something like that. |
@druuna
I can do it with Vim. I have done it now. And now I heard about vipe and moreutils package. Really good! :) @unSpawn Quote:
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... Thank you all! |
Quote:
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:
|
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.
|
All times are GMT -5. The time now is 05:11 AM. |