LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   How to make Vim (scripts) write to stdout? (or, how to use Vim as a filter program?) (https://www.linuxquestions.org/questions/linux-software-2/how-to-make-vim-scripts-write-to-stdout-or-how-to-use-vim-as-a-filter-program-4175448167/)

dedec0 02-01-2013 08:55 AM

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 have tried to do ":w! /dev/stdout" inside the Vim script, but it didn't work well.

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| ...
This doesn't really fit my needs, and it's a bit ugly too. :)

druuna 02-01-2013 09:05 AM

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.

schneidz 02-01-2013 09:14 AM

what are you trying to filter out.?

are you trying to recreate grep for learning purposes ?

dedec0 02-01-2013 10:29 AM

@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).

jpollard 02-01-2013 11:04 AM

Quote:

Originally Posted by dedec0 (Post 4882365)
@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.

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).
Which is exactly what sed is for.

druuna 02-01-2013 11:17 AM

Quote:

Originally Posted by dedec0 (Post 4882365)
@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).

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.

dedec0 02-01-2013 11:34 AM

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.

druuna 02-01-2013 12:15 PM

@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
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:
Code:

command | EDITOR='vim -s script.vim' vipe | command
Like I said, wrong tool for the job ;)

schneidz 02-01-2013 12:20 PM

why dont you explain what you are trying to achive (and provide a before and after picture) and maybe someone will provide a solution.

unSpawn 02-01-2013 12:25 PM

Heh, that reminded me I used to script Vi before I learned to use other tools ;-p


Quote:

Originally Posted by dedec0 (Post 4882411)
Your definition of "right tool" is not like mine, for this situation.

You don't say. About any post you find about using Vim in a pipe says it's like using a small tactical nuke where a double tap would do... Anyway how about pipe.vim : make vim part of a unix pipe and allow it to edit the pipe contents?

David the H. 02-01-2013 12:41 PM

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.

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.

dedec0 02-01-2013 01:11 PM

@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:

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...

Thank you all!

jpollard 02-01-2013 01:54 PM

Quote:

Originally Posted by dedec0 (Post 4882411)
...
@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.

dedec0 02-04-2013 05:45 AM

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.