I want to access any current file quickly. Therefore I want to build a function that put a file the register.

I've started with this function:

(defun tmp-register ()
""" put the current buffer in the temporary register """
(set-register , ?q  (file . buffer-file-name))

but it fails as expected as I need a string instead of buffer-file-name. Do you see a way around this?


I have tweaked some code from Sacha Chua here. I've come up with this. I don't understand it entirely yet and it can probably be improved. Do you have something else in mind?

(defun tmp-register ()
""" put the current buffer in the temporary register """
(my/defshortcut ?q buffer-file-name)

(defvar my/refile-map (make-sparse-keymap))
(defmacro my/defshortcut (key file)
    (set-register ,key (cons 'file ,file))
 (define-key my/refile-map
   (char-to-string ,key)
   (lambda (prefix)
     (interactive "p")
     (let ((org-refile-targets '(((,file) :maxlevel . 6)))
           (current-prefix-arg (or current-prefix-arg '(4))))
       (call-interactively 'org-refile))))))

(global-set-key (kbd "<f12>") 'tmp-register)
  • 1
    FYI, """ is not a string delimiter in elisp.
    – npostavs
    Dec 31, 2015 at 18:03
  • (set-register , ?q (file . buffer-file-name)) is wrong: (1) it uses a comma outside of backquoting and (2) it provides 3 args, not 2, to set-register.
    – Drew
    Dec 31, 2015 at 18:56

2 Answers 2


OK, I have updated my answer to save the file being visited by the current buffer.

Try this slight variation on your original function:

 (defun tmp-register ()
  """ put the current buffer in the temporary register """
  (set-register ?q (cons 'file buffer-file-name)))

After calling this function (with M-x tmp-register RET (or by key combination if you set that) in a buffer that is visiting a file, it will be saved to the register and you will be able to jump back to that buffer with jump-to-register (bound to C-x r j for me) and then typing q (or however you access registers in your setup). Note that if you call the function in a buffer that is not visiting a file (e.g., the *Scratch* buffer, it will not work.

  • Hello thanks for the suggestion but your version doesn't seem to work for me. My end goal is really to jump to the file. I realize that I was not sufficiently clear on this
    – DJJ
    Dec 31, 2015 at 15:32
  • OK, let me update it. But just to be clear, you want to be able to jump back to the file, not access the name of the file, correct? You might want to edit this line in your post to be more clear: "want to build a function to put a buffer file name the register quickly" - it sounds like you just want to save the file name to me (maybe because you will need to use the file path elsewhere)
    – elethan
    Dec 31, 2015 at 15:36
  • Exactly. I've update the question. Hope it's clearer. The code I've posted in the update seems to work. But I guess that there is a lot of things we don't need to perform the task I want.
    – DJJ
    Dec 31, 2015 at 15:40
  • If this solution works for you, I would suggest removing everything in your question after Update because, as you say, there is a lot going on there, and it makes things more confusing for future readers.
    – elethan
    Dec 31, 2015 at 16:11
  1. "I want to access any current file quickly. Therefore I want to build a function that put a file the register."

    I hope you realize that Emacs-Lisp "registers" are not computer registers. There is nothing inherently super-quick about them. A Lisp register is essentially a pointer to an object in memory, like a variable.

    Of course there is nothing wrong with using a register to save the content of a file or buffer. I just wanted to point out that using a register is not quicker than other means of doing the same thing.

  2. As an alternative, consider using Emacs bookmarks. They can be persistent or temporary; they point to file or buffer locations; and they are accessed quickly.

    Of course, if a file has not yet been visited or the buffer does not yet exist, then there is the cost of visiting that data. But that is true however you proceed. Once a buffer exists that corresponds to the data you want, jumping to it using a bookmark is immediate.

    It's the same as using a register, except that (1) a bookmark can have any name you like -- not just a character, and (2) a bookmark can be persistent.

  • I see your point. I will have a look at bookmarks.
    – DJJ
    Jan 1, 2016 at 12:23

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.