Can I send the "cd" command to xterm from emacs?

In Emacs, I don't like shell-mode / eshell-mode , as they cannot fully use zsh , and they suck a lot.

Therefore, I hope to use xterm as an external subprocess.

 (global-set-key (kbd "M-<f2>") (lambda () (interactive) (start-process "XTerm" nil "xterm"))) 

And now PWD xterm is synchronized with Emacs default-directory , and this term is now completely feathered. But there is ONE problem: I run sub-rountine always disappoints.

So, I hope that xterm will be launched only once, and when in Emacs, if it finds a subprocess called xterm running, 1) it will switch to it. 2) install the PWD of the shell running in xterm in the default-directory Emacs.

Can this be done?

If this is not possible, then with tmux can we achieve this?

+4
source share
2 answers

Here is 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 "\Ct" 'terminal) 

Could you tell us more about the launch time? My is about 0.3 s.

UPD A small fragment from my dired setup

I have 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 :

 (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 means is, it opens a terminal for the same directory as the rewritten buffer, reusing the existing *terminal* or creating a new one if it is missing.

To summarize the answer to your question:

Yes it is possible. This is done using:

 (term-send-string (terminal) (format "cd %s\n" default-directory)) 
+2
source

If xterm is not a strict requirement, only you somehow start zsh from emacs and then look at AnsiTerm , or my preference, MultiTerm . They implement a terminal emulator (e.g. xterm) in emacs, so you can run any terminal application (e.g. zsh) in a buffer. I like MultiTerm because it has the best IMO defaults.

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

Source: https://habr.com/ru/post/1492981/


All Articles