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

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.

Related

How to set new keyboard shortcuts in emacs?

I am new to emacs.
Is there a way set new key binding permanent for future use. That is I have a set of key bindings I would like to use in all of my future emacs sessions.
The following command is getting deleted (or forgotten) every time I quit emacs.
M-x global-set-key new key-binding command
How to save this key binding for future use?
Save the definitions into your .emacs file located in your home directory.
For example, I have the following there:
(global-set-key "\M-m" blink-matching-open)
This is covered comprehensively in the manual.
A number of answers above (and elsewhere on SO) didn't work for me. First, the meta key is referred to by something else in my emacs 24.3.1 with GTK+ version 3.10.7 (on Ubuntu 14.04).
A number of options pre-existing on my init.el didn't work.
There options on my init.el didn't work (and they include the one in here):
(a) (global-set-key (kbd "M-<up>") 'comment-region)
(b) (global-set-key (kbd "<M-up>") 'comment-region)
(c) (global-set-key [(meta up)] 'comment-region)
Then, I discovered that when I do a M-x describe-key and pressed metakey + up cursor arrow, the description said the key was <s-up>. I tried this:
(a) (global-set-key '<s-up>' 'comment-region)
Which didn't work either.
I then tried:
(a) (global-set-key [s-up] 'comment-region)
Which is the only one that worked after I exited the session.
I think a number of things need to be improved for both the emacs manual and the in-editor help with respect to keyboard shortcuts. As a nearly 20-year user of emacs, I can vouch for this. The OP's confusion is legitimate.

How to make auto-complete work with yasnippet and abbrev?

I want Emacs to work like this:
Let auto-complete auto-popup menu:
(setq ac-auto-show-menu 0.8)
(setq ac-delay 0.1)
Use C-n/p / M-n/p to select auto-complete popup menu candidates:
(define-key ac-menu-map (kbd "M-n") 'ac-next)
(define-key ac-menu-map (kbd "M-p") 'ac-previous)
When selecting a candiate
disable TAB / S-TAB in popup menu selection:
(define-key ac-menu-map (kbd "<tab>") nil)
(define-key ac-menu-map (kbd "<S-tab>") nil)
press Enter to select the candiate, without inserting newline:
;; ???
if the candidate is an abbrev, Enter should only select the candiate:
;; ???
... and pressing Space should cause Emacs to auto-expand the abbrev.
if the candidate is a dabbrev, pressing M-\ on candidate should trigger dabbrev-expand.
pressing TAB / C-i to expand the candidate for yasnippet:
(setq yas-trigger-key "TAB")
I set this, but the trigger does not expand when I press TAB.
pressing TAB to expand a snippet trigger while in a field:
(setq yas-triggers-in-field t)
pressing C-j to jump to next field:
(setq yas-next-field-key '("<tab>")) ;; or "C-j"
How can I expand a snippet within a snippet using yasnippet?
Some explanations
There are two TABs in Emacs:
(kbd "TAB") / (\t, [9])
(kbd "<tab>") / ([tab])
If modes like yasnippet and auto-complete want to bind to TAB, their trigger key must be the same as the original tab command. Since Emacs binds indent-for-tab-command to (kbd "TAB"), it's better to use that as the trigger key. yasnippet binds to it by default, and it is easy to set up auto-complete to trigger using TAB as well:
;; trigger using TAB and disable auto-start
(custom-set-variables
'(ac-trigger-key "TAB")
'(ac-auto-start nil)
'(ac-use-menu-map t))
But in some modes (ruby-mode, markdown-mode, org-mode, etc.), the command is bound to
(kbd "<tab>"). When the real tab key is typed, the function bound to (kbd "<tab>) has higher priority, so yasnippet and auto-complete are not invoked. This is easy to fix by moving the key binding:
(defun iy-tab-noconflict ()
(let ((command (key-binding [tab]))) ; remember command
(local-unset-key [tab]) ; unset from (kbd "<tab>")
(local-set-key (kbd "TAB") command))) ; re-bind to (kbd "TAB")
(add-hook 'ruby-mode-hook 'iy-ac-tab-noconflict)
(add-hook 'markdown-mode-hook 'iy-ac-tab-noconflict)
(add-hook 'org-mode-hook 'iy-ac-tab-noconflict)
My setup
I downloaded yasnippet, auto-complete via the el-get packager manager. I'm using Ubuntu 12.04 and Emacs 24.3.50.1.
Wrapping up
I know this problem is a little long, but it really makes it difficult for me to use auto-complete and yasnippet. If the basic key binding doesn't work smoothly, this slows down my workflow quite a bit. I think many people have similar problems because I found some similar questions on the internet (though none of them are exactly like mine).
As you can see above, some of the relevant settings I already know. (But if you think I made a mistake somewhere, please tell me.) There are also some things I still don't know how to set up (???). Maybe there isn't a way to make all of these settings work together? Let me know if that is the case, and otherwise please make sure none of these setting interfere with each other.
After I get the answer to this question, I hope to write an Emacs extension to initialize all of these settings automatically.
Thanks for your help!
I faced the problem you're describing a long time ago and resolved it like this:
bind auto-complete to TAB (also C-i which is the same)
and yasnippet to C-o.
Abbrevs are on C-o as well, but I don't use them a lot.
The advantages are:
No stateful behavior results in a much more relaxed and productive editing.
You no longer think "what will TAB do in this context?" before pressing,
you just press it.
You no longer check if you got the expected outcome, because there's only one.
You can use auto-complete while in the process of expanding yasnippet.
C-i and C-o are neighbors and very easy to press.
Yasnippets now expand reliably in any mode since no mode overrides C-o.
This may be not what you want right now but consider trying it:
you might like it after a while.
Bind RET or <return> to function ac-expand. This is for select candidate.

How to restore anything-like behavior for TAB autocomplete in helm?

A related question was asked here. But the answer is to get used to the new way autocomplete works in helm. I cannot get used to it, here's why.
Say, I want to open a file /home/user/work/f.txt. I do C-x C-f, it takes me to current dir, say /current/dir/. I hit Backspace and notice that autocomplete won't let me delete /. Ok, turn off autocomplete with C-Backspace. Then kill the line C-a C-k and start typing. Notice that autocomplete doesn't work, turn it back on C-Backspace. Normally I would type the part that I know is probably unique, e.g. /hom and hit Tab.
Not here. As soon as I type /ho, autocomplete resolves it to /home/, but since I type fast, I end up with /home/m, and continue typing now meaningless characters until I notice it. Chances are, by that time I got autocompleted into directories that I had no intent of going.
So I have to constantly watch what autocomplete is doing, rather than rely on what I type and only checking suggested completions when I hit Tab.
I also find myself descending into wrong directories due to occasional typo, and then having difficulty going up a level -- evil autocomplete won't let you fix the situation with a couple of Backspaces.
This interaction of autocomplete behavior and the removal of Tab functionality completely upsets my work, so much that I decided to ask this question. I am looking to either:
restore the old functionality
learn how to use autocomplete in a meaningful way, or
configure helm's C-x C-f to behave more like a linux command line
Please help.
Here are some ido tricks if you want to start using it.
Let me know if helm is better, perhaps I'll switch over.
I tried once shortly, but didn't like it.
Basic setup:
This will give you `ido-find-file on C-x C-f.
(ido-mode)
(setq ido-enable-flex-matching t)
Smex setup:
Install from https://github.com/nonsequitur/smex.
(require 'smex)
(global-set-key "\C-t" 'smex)
Switch buffers with ido:
(global-set-key
"η"
(lambda()(interactive)
(when (buffer-file-name)
(save-buffer))
(ido-switch-buffer)))
(global-set-key
(kbd "C-η")
(lambda()(interactive)
(let ((ido-default-buffer-method 'other-window))
(ido-switch-buffer))))
Tricks:
;; 1
(add-hook 'dired-mode-hook
(lambda()
(define-key dired-mode-map "j" 'ido-find-file)))
(add-hook
'ido-setup-hook
(lambda()
;; 2
(define-key ido-file-dir-completion-map "~"
(lambda ()(interactive)
(ido-set-current-directory "~/")
(setq ido-exit 'refresh)
(exit-minibuffer)))
;; 3
(define-key ido-buffer-completion-map "η" 'ido-next-match)
;; 4
(define-key ido-buffer-completion-map (kbd "C-p")
'ido-fallback-command)
;; 5
(define-key ido-completion-map (kbd "C-.") 'smex-find-function)
(define-key ido-completion-map (kbd "C-,") 'smex-describe-function)))
Quick open file from dired.
Move to home directory one key faster (i.e. ~ instead of ~/).
Cycle buffer candidates with the same key that shows the candidates (a la C-TAB in Firefox).
Useful to have a fall back when you want to create a file-less buffer (ido will try
select an existing buffer unless you fall back).
Useful to jump to function definition/documentation.
If you want TAB completion of directories and file names, map helm-execute-persistent-action to the TAB key:
(define-key helm-map (kbd "<tab>") 'helm-execute-persistent-action)
See also the answer to "How can I change emacs helm-find-file default action[...]".

shift up-arrow doesn't highlight text emacs iterm2

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.

How to switch between visible buffers in emacs?

If I am in split-screen viewing 2 different buffers on Emacs and the cursor is on the top buffer, what's a quick way to move the cursor to the bottom buffer?
Bonus question: if I know a command, is there an easy way to identify what key-combo it's bound to, if any?
To switch to other buffer use: C-x o.
Describe key: C-h k.
Here is a better solution when you open more than two windows(buffers) in one frame:
(global-set-key (kbd "C-x <up>") 'windmove-up)
(global-set-key (kbd "C-x <down>") 'windmove-down)
(global-set-key (kbd "C-x <left>") 'windmove-left)
(global-set-key (kbd "C-x <right>") 'windmove-right)
Now, you can use C-x UP/DOWN/LEFT/RIGHT to go to the above/nether/left/right buffer when you have three or more in one frame, they are more precise than 'other-window and you don't need to install any package.
You even CAN make it to cycle the buffers in the direction(vertically/horizontally) with one of the above shortkeys with configuration in .emacs/init.el file, but I don't recommend it(besides I don't remember it anymore, you can google it if you want).
Of course, you can use other shortkeys other than the ones I use in my .emacs.
You may also be interested in WindMove, which enables "directional" window navigation with <S-up>, <S-right> etc.
With respect to the bonus question, if you know the command (other-window), and you invoke it with M-x other-window, Emacs will show a brief message in the minibuffer stating: "You can run the command `other-window' with C-x n".
There is also M-x where-is which prompts for a command and gives you the current bindings that result in that command (if any).
There is a tutorial that's shipped with Emacs. It actually has the answer to your question (see the section MULTIPLE WINDOWS about 80% into the tutorial). The tutorial can be accessed via C-h t, or M-x help-with-tutorial. There's also a link to the tutorial on the initial splash screen of Emacs. Right below the link to the tutorial is a link to the on-line Emacs Guided Tour. The tutorial walks you through basic editing/movement commands, the guided tour is more of an introduction to what Emacs has to offer.
If you want to navigate among only buffers that are currently displayed, then you really want to navigate among the windows they are displayed in. This gives you a way to do that, using window/frame names that are the same as the buffers:
See Better window navigation in Emacs?