I have consolidated documents and other files in the homelab server such as:
- Memos and personal wiki pages (.org)
- Tax documents (.org, .pdf)
- Apartment leases (.org, .pdf)
- Holiday card-related info (.csv, .pdf)
- Calendar (.pdf, .py)
Many files can be accessed from Emacs via tramp. But in some cases, I need local copies which either Emacs can’t handle well, need to access with local apps or need to access offline.
An alternative and probably easier way is to make those files available by exporting a directory (ie, file server). But as files are in many directories and I don’t want to export everything, I haven’t set that up yet.
Accessing remote PDF files
Usually, I can read (remote) PDF files with pdf-tools from Emacs. But one of my MacBooks is 13 years old, and it’s too old to install the package. Instead, I download a PDF file to local /tmp directory and open it with Preview app. From dired in Emacs, I click C-. to invoke embark, and then type L. The PDF file is opened in Preview app. Here is the helper setting from my init.el, which was generated by generative AI.
(defun my-embark-open-remote-pdf-locally (file)
"Copy remote FILE locally and open with system PDF viewer."
(interactive "fPDF file: ")
(let* ((local-copy (concat "/tmp/" (file-name-nondirectory file))))
(copy-file file local-copy t) ;; Copy from TRAMP path to local /tmp
(start-process "open-pdf" nil
(cond
((eq system-type 'darwin) "open") ;; macOS
((eq system-type 'gnu/linux) "xdg-open") ;; Linux
((eq system-type 'windows-nt) "start")) ;; Windows
local-copy)))
;; Add to Embark file actions
(require 'embark)
(define-key embark-file-map (kbd "L") #'my-embark-open-remote-pdf-locally)
This is also convenient to create a temporary local copy for offline access later.
Create a local copy of a remote directory
Some times, I need to create a local copy of a remote directory. It could be that I have Microsoft Word, Excel or PowerPoint files that I don’t want to open them in Emacs. It might also be that I want to access them offline.
There are many ways to create a local copy of a remote directory. My preferred way is to use dired-rsync in Emacs. On the directory in dired you’d like to copy, type C-c C-r and specify the target directory. This is the setting in my init.el.
;; use rsync for copying
(use-package dired-rsync
:ensure t
:bind (:map dired-mode-map
("C-c C-r" . dired-rsync))
:config (add-to-list 'mode-line-misc-info
'(:eval dired-rsync-modeline-status 'append)))
Not like C (copy) in dired, it doesn’t block while copying large files. You can use dired-rsync for both from local to remote and from remote to local.
From command line, I do something like this:
ssh user@remote_host "cd /path/from; tar czf - dir" | tar xzf -
find dir -type f -exec chmod -w {} \; # (optional) make files read-only