How to check if connected to remote server?
Asked Answered
E

1

9

I use tramp to connect to my company's servers. I would like to customize my functions to work differently if I am accessing local files vs remote ones.

Is there a variable I could check if the buffer I am accessing is on a remote server or my local machine?

Something like:

if(TRAMP_CONNECTED)
 Do this
else
 Do that
Erl answered 22/2, 2015 at 1:18 Comment(4)
I asked a similar question that phils previously answered -- his answer, plus my comment underneath, may address your issue: https://mcmap.net/q/1314199/-emacs-dired-mode-test-to-determine-remote-server-name-address In my case, I also wanted to distinguish which remote I was connected to -- since I have a few.Dexter
lawlist, for some reason this code: (and (file-remote-p dired-directory 'localhost) (equal (tramp-file-name-real-host (tramp-dissect-file-name dired-directory)) "remote-name")) return error if executed while inside a file. Is there a way to change it so it checks whether buffer is file or directory?Erl
Perhaps using file-directory-p -- the doc-string is as follows: (file-directory-p FILENAME) -- Return t if FILENAME names an existing directory. Symbolic links to directories count as directories. See file-symlink-p to distinguish symlinks. Depending on the usage, you might also be interested in buffer-file-name as a variable or (buffer-file-name) as a function -- both are usually set when there is a file; however, some modes set a non-file-visiting buffer equal to a specific buffer-file-name.Dexter
I need to correct a slight misstatement in my previous comment -- the value for buffer-file-name is sometimes set by some modes for non-file-visiting buffers. The doc-string for the function buffer-file-name states: Return name of file BUFFER is visiting, or nil if none -- No argument or nil as argument means use the current buffer. I have not yet seen an example where the value would be different between the variable or the function that returns the value.Dexter
S
14

The most robust check is likely default-directory. This variable is always set buffer local. If (file-remote-p default-directory) returns non-nil, your buffer is under Tramp control.

Specht answered 22/2, 2015 at 9:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.