Emacs: Open a Bookmarked File and Save to a Bookmarked File

In Emacs, it is possible to bookmark a file at a certain location, or bookmark locations in any other Emacs’ buffers, such as in dired (Emacs’ file manager). Bookmarking is a quick way of finding and opening files that you need often.

Xah Lee has a good overview of Emacs’ bookmarks feature. Bookmarking provides a quick access to files that you need every day across the work week. After having saved some bookmarks, the most important commands to remember are:

  • bookmark-jump to open a prompt to go to a saved bookmark
  • bookmark-list to open the complete list of bookmarks.

The above commands provide access to the bookmarked files that you want to open up.

Xah Lee has written also a function to open file fast based on Emacs’ bookmark feature. Xah’s “Open file fast” prompts for a bookmarked file to be opened, i.e. it is a modified bookmark-jump.

But what if you want to save from current buffer into a bookmarked file?

Here’s some code (rewritten from Xah’s “Open file fast”):

  (defun overwrite-bookmarked-file ()
  "Prompt to save buffer contents to a bookmarked file in `bookmark-bmenu-list'.
This command overwrites the contents without further warning and
  switches buffer to the bookmarked file."
  (interactive)
  (require 'bookmark)
  (bookmark-maybe-load-default-file)
  (let (($this-bookmark
         (ido-completing-read "Overwrite bookmarked file:"
                              (mapcar (lambda ($x) (car $x)) bookmark-alist))))
    (set-visited-file-name (bookmark-get-filename $this-bookmark))))

As the description says, the above command overwrites the contents of the bookmarked file on the disk, erasing former contents, and switches Emacs to display the new contents.

If, in contrast, you want to append to the bookmarked file off the cuff (without switching to the bookmarked file), try the following:

  (defun append-to-bookmarked-file ()
  "Like `append-to-file' except prompt for a bookmarked file in
`bookmark-bmenu-list'. As with `append-to-file', a region must be selected for the command to work."
  (interactive)
  (require 'bookmark)
  (bookmark-maybe-load-default-file)
  (let (($this-bookmark
         (ido-completing-read "Append to bookmark:"
    (mapcar (lambda ($x) (car $x)) bookmark-alist))))
    (prog1 (write-region (region-beginning) (region-end) (bookmark-get-filename $this-bookmark) t)
    (when save-silently (message nil)))))

With the latter command, there will be no overwriting in the bookmarked file, only appending (adding to the end of the file). The destination file does not need to be open in Emacs. However, if the destination file happens to be open in Emacs and you want to take a look at the changes, use revert-buffer in the destination buffer or global-auto-revert-mode.

Conversely, it is also possible to insert the contents of a bookmarked file into a current Emacs buffer:

  (defun insert-bookmarked-file ()
  "Prompt to insert file contents from `bookmark-bmenu-list'.
This command is similar to `bookmark-jump', but uses `ido-mode' interface, and ignores cursor position in bookmark."
  (interactive)
  (require 'bookmark)
  (bookmark-maybe-load-default-file)
  (let (($this-bookmark
         (ido-completing-read "Insert from bookmark:"
    (mapcar (lambda ($x) (car $x)) bookmark-alist))))
    (insert-file-contents (bookmark-get-filename $this-bookmark))))

Leave a Reply

Your email address will not be published. Required fields are marked *