2

While editing buffer A, I would like to append some text to buffer B without leaving buffer A. How can I achieve that?

My ultimate goal is to form a collection of words that I cherry-pick from the buffer I am currently editing without having to switch buffers or windows.

My plan was to write a mapping to yank <cword> and append it to another buffer in its own line, but I can't figure out how to write to a different buffer.

  • excellent question, excellent answer. solved my puzzle. – qeatzy May 12 '18 at 0:57
3

As you said in your post, you can grab the word under the cursor with the special keyword <cword> (or <cWORD> if you want a big word), but before writing it inside a buffer you need to expand it :

expand("<cword>")

Next, you need a function to write into a buffer.
When you are looking for a function, type :help function-list.
It will give you a list of all the functions available sorted by category.
In this buffer of the help, if you search for the word file, you will find a category of functions that allow you to manipulate files :

:help file-functions

At the bottom of this category, there's a function that seems to do what you want (write into another buffer) : writefile().

For more details on how to use it, read :h writefile(.

To write a word inside a buffer and append it to its existing content here is the syntax given by the help :

call writefile(["foo"], "event.log", "a")

You just need to replace "foo" with the word under the cursor and "event.log" with the name of the buffer in which you want to append some text (let's say for example "file").
It gives something like this :

call writefile([expand("<cword>")], "file", "a")

Next, you need a mapping to do it automatically :

nnoremap <leader>a :<c-u>call writefile([expand("<cword>")], "file", "a")<cr>

Here I've chosen <leader>a as a mapping, but you can choose another one.

Finally, you could have the following message error :

Warning: File "{filename}" has changed since editing started

The solution given by the help is to set an option in your $MYVIMRC :

set autoread

Two possible improvements :

To write in the alternate file (the file that was last edited in the current window) instead of file :

nnoremap <leader>a :<c-u>call writefile([expand("<cword>")], expand("#"), "a")<cr>

In case you use a split window, to update all the splits, you can use the following command :

windo e

So the previous mappings become :

nnoremap <leader>a :<c-u>call writefile([expand("<cword>")], "file", "a") \| windo e<cr>

and

nnoremap <leader>a :<c-u>call writefile([expand("<cword>")], expand("#"), "a") \| windo e<cr>
  • Thank you! This works nicely to do what I asked, but I now realize that it would also be nice to be able to update the window the buffer ist being show in. autoread doesn't seem to be able to do that, unfortunately. – Vitor Eiji Nov 20 '15 at 18:02
  • I'm really glad I could help you a little bit ! I'm sorry, I don't understand, what do you mean by updating the window ? You want another mapping that would display the buffer in which you copy your words inside the current window ? Or modify the existing mapping that copy a word so that it also automatically switch to the other buffer ? I don't know if it can help but there's a command that can redraw the window : :redraw! – saginaw Nov 20 '15 at 18:15
  • What I mean is that I have the main buffer in one vim window and the other one, where I'm appendind the words, open in another vim window, which is being displayed at the same time (a :vsplit). When I use the mapping you suggested, the file is updated but that change is not reflected on the buffer until I switch to its window and :e. – Vitor Eiji Nov 20 '15 at 19:58
  • Ok, I understand, I was not using a split window, so I didn't realise the display was not updated. I think you can force an update on all windows with the command windo e. I've edited the answer to add it in the mappings. I hope it will be enough to solve the problem. – saginaw Nov 20 '15 at 20:23
  • 1
    Thank you, this is great. I'll use that in conjunction with this to prevent the focus to leave the main window. Thanks again! – Vitor Eiji Nov 21 '15 at 0:15

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.