I have a system that I am not allowed permissions to install software but need to copy text from the remote server to the host computer's system clipboard. Because I can't install xclip or xsel and I am using emacs with tmux, the formatting of text gets all screwed up because of the newlines that tmux introduces. I thought of the possibility of writing regions to a file that I can remotely open the file and manually copy the properly formatted text.

I've created my own keyboard defined macro using write-regions that worked during a current open session of emacs but would have problems upon reopening. Error along the lines of command terminated by ringing the bell.

I've also used the following answer's code but the OP wanted to interactively set the file but in my case the file is known already so there is no need for me to set it each time I use the function.

Is there a way for me to write this function such that the a region automatically writes to ~/copybuffer.txt and doesn't delete the region afterwards. See original QA here and my code edits.

Unfortunately, in my code I see variable is void: start. I clearly don't understand something about defining lisp functions.

(defun copybuffer
  "function takes current region, and writes it to specified file"
  (write-region region-beginning region-end '~/copybuffer.txt' t)))

1 Answer 1


It's only you're having syntax issues, try this:

(defun copybuffer ()
  "function takes current region, and writes it to specified file"
  (write-region (region-beginning) (region-end) "~/copybuffer.txt"))


  • region-beginning and region-end are functions, so they have to be between parenthesis to be recognized as functions.
  • start and end should be variables which you haven't defined in your code, so I used region's limits, which should fit the bill in this case.
  • copybuffer didn't conform to the function definition syntax, and was closed early after write-region.
  • Added interactiveto be able to call it with M-x.
  • Removed t as this is an optional argument that appends to file instead of overwrites

Take a look to the elisp intro: C-h i m Emacs elisp intro.

  • 1
    Thank you so much! I will look into this more deeply - it really helps me understand what I didn't know earlier. Additionally, I've added some edits to correct for some details in my OP (e.g., no need to remove region after writing to file and function shouldn't append). Thank you again for the awesome answer.
    – LeanMan
    Jul 2, 2020 at 19:05
  • I'm noticing a bug in the above code. I'm not sure if it is an underlying IO call that emacs makes that is causing it or if its an issue with emacs. however this is the behavior. When I run the command once, it works fine. However if I run it a second time, it won't write to file. I have to either collect a new region or edit/save the ~/copybuffer.txt file in order for emacs to properly write-region to the file. In either case, the added indirection is undesirable.
    – LeanMan
    Jul 3, 2020 at 7:22
  • Cannot reproduce. Each time I execute the above function it writes and file changes. It may have exactly the same content, as expected, but stat ~/copybuffer.txt show changes in mtime and ctime.
    – Muihlinn
    Jul 3, 2020 at 8:05
  • any thing I can look into further to see what could be causing it? I do have ~/copybuffer.txt open the host computer over the network and also it does seem to clear up after I manually edit the file so it seems like some kind of write IO issue.
    – LeanMan
    Jul 3, 2020 at 16:23
  • I don't know, probably it's more related to your particular setup than anything else, or something as simple like not having auto-revert enabled for that buffer.
    – Muihlinn
    Jul 6, 2020 at 7:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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