4

To print the whole buffer, you can use %p, how do I print value of register to the standard output?

Here is example how I would print the whole buffer:

$ ex +'norm dw' +'%p' -scq! <(echo foo bar)
bar

So I'm looking for %p equivalent printing way for registers, for example to print the last deletion ("").

  • Why do you want that? – VanLaser Oct 16 '15 at 15:52
  • For example I've many columns in the file, and I want to delete selected column using ex and print what was deleted. – kenorb Oct 16 '15 at 15:57
  • E.g. This would remove 3rd column: ex +'%norm 2WdW' '+%p' -scq! <(echo 1 2 3 4 5) in shell, but I want opposite, to print what was actually deleted. – kenorb Oct 16 '15 at 16:00
  • Why don't you use other tools like awk? – romainl Oct 16 '15 at 18:40
  • @romainl The awk is more stream oriented, so there are some limitations in more complex parsing scenarios. On the other side awk is quicker, as I did some benchmarking recently, but I want to learn ex/vi more efficiently. – kenorb Oct 16 '15 at 22:04
3

Echo command can print the value of any register:

:echon @"

This output can be redirected to the file (or standard output in this case) by using

:redir > file
:echon @"
:redir END

So the working example is as below:

$ ex +'norm dw' +'redir>>/dev/stdout|echon @-' -scq! <(echo foo bar)
foo

Where @- register stores the last text that you deleted or changed.

Note: The /dev/stdout could not exist on some non-Unix systems.

Or workaround would be to replace the entire buffer with register and print it, like:

ex +'norm dw' +'%d|put-|%p' -scq! <(echo foo bar)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.