Sunday, April 1, 2012

Re: Write register contents to file

On 01/04/12 11:00, howard Schwartz wrote:
> I would like to write the contents of a register to a file. Easy enough
> to grab the contents with let variable = getreg('a'). It would then be easy
> if :echo variable > file worked like unix echo, but it doesnt. Any
> simple way to get contents of some register a into a file of my
> choosing? There is a writelist() function, but I do not know how to get
> the context of a register into a list.
>
>


As an alternative to all that's already been said, you could also do the
following:

:new file | $put a | x

which can be assigned to the {rhs} of a mapping.

Pro: no need for an auxiliary variable

Con: a new window is opened and closed. This, however, should happen too
fast to be noticeable except that:
- it requires a Vim compiled with +windows (rarely a problem)
- if you're at the |hit-enter-prompt| (q.v.), it clears it.

The snippet above appends the register at the end of the file (and after
an empty line if the file didn't exist yet). Replace the dollar sign by
a zero to insert at top, by a line number (see |[range]|) to insert
after that line, or by nothing to insert after the current (usually
latest used) line in the file.

It is possible, with an if clause, to make the register contents the
only contents of the file if it didn't yet exist. The details are left
as an exercise to the reader. (Hint: see |filereadable()| and
|filewriteable()|)

See also |:execute| to read the file name (and path) and/or the register
name from a variable.


Best regards,
Tony.
--
I've enjoyed just about as much of this as I can stand.

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: