Reputation: 5188
How can I access the contents of a modified buffer in Vim?
For example, if I want to concatenate the contents to a temporary file, like:
:! cat % > /tmp/modified.txt
But that gives me the last saved contents of the file (rightly so?). I do want to avoid saving the file before because this interaction is meant to allow some analysis without saving the buffer first.
It seems that the Python extension for Vim allows you to do something like:
def buffer_contents(buffer=vim.current.buffer):
contents = buffer[:]
But I can't find any VimL
references for the same functionality.
EDIT: It seems I could do something like:
:let buffer_contents = join(getline(1, '$'), '$')
At this point I just wonder if there is a builtin approach.
Upvotes: 0
Views: 705
Reputation: 172540
Though you can use getline(1, $)
to retrieve all (modified) lines in the buffer, when your goal is writing them to a file, the :w! > filename
as per ZyX's answer is still the way to go. Though there is a writefile()
function in Vimscript, you'd have to deal with encodings, line endings, etc. all on your own, and that's simply too cumbersome when the built-in :write
can do it for you.
Upvotes: 1
Reputation: 53604
If you want to append current contents to temporary file you can use
:w! >> /tmp/modified.txt
,
:w! > /tmp/modified.txt
for overwriting that file (like in your cat example). To pass it to stdin of some script
:w !some-script
. Wondering though what’s wrong with your getline(1, '$')
?
Upvotes: 2