Locate and open remote files - emacs

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.

Related

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.

Remote ssh connection from within Emacs

Several questions, including this one, discuss aspects relating to ssh connections from within Emacs. I haven't found an answer to my question though: How can I ssh into a remote machine from within Emacs?
I do not wish to edit a file on the remote machine from within Emacs. I am aware of M-x shell which opens a shell on my local machine and I am aware of using TRAMP to edit a file over ssh on the remote machine. However, neither of these relate to this question.
(Instead of voting to close, maybe migrate the question to another site.)
Edit: Related discussion here.
Firstly, I am unaware of a native elisp ssh client (and do not imagine there is a great deal of motivation for writing one), so you will certainly need to interact with an external ssh client process.
As you wish to use ssh interactively, the ssh process requires a terminal on the local side of the connection.
The question therefore becomes: Does Emacs implement a terminal to which an ssh process can be attached?
The answer is: yes -- term.el provides a robust terminal implementation, through which ssh can be run directly, without the requirement for a shell.
If you run M-x term RET you will be prompted for a program. (It defaults to a shell, but that is certainly not the only type of process you can run.)
For reasons unknown, the interactive term (and ansi-term) functions do not support passing arguments to the specified program, which renders them less useful if you wished to run something like ssh user#host. You could instead specify a script which handled the arguments, but we can manage that in elisp as well:
The term function is actually a simple wrapper which calls make-term to start the program and then sets the appropriate modes. As make-term does accept program arguments, it is quite straightforward to copy-and-modify the definition of term to suit your own purposes.
Here is a very simple implementation:
(defun my-ssh (user host port)
"Connect to a remote host by SSH."
(interactive "sUser: \nsHost: \nsPort (default 22): ")
(let* ((port (if (equal port "") "22" port))
(switches (list host "-l" user "-p" port)))
(set-buffer (apply 'make-term "ssh" "ssh" nil switches))
(term-mode)
(term-char-mode)
(switch-to-buffer "*ssh*")))
or perhaps this is preferable:
(defun my-ssh (args)
"Connect to a remote host by SSH."
(interactive "sssh ")
(let ((switches (split-string-and-unquote args)))
(set-buffer (apply 'make-term "ssh" "ssh" nil switches))
(term-mode)
(term-char-mode)
(switch-to-buffer "*ssh*")))
Obviously there is scope for improvements, but I think that's fairly useful as-is.
You should ensure that you are familiar with the quirks of term-mode. See:
M-: (info "(emacs) Terminal emulator") RET
M-: (info "(emacs) Terminal Mode") RET
C-hf term-mode RET
It turns out, there is what you want:
(setq rlogin-program "ssh")
(setq rlogin-process-connection-type t)
and then M-x rlogin RET <myhost> RET will do that.
Maybe ssh.el is what you are looking for. The ssh command provides a single-step way to create remote shells in Emacs via ssh, including specifying the user name in a natural way, and enabling tramp directory tracking if desired.
I'm not sure I understand. Open up M-x ansi-term and run ssh user#host there?

Emacs: How to start Local python interpreter when editing a remote python file via tramp

On Emacs, when I start python with C-c ! while editing a remote python file (using tramp - plinkx: on windows), the python interpreter is started on the remote host.
Is there any way I can edit the remote python file and start a local python interpreter?
I am using python-mode (not the default python.el)
python-mode creates an inferior process via 'make-comint, which uses 'start-file-process, which creates the process relative to the variable 'default-directory. So there are a few ways you can tackle this beast.
The first is to change 'default-directory to be something local, like:
(add-hook 'python-mode-hook (lambda () (setq default-directory "~"))
That has the downside that C-x C-f now behaves differently (starting at ~).
Another is to change the 'default-directory just for the invocation of 'py-shell, like so (untested):
(defadvice py-shell (around py-shell-different-directory activate)
"set default-directory just for py-shell"
(let ((default-directory "~"))
ad-do-it))

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))
)
)