0

So I have a couple of VM I'm using (mainly for experiment) I use Tramp a lot on a particular file in the VM, so I started to use this:

(find-file "/ssh:[email protected]:/home/vm/test.org" t)

I put that in my init file, so it always connects when I launch emacs... except that most of the time, I didn't plan on using the vm, so it cannot connect to it (since the vm isn't opened before emacs is launched) emacs stop halfway in loading the init file.

So how could i make it so the aforementioned line of elisp would only open the file if the address is reachable?

5
  • 1
    Try (file-exists-p “/ssh:vm@...”) to see if it returns a more timely negative result.
    – InHarmsWay
    Commented May 14, 2020 at 3:01
  • I'd use a sync utility for that kind of task, either syncthing or a croned task.
    – Muihlinn
    Commented May 14, 2020 at 6:28
  • 2
    Elisp tag doesn't apply here. Please read Elisp tag info
    – Muihlinn
    Commented May 14, 2020 at 6:29
  • prefer to not use sync or sync the file, as i only sync it when i need to :) (it's mostly to save space, since it's already in the vm anyway) @Muihlinn Commented May 14, 2020 at 7:32
  • Thanks, i'll try this @InHarmsWay Commented May 14, 2020 at 7:32

1 Answer 1

1

First of all, isn't a great idea opening remote files from your init files. I'd use bookmarks or whatever else to open them quickly when needed, also, I'd rather use any sync service like syncthing or a rsync cron task to keep them synced if, as you seem to suggest, the file also lies elsewhere.

If you absolutelly need to do it, I'd check if the server is up using something like this (assuming you're on some kind of un*x):

(let ((out (shell-command-to-string "nc -v -z -W 2 192.168.122.141 22")))
  (if (string-match "succeeded" out 0)
    (find-file "/ssh:[email protected]:/home/vm/test.org" t)
    (message "server down")))

While probably there is a pure elisp way to do it, I'd prefer calling nc because is straightforward, usually comes installed by default and there are plenty of examples of its use. In this case -W specifies the timeout.

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.