1

Here's how you can automate vim in an interesting way:

vim -c '0,$d | r source.txt | 1d | w | q' dest.txt

This uses vim ex commands to erase dest.txt, read source.txt into the buffer, erase the first line (which ends up as a blank line due to the way r works), write to the file (dest.txt), and then quit.

This (as far as I can tell) skips the entire vim terminal UI from loading and is conceptually a little like having a vimscript interpreter.

Now I'd love to be able to take this just one little step further to abuse the capabilities of vim: I want a script to peer at the currently edited changes of an opened file (as part of an interactive automation shell script) which exist in the vim *.swp swapfiles, apply the changes through vim's recover command, and then obtain the output.

Of course it would be perfectly serviceable to use an actual file, e.g. orig_file.txt is being edited in vim in another terminal; my script could do this at each point that the swapfile is detected to change:

cp orig_file.txt orig_file_ephemeral.txt
cp .orig_file.txt.swp .orig_file.txt_ephemeral.txt.swp
vim -c 'recover | w | q'

At this point orig_file_ephemeral.txt shall contain the content of the vim buffer from the other process in which editing is taking place, and we obtained this data without requiring any direct interaction with said process. Which is neat.

Of course for practical purposes it would probably make more sense to do exactly that, and just have the primary vim participate in the process. It would be splitting the functionality for the script out into the configuration of vim, which is a downside, but it would be more straightforward conceptually and computationally as it already has the buffer contents readily available for writing, and it should be straightforward to do so as I believe there exists an autocommand we can use (though whether that autocommand is run prior to saving the swapfile or not remains to be seen).

Either way, for the sake of completeness I'm curious to know if there exists an ex command to write stuff to the STDOUT of vim. Or if this even makes any sense.

I think it perhaps makes no sense as STDOUT is bound to be the actual terminal, e.g. it is where vim sends out its "view" of its UI and the buffer, and everything, to the terminal. So that for example if any of the vim -c 'vimscript commands' commands produce vim errors, I'll be seeing vim's terminal output to display these errors over STDOUT.

Therefore it may only be practical to use a file. But maybe there's some kind of craziness like !tee /dev/fd/3 I could do?

In addition, there is a wrinkle with this roundabout approach, which is that vim presents a Warning: Original file may have been changed error in bright red background text for about a second, and this is surely due to renaming the file. I can likely work around that by doing this work inside of a sub-directory while keeping the filenames identical.

Steven Lu
  • 41,389
  • 58
  • 210
  • 364
  • Oh that's nice. Not that I foresee ever making use of this. ;-) – Steven Lu Jun 24 '13 at 04:30
  • 1
    possible duplicate of [Redirect ex command to STDOUT in vim](http://stackoverflow.com/questions/16739300/redirect-ex-command-to-stdout-in-vim), or of [write buffer content to stdout](http://stackoverflow.com/questions/3219479/vim-write-buffer-content-to-stdout) – Ingo Karkat Jun 24 '13 at 06:24
  • Although the question is a duplicate, Stephane Chazelas's answer below is by far the bast answer given for either. – jthill Oct 31 '14 at 15:15
  • This comment is really more of a note to self. The purpose of this question was to build a component of a system where I am coding away in Vim and in another terminal session I have an automated process that (on a timer, I guess) copies out the undofile and *applies* it, in order to temporarily obtain the "realtime version" of the file. Then, I generate a diff so I can view the diff of my code being written in realtime. However, I have found no need for such a tool (and the CPU overhead it will likely introduce) yet as it's pretty easy to keep track of things. It's one thing I miss about MSVS. – Steven Lu Nov 01 '14 at 17:57

1 Answers1

2

That's the p command (and where the p in grep comes from):

ex -sc '%p|q' file

Would be a bit like cat file.

Stephane Chazelas
  • 5,859
  • 2
  • 34
  • 31