Is it possible to start emacs using a remote configuration file? - emacs

I have different work-specific .emacs configuration files on servers where I do work for various companies. These are packages that, for various reasons, I don't want to store locally on my machine. Is there any way to start up emacs using one of those remote directories as if it were local?
Tramp is great for actually editing remote files as if they were local -- now I'd like to be able to start up a whole emacs editor in the same way.

Out of curiosity I just gave this a go:
emacs -q --load "/ssh:USER#HOSTNAME:.emacs.d/init.el"
That worked, however the remote init.el is setting load paths like "~/.emacs.d/vendor/", so really an init file needs to be aware that the paths and filenames it sets need to be relative to a common base. This base could be set by a common variable, so try:
emacs -q --eval '(setq init-base "/ssh:USER#HOSTNAME:") (load (concat init-base ".emacs.d/init.el"))'
Then in the remote config, prepend init-base to any path or filename it sets that should be remote.

start up emacs using one of those remote directories as if it were local
If the servers are unix/linux servers and you have ssh access, then you could try SSH Filesystem. Then you can mount the servers to local directories, e.g.:
> sshfs server1: ~/remote/server1
> sshfs server2: ~/remote/server2
Then you could start emacs with emacs --no-init-file --load ~/remote/servers2/.emacs and so on.
packages that, for various reasons, I don't want to store locally
If packages are installed in .emacs.d on the remote machines you could create scripts like the following on your local machine:
;; .emacs.server1.el
(add-to-list 'load-path (expand-file-name "~/remote/server1/.emacs.d"))
(add-to-list 'load-path (expand-file-name "~/remote/server1/.emacs.d/package1"))
(load (expand-file-name "~/remote/server1/.emacs"))
And then start emacs like this: emacs --no-init-file --load ~/.emacs.server1.el
Obvious this script depends on the mounts above.

You may find the SO question Originate edit of remote file using emacs tramp from ssh session is asking for a solution to a related problem. Perhaps the answers there might give you what you want, namely, you can attach to an emacs running on the remote machine.

Related

Locate and open remote files

In emacs, the locate command provides a way to run unix's locate and open files on the local filesystem.
I'm using TRAMP to access remote files - is it possible to use emacs' locate to find and open remote files?
Likely, it is not possible. locate uses call-process, which does not run remote processes.
I don't know if you can use Emacs's locate function with TRAMP per se, but you could connect to a host remotely and run locate on that system via M-x shell or M-x eshell. I have some wrapper functions which make this task easier (although I mostly got them from elsewhere):
(defun remote-eshell (host)
(interactive "sHost: ")
(let ((default-directory (concat "/ssh:" (format "%s:" host))))
(eshell host)))
(defun remote-shell (host)
(interactive "sHost: ")
(let ((default-directory (concat "/ssh:" (format "%s:" host))))
(shell)))
After locating the desired files on the remote host, you can then open them via TRAMP using C-x C-f /ssh:remotehost
Also, if you're connecting via TRAMP to a remote host, you should make sure that said host is not sending any weird prompts since TRAMP doesn't handle those well. See more about that here.

eshell TRAMP find remote file with relative path (or at least less than the full Tramp path)?

I love eshell's TRAMP integration. With it I can do cd /ssh:foo:/etc to
ssh into a remote machine and visit its /etc/ directory. I can also do
find-file motd to open this file in my local emacs. However, what if I need to use sudo to change the file? I know I can give the
full path, like so:
find-file /sudo:foo:/etc/motd
but is there a way to open the file via TRAMPs sudo support, without having to type the full path?
I managed to came up with the following eshell alias that works for me:
alias sff 'find-file "${pwd}/$1"(:s/ssh/sudo/)'
It should be fairly obvious what it does. It prepends the working directory
path, but with the string ssh replaced by sudo. Thus it only works for
remote files accessed over ssh. I rarely edit files using sudo locally, so
that's not a problem for me. However, we can make it work for local files too, at the cost of complexity:
alias sff 'find-file "${pwd}/$1"(:s,^,/sudo::,:s,::/ssh:,:,)'
That is, prepend /sudo:: (which is how to sudo for local files) and
subsequently replace any ocurrence of ::/ssh: with :. (I would have just removed :/ssh:, but eshell's :s/// construct didn't accept an empty
replacement.)
I found an alternative answer that works very well over at EmacsWiki.
Using that you'd still open the file with find-file as usual, but then
invoke M-x sudo-edit-current-file (shown below) to re-open the file as root
using Tramp. I think this is a very elegant solution, because often I
initially just want to look at a file, then later find that I need to edit it.
Here's the function, in case it disappears from the page above:
(set-default 'tramp-default-proxies-alist (quote ((".*" "\\`root\\'" "/ssh:%h:"))))
(require 'tramp)
(defun sudo-edit-current-file ()
(interactive)
(let ((position (point)))
(find-alternate-file
(if (file-remote-p (buffer-file-name))
(let ((vec (tramp-dissect-file-name (buffer-file-name))))
(tramp-make-tramp-file-name
"sudo"
(tramp-file-name-user vec)
(tramp-file-name-host vec)
(tramp-file-name-localname vec)))
(concat "/sudo:root#localhost:" (buffer-file-name))))
(goto-char position)))

how to use emacs tramp with ssh remote to server

I want use emacs remote server(freebsd use sftp with ssh).
I already read this and write (require 'tramp) (setq tramp-default-method "ssh") in my .emacs. I use C-c C-f RET /sftp:ganluo-home-vm-freebsd-user#192.168.1.104/.
But it does not work
Look Here:I use emacs for windows so can't direct use tramp.Have to download plink.exe(I use it maybe had other way) and do some other exp:
first download here (also you can found some windows conntion unix\linux software in this website).Find plink.exe download it (offer release and development and source code)
than you can cut plink.exe in the your emacs\bin directory.
find your Environment Variables Edit/New "PATH" Add your emacs\bin complete directory and last the path must with ';'
C:\Emacs\bin;
then sign out use cmd test plink command is useful.if doesn't restart you PC.
C-c C-f /pink:YouName#HOST[IP,HOSTName]:/
it had some problem,I will post new status right here.

Why does emacs does not find psql?

This is somewhat weird, I compiled from source postgresql. I've had no problem using psql at all.
$ psql -d rita
psql (9.3.2)
Type "help" for help.
rita=>
`
However when I try to run M-x sql-postgres on emacs, it prompts for user, password, database, host. But when I input the data in the minibuffer just appears: Unable to locate SQL program psql. I hope anyone can tell me how can I fix this, or what is happening.
Your psql isn't on the standard path.
You are likely setting the PATH in
~/.bashrc, and Emacs will not see it unless you launch Emacs from the terminal.
A solution is:
(setq sql-postgres-program "/usr/local/pgsql/bin/psql")
Or ln -s to /usr/bin if you want.
Alternatively, you can add the path to the psql binary to your emacs 'exec-path list as follows:
(add-to-list 'exec-path "/path/to/program")
(setenv PATH (mapconcat 'identity exec-path ":"))
I run Emacs on a Mac as my development environment. Launching emacs from the Dock doesn't include the path for programs installed via MacPorts. This is going to be a standard problem for anyone who has "non-standard" exec path elements and launches Emacs via their GUI. For me, OSX : MacPorts : /opt/local/... this includes psql, mysql, py-flakes and others. I could either set a variable for each add-on or modify exec-path. The setenv is needed where the elisp function defers searching the exec-path to the shell. The C-c C-v binding in python mode leverages the shell and the $PATH environment to find py-flakes.
The add-to-list is a simple solution that prepends the path to the list. One could make a case for more complex path wrangling in your .emacs file. YMMV.

Emacs + tramp + plink

I'm trying to get emacs tramp running under Windows XP to work over putty plink on an Amazon EC2 instance. The documentation for doing this is sparse. I can find partial documentation, but none that addresses all the steps required to get this working.
Can anyone provide a walk through, or a pointer to a walk through?
(add-to-list 'load-path
(expand-file-name "C:/tools/emacsw32/emacs/lisp/tramp/lisp"))
(require 'tramp)
;(setq tramp-chunksize "500")
(setq tramp-default-method "plink")
from my dot-emacs file. If I find more notes, I shall add them here.
I'll assume you have a GNU/Linux server you want to access, a username and a .ppk file. Also, Emacs 24.4+.
First set up server in PuTTY Configuration
In section Session, specify Host Name, for example username#server.
Go to section Connection > SSH > Auth and Browse for your "Private key file for authentication".
Back to section Session, name your Saved Sessions, for example putty-test, and click Save button.
Check your connection by clicking the Open button. If it works, you can close these now.
Next, head to your Emacs.
Make sure Emacs knows where your plink.exe is. One way is to just inform Emacs directly in your .emacs, for instance I have at the moment,
(setenv "PATH" (concat "c:/Users/Brady/Documents/putty/;" (getenv "PATH")))
Simply type C-x C-f //plink:putty-test:/ RET. Wait a moment while it connects, and window will open to dired buffer on the remote ~/ directory.
This worked for me on :
Windows 10
Emacs found at https://sourceforge.net/projects/emacsbinw64/files/release/.
cygwin64
Putty.
https://github.com/d5884/fakecygpty
The changes from the original tramp-sh.el is
for cygwin, use fakecygpty with ssh and change the prompt to ##
for plink, remove -ssh option
I have also renamed these method with w to differentiate it.
(when (string-equal system-type "windows-nt")
(add-to-list 'tramp-methods
`("sshw"
(tramp-login-program "fakecygpty ssh")
;; ("%h") must be a single element, see `tramp-compute-multi-hops'.
(tramp-login-args (("-l" "%u" "-o \"StrictHostKeyChecking=no\"") ("-P" "%p") ("-t")
("%h") ("\"")
(,(format
"env 'TERM=%s' 'PROMPT_COMMAND=' 'PS1=%s'"
tramp-terminal-type
"##"))
("/bin/sh") ("\"")))
(tramp-remote-shell "/bin/sh")
(tramp-remote-shell-login ("-l"))
(tramp-remote-shell-args ("-c"))
(tramp-default-port 22))
)
(add-to-list 'tramp-methods
`("plinkw"
(tramp-login-program "plink")
;; ("%h") must be a single element, see `tramp-compute-multi-hops'.
(tramp-login-args (("-l" "%u") ("-P" "%p") ("-t")
("%h") ("\"")
(,(format
"env 'TERM=%s' 'PROMPT_COMMAND=' 'PS1=%s'"
tramp-terminal-type
"$"))
("/bin/sh") ("\"")))
(tramp-remote-shell "/bin/sh")
(tramp-remote-shell-login ("-l"))
(tramp-remote-shell-args ("-c"))
(tramp-default-port 22))
)
)