My setup:
Emacs terminal mode (emacs -nw)
inside it, use the shell mode (invoked with M-x ansi-term)
inside this shell, connect to a remote server with ssh
Suppose I'm browsing the remote server inside the shell and find a file I want to edit. Is there a command to open it as a parallel buffer/window?
The only method I know to open a file from the shell is to do emacs -nw again, which isn't quite convenient because a) I don't keep the shell open and b) it's really a different Emacs session, so for instance the "yank buffer" is different.
Edit: if there is a different/better way to work with a remote server with Emacs I'm just as interested; it's what I'm trying to do.
It's best to use tramp.
I have this shortcut (I call it with smex):
(defun connect-remote ()
(interactive)
(dired "/user#domain.com:/"))
This opens a dired buffer on the remote. You just use it as any dired buffer.
I've had a function to open a term from dired for a while, but I've
added an option to ssh from a tramp dired buffer just now:
(defun dired-open-term ()
"Open an `ansi-term' that corresponds to current directory."
(interactive)
(let ((current-dir (dired-current-directory)))
(term-send-string
(terminal)
(if (file-remote-p current-dir)
(let ((v (tramp-dissect-file-name current-dir t)))
(format "ssh %s#%s\n"
(aref v 1) (aref v 2)))
(format "cd '%s'\n" current-dir)))))
(defun terminal ()
"Switch to terminal. Launch if nonexistent."
(interactive)
(if (get-buffer "*terminal*")
(switch-to-buffer "*terminal*")
(term "/bin/bash")))
And this is the shortcut that I use:
(define-key dired-mode-map (kbd "`") 'dired-open-term)
You could use tramp from within your original emacs session to browse the remote server via ssh, using dired. Then, any remote file you open is opened in your local emacs session.
If you prefer to avoid dired and want to browse with a shell, you can prepend the remote location (/name#host:/path/from/pwd) to the file name. You can automate that with a function.
Related
In Emacs, I don't like shell-mode/eshell-mode since they cannot take full use of zsh and they suck much.
So I hope to use xterm as the external subprocess.
(global-set-key (kbd "M-<f2>")
(lambda () (interactive)
(start-process "XTerm" nil "xterm")))
And now the PWD of xterm is synced with Emacs default-directory and the term is now a full-feathered one. But there is ONE problem: I the startup time of the sub-rountine is always disappointing.
So I hope starting xterm only once and when in Emacs, if it finds there is a subprocess called XTerm running, 1) switch to it 2)set the PWD of shell running in xterm to default-directory of Emacs.
Is it possible to do so?
If neither is possible, then with tmux, can we achieve this goal?
Here's my setup:
(defvar terminal-process)
(defun terminal ()
"Switch to terminal. Launch if nonexistant."
(interactive)
(if (get-buffer "*terminal*")
(switch-to-buffer "*terminal*")
(term "/bin/bash"))
(setq terminal-process (get-buffer-process "*terminal*")))
(global-set-key "\C-t" 'terminal)
Could you elaborate more on the start-up time? Mine is around 0.3s.
UPD A small snippet from my dired customization
I've got this in my dired setup:
(add-hook
'dired-mode-hook
(lambda()
(define-key dired-mode-map (kbd "`")
(lambda()(interactive)
(let ((current-dir (dired-current-directory)))
(term-send-string
(terminal)
(format "cd %s\n" current-dir)))))))
where terminal is:
(defun terminal ()
"Switch to terminal. Launch if nonexistant."
(interactive)
(if (get-buffer "*terminal*")
(switch-to-buffer "*terminal*")
(term "/bin/bash"))
(setq terminal-process (get-buffer-process "*terminal*")))
What this does is it opens a terminal for the same directory as dired buffer,
reusing the existing *terminal*, or creating a new one if it's absent.
To summarize the answer to your question:
Yes, it's possible. It's done with:
(term-send-string
(terminal)
(format "cd %s\n" default-directory))
If xterm is not a hard requirement, only that you somehow launch zsh from emacs, then take a look at AnsiTerm, or my preference, MultiTerm. They implement a terminal emulator (like xterm) in emacs, so you can run whatever terminal application (e.g. zsh) in a buffer. I like MultiTerm because it has better defaults IMO.
Then you can change directories with
(defun term-send-cd (&optional dir)
(interactive "DDirectory: ")
(let ((dir (if dir (expand-file-name dir) "")))
(term-send-raw-string (format "cd '%s'\n" dir))))
As explained in here, putting (setq default-directory "~/Desktop/mag" ) in .emacs is supposed to change the default directory.
When I do that with the emacs on my mac, it doesn't work. C-x C-f still shows ~/ not ~/Desktop/mag.
(cd "Users/smcho/Desktop/mag") also gives me this error - Error: No such directory found via CDPATH environment variable
What's wrong with them?
The directory that appears in the prompt for C-x C-f ('find-file') comes from the value of default-directory, which is a buffer-local variable. When you first start Emacs, the initial buffer displayed is the GNU Emacs buffer. That buffer's default-directory is set from the variable command-line-default-directory.
So, try this:
(setq command-line-default-directory "~/Desktop/mag")
The straight-forward answer to your question is:
(setq-default default-directory "~/Desktop/mag")
Reading the documentation for the variable (C-h v default-directory RET) you'll see:
Automatically becomes buffer-local
when set in any fashion. This
variable is safe as a file local
variable if its value satisfies the
predicate `stringp'.
That said, opening a file automatically sets the default-directory to the path of the file...
So, if you always want find-file to start at that directory, you can use this:
(global-set-key (kbd "C-x C-f") 'my-find-file)
(defun my-find-file ()
"force a starting path"
(interactive)
(let ((default-directory "~/scratch/"))
(call-interactively 'find-file)))
This question may be a duplicate of Preventing automatic change of default-directory. Though it's difficult to tell.
In addition to the notes above regarding default-directory, I had to also prevent the emacs splash screen from starting in order to make subsequent commands like dired actually show their buffer when invoked from .emacs on startup:
(setq inhibit-splash-screen t)
I have configured my emacs to run zsh shell within ansi-term. However, copy/paste no longer works i.e. nothing is getting pasted from kill-ring to the terminal.
Changing the TERM to vt100, or eterm doesn't solve the problem.
Would appreciate any ideas or solution.
To provide context I have configured ansi-term as follows:
(global-set-key "\C-x\C-a" '(lambda ()(interactive)(ansi-term "/bin/zsh")))
(global-set-key "\C-x\ a" '(lambda ()(interactive)(ansi-term "/bin/zsh")))
You may want to simply switch between character mode and line mode while using the terminal. C-c C-j will run term-line-mode, which treats the terminal buffer more like a normal text-buffer in which you can move the cursor and yank text. You can switch back to character mode by running term-char-mode with C-c C-k.
As described in this lovely blog snippet, there's a function, term-paste, in term.el, that does exactly what you want. By default it's bound only to S-insert but the blog's recommended C-c C-y seems like a good suggestion.
ansi-term, in char-mode, takes the ordinary bindings for the terminal emulation. You need a new binding, plus a way to output to ansi-term correctly. I use this:
(defun ash-term-hooks ()
;; dabbrev-expand in term
(define-key term-raw-escape-map "/"
(lambda ()
(interactive)
(let ((beg (point)))
(dabbrev-expand nil)
(kill-region beg (point)))
(term-send-raw-string (substring-no-properties (current-kill 0)))))
;; yank in term (bound to C-c C-y)
(define-key term-raw-escape-map "\C-y"
(lambda ()
(interactive)
(term-send-raw-string (current-kill 0)))))
(add-hook 'term-mode-hook 'ash-term-hooks)
When you do this, C-c C-y will yank. It only does one yank, though, and you can't cycle through your kill-buffer. It's possible to do this, but I haven't implemented it yet.
The above solutions work well for copying text from some buffer to ansi-term, but they aren't able to copy text from ansi-term to another buffer (eg copy a command you just ran to a shell script you're editing). Adding this to my .emacs file solved that problem for me (in Emacs 24.4):
(defun my-term-mode-hook ()
(define-key term-raw-map (kbd "C-y") 'term-paste)
(define-key term-raw-map (kbd "C-k")
(lambda ()
(interactive)
(term-send-raw-string "\C-k")
(kill-line))))
(add-hook 'term-mode-hook 'my-term-mode-hook)
Note that if you want to bind kill/yank to a keystroke that starts with the ansi-term escape characters (by default C-c and C-x), and want this to work in the unlikely event that those change, you can instead define your keystrokes (without the leading escape) to term-raw-escape-map, as is done in user347585's answer.
These other solutions don't work well for me, switching between character mode and line mode causes ansi-term to stop working properly randomly, and setting ansi-term's term-paste to C-c C-y (based on Glyph's link), didn't work the code snippet was for term, not ansi-term:
(eval-after-load "ansi-term"
'(define-key ansi-term-raw-map (kbd "C-c C-y") 'term-paste))
I enabled xterm-mouse-mode, after that I was able to select text using mouse and copy using standard Mac command C button in ansi-term in emacs GUI in Mac OS X,
I have the following emacs lisp snippet that will launch my browser from within emacs and open the specified pages. However when I run it as a script from a shell nothing happens. What more do I need to do? I tried dropping (interactive).
#!/usr/bin/emacs --script
(defun surf-news ()
(interactive)
(progn
(browse-url "http://news.ycombinator.com")
(browse-url "http://stackoverflow.com")
))
(surf-news)
A neat function I discovered yesterday is M-x webjump which has recently been added to emacs.
As Ryan Thompson mentioned above, you can use xdg-open in Emacs.
Change:
(browse-url "http://news.ycombinator.com")
(browse-url "http://stackoverflow.com")
To:
(browse-url-xdg-open "http://news.ycombinator.com")
(browse-url-xdg-open "http://stackoverflow.com"))
I found the answer on this site, at the bottom, where it describes a Mac OS pty bug. I had to add this line and a slight delay between urls.
(setq process-connection-type nil)
Here's the complete solution.
#!/usr/bin/emacs --script
(setq process-connection-type nil);; pty's broken on the Mac
(defun surf ()
(progn
(browse-url "http://news.ycombinator.com")
(sleep-for 0.5); We need a delay
(browse-url "http://stackoverflow.com")
))
;;
;; This is what's going on behind the scenes
;;(setq url "http://www.google.com")
;;(start-process (concat "open " url) nil "open" url)
(surf)
On linux, if you are in GNOME, KDE, or probably almost any graphical environment, there is probably a command called xdg-open. This command basically takes its argument and opens it with the desktop default application. It handles both files and URLs. So generally when I need to say "Open this in the default application," I use xdg-open.
I think Mac OS has a similar command just called open.
I tend to run my shell in emacs, but when I use it to ssh into another computer this breaks tab-completion. Is there a way to fix this?
Try:
M-x cd /hostname:/current/path/in/the/shell
That should set up ange-ftp (or tramp), and then TAB completion for paths should work properly for that shell - until you log into a different machine.
You could set up a comint process filter to recognize when you type ssh to do that for you automatically, but that's difficult to get right as it should revert when you exit the ssh session, but not be tricked by other uses of exit.
For an automated solution, I'd suggest augmenting the approach I personally use to keep Emacs synchronized with the current working directory of the shell buffer. Just add an an extra bit of information with the hostname, and use that to set the hostname and path like shown above.
in another thread, someone mentioned eshell which I've never used but I tried it with SSH and all sorts of nice integration is happening. Time to learn eshell.
You could try M-x ansi-term to host your shell if your getting unexpected behavior with key mappings. Having said that I couldn't re-produce the problem your describing on your set-up.
I had a similar problem I think and solved it by editing my ~/.bash_login on the remove machine and append
export TERM=xterm
I use OS X and had problems when connecting to a Linux (Debian Lenny)
I just wrote a little function to open a shell on a remote host. The cd call before shell gets the tab completion working.
This may be different than you want, since it opens a new shell instead of ssh'ing in a local shell. Beyond that, you could look into hacking emacs Directory Tracking (or see who else has).
(defun remote-shell (&optional host)
"Open a remote shell to a host."
(interactive)
(with-temp-buffer
(let ((host (if host host (read-string "Host: "))))
(cd (concat "/scp:" host ":"))
(shell (concat "*" host "*")))))
(defun myserver-shell () (interactive) (remote-shell "myserver"))
M-x shell invoked in buffer A will switch to a shell buffer B; usually shell. Unsurprisingly it creates B if necessary. A prefix arg will cause it to ask for the name of B. If A is viewing something on a remote host then the shell will be run on the remote host. But only if it can't find an existing B. At that point file name completion will work.
There are some notes about how to tinker with this in the emacs wiki. See for example the function my-shell in this section, which will extend the default name for B so remote files get remote shells. I do that same thing by advising the shell function.
The filename auto completion will work fine. Command autocomplete? Less so. For me it blocks emacs and then doesn't actually work.
I use dired to access the remote machine and open a shell there.
Here is the function I use, taken and modified from Tikhon Jelviss' emacs configuration:
(defun anr-shell (buffer)
"Opens a new shell buffer where the given buffer is located."
(interactive "sBuffer: ")
(pop-to-buffer (concat "*" buffer "*"))
(unless (eq major-mode 'shell-mode)
(dired buffer)
(shell buffer)
(sleep-for 0 200)
(delete-region (point-min) (point-max))
(comint-simple-send (get-buffer-process (current-buffer))
(concat "export PS1=\"\033[33m" buffer "\033[0m:\033[35m\\W\033[0m>\""))))
Example:
(anr-shell "/vagrant#localhost#2222:/vagrant/")