shift up-arrow doesn't highlight text emacs iterm2 - emacs

I recently had help fixing M-left and so forth here: emacs in terminal meta arrow keybindings, but am unable to fix Shift-up using a similar solution. When I try shift-up I get an error <select> is undefined. I've tried re-mapping it using:
(add-hook 'term-setup-hook
'(lambda ()
(define-key function-key-map "\e[1;9A" [M-up])
(define-key function-key-map "\e[1;9B" [M-down])
(define-key function-key-map "\e[1;9C" [M-right])
(define-key function-key-map "\e[1;2A" [S-up])
(define-key function-key-map "\e[1;9D" [M-left])))
But shift remains undefined. I also tried rebinding the key by setting it using the escape sequence returned from cat which is ^[[1;2A. Oddly enough shift down does work. shift-select-mode is marked at t as well.

This sounds like a trouble I had accessing a Ubuntu 12.04 machine via Putty, when END caused Emacs 23.3.1 to say <select> is undefined. That turned out to be an issue with the terminfo that lets programs use terminals in a device independent way.
Based on this 2008 bug report discussion, I solved my problem by adding the following to the top of my ~/.bashrc:
#so the END key will work correctly in Emacs over PuTTY
TERM=xterm-vt220
N.B., with either xterm-vt220 or the default xterm, emacs -Q -nw is getting ESC [ 4 ~ when I press END, ESC O A for Up, and ESC [ A for Shift-Up. (To see what keycodes Emacs is getting, press some buttons and then C-h,l.) For the same keys in the same order, cat says [4~, [A, and [OA...so Up and Shift-Up are oddly reversed.
If you don't want to change your terminfo, see this discussion for a workaround
http://lists.gnu.org/archive/html/help-gnu-emacs/2011-05/msg00211.html
You should be able to work around the issue with something like:
(define-key input-decode-map "\e[1;2A" [S-up])
And for this to take effect at the right time, you will have to use in your .emacs something like:
(if (equal "xterm" (tty-type))
(define-key input-decode-map "\e[1;2A" [S-up]))

Just to add more information about the solution:
https://github.com/arthurnn/dotfiles/blob/8d56f2419da9a4cb654d8941f379d6d5783bdb90/.emacs.d/init.d/setup-bindings.el#L3-L10 this should fix all the cases including emacsclient.
The last line is responsible for fixing the Shift-up when using emacsclient.

Related

Emacs evil-mode how to change insert-state to emacs-state automatically

I don't like the insert-state, and so I want to replace it with emacs-state. But this setting does not work:
(add-hook 'evil-insert-state-entry-hook 'evil-emacs-state)
After press o or cw, I am still in insert-state.
How about this approach:
(setq evil-insert-state-map (make-sparse-keymap))
(define-key evil-insert-state-map (kbd "<escape>") 'evil-normal-state)
I use it and it seems to do the trick. And since you're not changing the state, you retain state-related configs like cursor-color, etc.
Surprised nobody posted this yet...
(defalias 'evil-insert-state 'evil-emacs-state)
Anything that tries to call evil-insert-state will just end up calling evil-emacs-state. Works for i, a, o, O, etc.
There is now a bulitin way for Evil to do this
(setq evil-disable-insert-state-bindings t)
before loading evil
Reference: https://github.com/noctuid/evil-guide#use-some-emacs-keybindings
Tell me how this works. It's a hack that basically replaces the function evil-insert-state with evil-emacs-state. The problem is figuring out how to exit emacs state with the escape key. For instance, this version works fine when I exit emacs state with the ESC key, but not when I try to do the same with C-[:
; redefine emacs state to intercept the escape key like insert-state does:
(evil-define-state emacs
"Emacs state that can be exited with the escape key."
:tag " <EE> "
:message "-- EMACS WITH ESCAPE --"
:input-method t
;; :intercept-esc nil)
)
(defadvice evil-insert-state (around emacs-state-instead-of-insert-state activate)
(evil-emacs-state))
If the point is that you want to use normal Emacs editing when doing the kind of tasks vi uses insert mode for, then wiping the insert mode dictionary accomplishes this. It is probably desirable that the ESC key gets you back into normal mode and have C-z get you into Emacs state; Leo Alekseyev posts a tiny bit of code that does this:
(setcdr evil-insert-state-map nil)
(define-key evil-insert-state-map
(read-kbd-macro evil-toggle-key) 'evil-emacs-state)
which I use and like. There are two potential disadvantages to being in insert mode rather than emacs mode:
You can't use the ESC key as another, prefixed way of ALT-keymapping; and
There is a risk (so I am told, though I haven't encountered this) if you are accessing Emacs through a tty, that Emacs will interpret ALT-modified keys as ESC followed by the character, which gives a difference in insert mode than in emacs mode.
I don't think either problem is serious.
How I became a unix chad:
;; unix chad setting
(defalias 'evil-insert-state 'evil-emacs-state)
(define-key evil-emacs-state-map (kbd "<escape>") 'evil-normal-state)
(setq evil-emacs-state-cursor '(bar . 1))
From the documentation about evil-emacs-state-entry-hook:
Hooks to run when entering Emacs state.
So the evil-emacs-state function is run when you enter emacs-state (with C-z).
You can, however, do this:
(define-key evil-normal-state-map (kbd "i") 'evil-emacs-state)
The problem now is exiting emacs state. I remember there were some problems binding ESC in emacs state, as ESC is used as META, and (IIRC) Evil uses some "special" code to intercept the ESC key.
EDIT: following your comment: this one should work:
(fset 'evil-insert-state 'evil-emacs-state)

Emacs, org-mode, evil-mode - TAB key not working

I've been working with VIM for decades, and I've become quite proficient in it. I was however sort-of... seduced by Emacs's org-mode, and in order to try it, I installed Emacs and Evil.
Evil satisfies most of my VIM-related muscle memory, so I proceeded with my testing of org-mode - and met my first issue: when I spawn Emacs in its own window (i.e. emacs plan.org) then the TAB key works, opening and closing my plan's sections just fine. However, TAB does nothing when I use Emacs in text mode (i.e inside my XTerms, via "emacs -nw plan.org"). And that's the state that I am mostly interested in, since I usually work from inside screen/tmux over SSH connections.
If it's a conflict with Evil-mode, I don't understand why - I am unaware of any TAB functionality in VIM's normal mode (which is what we're in when opening/closing org-mode sections).
Any Emacs-guru out there with a suggestion on why this happens?
Try
(setq evil-want-C-i-jump nil)
in your ~/.emacs before
(require 'evil)
Evil has, in evil-maps.el
(when evil-want-C-i-jump
(define-key evil-motion-state-map (kbd "C-i") 'evil-jump-forward))
That should give you org-mode Tab functionality back
I have almost no experience with terminals. However, I know that TAB is equivalent to C-i. Maybe that one would go through the terminal? If that works, you could add some key bindings for every TAB operation?
Try maybe C-h k TAB as well to see if TAB if sent on the wire.
(define-key evil-normal-state-map (kbd "M-i") 'evil-jump-forward)
(define-key evil-normal-state-map (kbd "M-o") 'evil-jump-backward)
I bind the function to other keys, so it's also work.

Emacs/elisp: global-set-key bindings not taking effect for Meta-<down> or -<up>?

I am trying to bind M-<up> and M-<down> to scroll-down-line and scroll-up-line respectively as indicated here: https://stackoverflow.com/a/16229080/562139.
This is what I have in my .emacs:
;; Key bindings
(global-set-key (kbd "M-g") 'goto-line)
;; Scroll line by line
(global-set-key (kbd "M-<down>") 'scroll-up-line)
(global-set-key (kbd "M-<up>") 'scroll-down-line)
Problem:
The scroll key bindings are not taking effect, while the one for goto-line does.
When I run M-x scroll-down-line however, emacs prompts me and says
"you can run the command with <M-down>"
Note:
When I run global-set-key (kbd "M-<down>") 'scroll-up-line) or (global-set-key (kbd "M-<up>") 'scroll-down-line) directly in the mini-buffer, the bindings take effect! However, I seem to have noticed through the corner of my eye when I do the latter, that pressing M-<up> actually sends something like ESC ESC-<up>.
I'm foxed. What gives?
Note: I am running emacs 24.3 in a terminal (via iTerm on OSX with Option key mapped to ESC+) over SSH to a RHEL5 virtual machine.)
Update
I followed the suggestion in this answer and found that pressing M-<up> results in something completely different:
ESC <up> (translated from ESC M-[ A) runs the command
scroll-down-line, which is an interactive compiled Lisp function.
It is bound to <M-up>, ESC <up>.
(scroll-down-line &optional ARG)
I'm going to try binding that key sequence to the function and check the result.
Try starting Emacs without your init file: emacs -Q, and see if you can reproduce the problem.
I do not see the problem, with Emacs 24.3 in terminal mode.
What you saw briefly was probably ESC <up>, which is equivalent to M-<up>.
Did you perhaps mean to type "When I run M-x scroll-up-line (instead of down)?
I suspect that you are in some mode that gives a local binding or a minor-mode binding to these keys, which overrides the global binding. To test that, try in a buffer that is in fundamental mode. If that is the case, then to override that overriding you will need to also bind the keys in that mode's keymap.
If you cannot repro the problem starting from emacs -Q then bisect your init file (~/.emacs) recursively until you find the culprit code.
Seems key got lost in translation.
Planted a forward-paragraph at openSuse that way:
(global-set-key [(meta down)] 'forward-paragraph)

Emacs: how to fix annoying escape behavior when in split windows?

I have a really annoying situation; when I'm editing in emacs and the auto-complete box loads up, I find I'm using the escape key to quit out of it when I don't need it. The problem is this has the unwanted behaviour of making the current window the only window. This is really annoying when I've set up a number of windows/frames for various tasks.
I'm using auto-complete.el, with the following options:
(ac-config-default)
(define-key ac-completing-map "\e" 'ac-stop) ; use esc key to exit completion
(global-set-key "\C-f" 'ac-isearch)
Since hitting the ESC key is in my muscle-memory for dismissing UI elements (drop-downs, dialogs, etc), any idea on how I can hit escape without having the current focussed frame take over?
In Mac emacs "ESC ESC ESC" is bound to keyboard-escape-quit by default, which is what you're accidentally invoking. This fixed it for me:
(global-unset-key (kbd "ESC ESC ESC"))
Press C-g instead of escape.ff

emacs -- keybind questions

I have successfully used Ctrl+Shift+Up ' Ctrl+Shift+down '
Ctrl+Shift+left' Ctrl+Shift+Right to different commands. But when I
tried to use Ctrl+s to the command save-buffer and Ctrl+Shift+s, which
is equivalent to Ctrl+S, to another command, it has some problem.
save-buffer works fine, but when I type Ctrl+Shift+s, it excute
the command save-buffer. I used Ctrl+q to find the control sequences of
Ctrl+s and Ctrl+Shift+S, I get the same result, which is ^S.
I expect that I will get ^s for Ctrl+s, but it doesn't.
Anyone knows the reason?
Another queston is: I use Ctrl+c for the command killing-ring-save. In this
case, all commands (which are of large number) begin with Ctrl+c don't work now.
Is there a way to replace the prefix Ctrl+c by another customized prefix?
I may pose my question in the wrong direction. I use ctrl+c as
killing-ring-save. It works fine in emacs (no mode). But if I open a .c file (C-mode), then
when I type Ctrl+c, it waits me to type another key. I think in this case,
ctrl+c is regarded as a prefix. In this case, I need the following modifications:
Using a custom defined prefix, say Ctrl+a, as Ctrl+c ; Remove the
prefix Ctrl+c ; Using Ctrl+c as killing-ring-save.
I add the following to my ~/.emacs :
(global-set-key (kbd "C-a") mode-specific-map)
(global-set-key (kbd "C-c") 'kill-ring-save)
(global-set-key (kbd "C-f") 'isearch-forward)
(global-set-key (kbd "C-v") 'yank)
(global-set-key (kbd "C-s") 'save-buffer)
(defun my-c-initialization-hook ()
(define-key c-mode-base-map (kbd "C-a") mode-specific-map)
(define-key c-mode-base-map (kbd "C-c") 'kill-ring-save))
(add-hook 'c-initialization-hook 'my-c-initialization-hook)
But this doesn't work. Ctrl+c is still regarded as a prefix, so I can't use it
as kill-ring-save. Furthermore, if I type Ctrl+a Ctrl+c, it said it's not
defined. (I thought it will have the same result as I type Ctrl+c Ctrl+c)
The C-c binding is tricky, CUA mode solves it well, by only making it do kill-ring-save when you have a region marked.
First, Control-S is an ASCII control character -- ^s and ^S are the same character.
Keys are something different from characters, however, and if you are using Emacs with a window manager then you can distinguish the keys C-s and C-S-s. The latter is Control-Shift-s.
The problem you are hitting is that if you do not explicitly bind the shifted version of a letter key, then the shifted letter key uses the binding of the unshifted key. This is a "feature".
So you need to bind both C-s and C-S-s.
(global-set-key (kbd "C-s") 'save-buffer)
(global-set-key (kbd "C-S-s") 'another-command)
If you're running emacs in a terminal, then the reason for the shift-ctl-c issue could be the terminal driver. In that case, give the command stty stop undef, then run emacs again, and see if it affects the problem. Also, see if you get same problem with shift-ctl-other letters