I'm trying using Emacs and tmux in the Terminal.app, and having difficulty getting the mouse to work correctly.
If I use Emacs outside of tmux, then I can use the mouse to click on text to jump around the buffer.
However, if I am inside tmux, and I start editing a file in Emacs, then I can't click around. I have to do
M-x load-file ~/.emacs.d/init.el
.. to get it working.
Here is the content of my init.el
(require 'package) ;; You might already have this line
(when (< emacs-major-version 24)
;; For important compatibility libraries like cl-lib
(add-to-list 'package-archives '("gnu" . "http://stable.gnu.org/packages/")
'("melpa" . "http://melpa.org/packages/")))
(package-initialize) ;; You might already have this line
;; magit
(global-set-key (kbd "C-x g") 'magit-status)
(global-set-key (kbd "C-x M-g") 'magit-dispatch-popup)
(global-set-key (kbd "C-c SPC") 'ace-jump-mode)
;; mouse
;; enable mouse reporting for terminal emulators
(unless window-system
(xterm-mouse-mode 1)
(global-set-key [mouse-4] (lambda ()
(interactive)
(scroll-down 1)))
(global-set-key [mouse-5] (lambda ()
(interactive)
(scroll-up 1))))
..and my tmux.conf
set -g mouse on
bind X confirm-before kill-session
bind C-s set-window-option synchronize-panes
bind r source-file ~/.tmux.conf \; display "Reloaded!"
set -g window-active-style 'fg=colour250,bg=black'
bind -t emacs-copy C-WheelUpPane halfpage-up
bind -t emacs-copy C-WheelDownPane halfpage-down
# plugins
set -g #plugin 'nhdaly/tmux-scroll-copy-mode'
run '~/.tmux/plugins/tpm/tpm'
Versions
tmux version 2.1
emacsclient 24.5
macOS 10.11.4
This worked for me:
(xterm-mouse-mode 1)
https://unix.stackexchange.com/questions/252995/how-can-mouse-support-be-enabled-in-terminal-emacs/406519#406519
Related
I am using logitech pc keyboard with my eMac and want to use Home and End buttons as usual. I've already rebound them for mac terminal. And want to rebind them in emacs as well.
Like this:
(global-set-key [home] 'move-beginning-of-line)
(global-set-key [end] 'move-end-of-line)
As for Terminal.app, its default key bindings grab Home and End for itself (to scroll to the beginning and end of the terminal output), and lets Shift-Home and Shift-End through to the application. You can switch this around in under "Settings" -> "Keyboard" in the Terminal preferences.
In your .emacs file put something like this:
(add-hook 'after-init-hook
'(lambda ()
(define-key global-map [C-home] 'beginning-of-buffer)
(define-key global-map [C-end] 'end-of-buffer)
(define-key global-map [home] 'beginning-of-line)
(define-key global-map [end] 'end-of-line)
;;; much more semi private stuff deleted
))
It works for me since ancient times.
If I runs emacs in a terminal (i.e start emacs in iTerm2 with emacs -nw)
using windmove and it's default bindings, I should be able to navigate between windows using various combinations of Shift + → ← ↑ ↓, Also paredit has bindings that involve Ctrl/Meta + → ← ↑ ↓, these all work fine in (say) an elisp mode buffer.
Looks like the arrow keys functionality relies on emacs decoding terminal escape sequences, via (I think) the input-decode-map
However, if I set the major mode as clojure-mode then the decoding of the escape sequences appears to be disabled (or overwritten). When I execute those bindings I just get the escape sequence instead.
What's going on with the bindings in clojure-mode ?
Versions:
emacs 24.3.1 (have tried maxosxforemacs.com version and homebrew)
clojure-mode 20131117.2306 (have tried other versions)
OSX 10.8.5
iTerm2 1.0.0.20131124
(disclaimer: I raised this as an issue on clojure-mode, but no solution is forthcoming)
UPDATE 2013/12/10: To be absolutely clear: this problem is specific to clojure-mode. emacs -nw in iTerm + arrow keys works fine in other major modes. I'm sure I'm doing something silly that is causing this, I'd like to know what.
Almost identical setup here, but no similar problem. Here's what I would do anyway. First, evaluate the following emacs-lisp code in the scratch buffer.
(progn
(define-key input-decode-map "\e[1;2D" [S-left])
(define-key input-decode-map (kbd "M-[ 1 ; 2 C") [S-right])
(define-key input-decode-map (kbd "M-[ 1 ; 2 B")[S-down])
(define-key input-decode-map "\e[1;2A" [S-up])
(define-key input-decode-map "\e[1;6A" [S-C-up])
(define-key input-decode-map "\e[1;6B" [S-C-down]))
Try the windmove bindings when clojure-mode is activated. If this fixes your problem, and assuming your TERM environment variable is set to xterm-256color, put the following in your init.el
(if (equal "xterm-256color" (tty-type))
(progn
(define-key input-decode-map "\e[1;2D" [S-left])
(define-key input-decode-map (kbd "M-[ 1 ; 2 C") [S-right])
(define-key input-decode-map (kbd "M-[ 1 ; 2 B")[S-down])
(define-key input-decode-map "\e[1;2A" [S-up])
(define-key input-decode-map "\e[1;6A" [S-C-up])
(define-key input-decode-map "\e[1;6B" [S-C-down])))
Hope this helps.
Check your settings in Iterm, more specifically the "Global shortcut keys". Those will take precedence over what is sent to emacs. C-Shift-Arrow is used to switch tabs in Iterm I believe and may be interfering with emacs.
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))))
I use WindMove in Emacs 24.1 to move around multiple windows using keyboard shortcuts:
(global-set-key (kbd "M-J") 'windmove-left) ; move to window on the left
(global-set-key (kbd "M-L") 'windmove-right) ; move " " the right
(global-set-key (kbd "M-I") 'windmove-up) ; move " " above
(global-set-key (kbd "M-K") 'windmove-down); ; move " " below
Is there a way to associate these WindMove keyboard shortcuts with char run mode on an ansi-term? I would like to avoid having to switch into line run mode for moving between windows. After a while it becomes tedious to type C-c C-k and C-c C-j back and forth to go into/leave the terminal.
The code below will work for you. It uses the Emacs advising mechanism, which allows a user to dynamically change a function's behavior without modifying its source code directly. In this case, it is used to make the winmove-* functions run term-char-mode before executing their original definition. If you are also using other window-selecting functions, such as other-window, besides winmove-*, you can advise those functions in the same way. Refer to Advising Functions for details on the advising mechanism.
The code also handles the issue related to the term-raw-map keymap. term-raw-map is not defined until term.el is fully loaded (or M-xansi-term is executed). So you should add the (define-key term-raw-map ...) forms to the term-load-hook hook, which is automatically run when term.el is loaded, instead of putting them at the top level of the init file.
(global-set-key (kbd "M-J") 'windmove-left)
(global-set-key (kbd "M-L") 'windmove-right)
(global-set-key (kbd "M-I") 'windmove-up)
(global-set-key (kbd "M-K") 'windmove-down)
(defun ansi-term-char-mode ()
(if (string= (buffer-name) "*ansi-term*")
(term-char-mode)))
(defadvice windmove-left (before windmove-left-ansi-term (&optional arg))
(ansi-term-char-mode))
(defadvice windmove-right (before windmove-right-ansi-term (&optional arg))
(ansi-term-char-mode))
(defadvice windmove-up (before windmove-up-ansi-term (&optional arg))
(ansi-term-char-mode))
(defadvice windmove-down (before windmove-down-ansi-term (&optional arg))
(ansi-term-char-mode))
(add-hook 'term-load-hook
(lambda ()
(define-key term-raw-map (kbd "M-J") 'windmove-left)
(ad-activate 'windmove-left)
(define-key term-raw-map (kbd "M-L") 'windmove-right)
(ad-activate 'windmove-right)
(define-key term-raw-map (kbd "M-I") 'windmove-up)
(ad-activate 'windmove-up)
(define-key term-raw-map (kbd "M-K") 'windmove-down)
(ad-activate 'windmove-down)))
By default, term-char-mode forwards most keys to the terminal, without interpreting them within Emacs, which is why windmove keybindings are not working.
Here is a way to override the term-char-mode keymap to force windmove bindings to be interpreted instead of forwarded:
(eval-after-load "term"
'(progn
(define-key term-raw-map (kbd "M-J") 'windmove-left)
(define-key term-raw-map (kbd "M-L") 'windmove-right)
;; ...
))
I want to use interactive debugging of R code in emacs, and found about ess-tracebug, but can't get it to work. I did the following:
Installed ess-tracebug
Started execution of an R script with ESS C-c C-n
Set up a breakpoint, now if I try to do M-c c, says "Debugging is not active"
Here is what I added to my ~/.emacs file:
(load "/home/user/ess/ess-5.13/lisp/ess-site")
(ess-toggle-underscore nil)
(add-to-list 'load-path "/home/user/emacs/")
(require 'ess-tracebug)
(add-hook 'ess-post-run-hook 'ess-tracebug t)
(define-key ess-mode-map "\M-]" 'next-error)
(define-key ess-mode-map "\M-[" 'previous-error)
(define-key inferior-ess-mode-map "\M-]" 'next-error-no-select)
(define-key inferior-ess-mode-map "\M-[" 'previous-error-no-select)
Any suggestions?
I assume you are running R in an Emacs buffer and you have your script in another buffer. In order to debug with ess-tracebug you must type M-x ess-tracebug to start the actual tracebug session.
This should do the job.