Non-arrow navigation of auto-complete menu in Emacs? - emacs

When the auto-complete menu pops up in Emacs, what key do I use to navigate up and down the menu besides the up and down arrows? I tried C-n and C-p but that makes the menu disappear and move my cursor up and down the text area instead.

You can also use M-p and M-n to select the previous and next candidates respectively as described in the Summary section of the User Manual.

An answer to this question with the C-n and C-p keybindings could be:
; Cycle candidates with C-n and C-p
(setq ac-use-menu-map t)
Setting this variable (that comes from auto-complete.el) to true will remap the keybindings. auto-complete.el says a special keymap ac-menu-map on completing menu will be used. This is where the binding is made.

Related

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: setting doc-view-continuous doesn't work with modified key-bindings

I work in Emacs with ergoemacs minor mode turned on. This minor mode changes C-n and C-p to M-k and M-i correspondingly.
In doc-view mode I can move up and down inside one page with M-i and M-k but when the end (beginning) of the page is reached the scrolling stops.
I have set doc-view-continuous variable to t. Here is the result:
continuous scrolling with M-k and M-i doesn't work if ergoemacs minor mode is turned on
continuous scrolling with C-n and C-p works if ergoemacs minor mode is turned off
next page C-x,] and previous page C-x,[ always work
continuous scrolling with mouse wheel always works
PS:
While writing this post I've found out the following:
in doc-view mode C-p is bound to doc-view-previous-line-or-previous-page function which behaves in different ways depending on doc-view-continuous
in doc-view mode + ergoemacs minor mode M-i is bound to image-previous-line function
This difference is the reason of the problem. I will try to use doc-view-mode-hook.
Edited:
Here is the startup code that works for ergoemacs mode:
;; adjust docview mode
(setq doc-view-continuous t)
(defun adjust-doc-view ()
(ergoemacs-local-set-key (kbd "M-i")
'doc-view-previous-line-or-previous-page)
(ergoemacs-local-set-key (kbd "M-k")
'doc-view-next-line-or-next-page)
)
(add-hook 'doc-view-mode-hook 'adjust-doc-view)
The thing I don't understand is why doc-view functions are bound to standard keys but are not bound to ergoemacs keys.
Apparently doc view binds its commands explicitly to C-n and C-p. My guess would be that ergoemacs remaps the usual commands that are bound to those keys, to the keys M-k and M-i instead. Ergoemacs probably does not know about the doc-view commands in question.
Consider filing an enhancement request for ergoemacs, so that it provides a user option whose value is the list of commands to remap this way. That way, instead of doing what you do above, you can just customize the option.
For an example of code that defines such an option, you can refer Xah Lee (author of ergoemacs) to file icicles-opt.el, option icicle-top-level-key-bindings.

How to scroll up in Emacs ansi-term

I've been Googling around and looking at Emacs built-in help but I have yet to determine how to scroll up (or down) in Emacs ansi-term.
I'm using Emacs 23.3.1, OS X, in iTerm2. Thanks!
Edit: I've noticed most the advice people give me doesn't work in ansi-term but does work in eshell. I have since moved to eshell.
In general, if you don't need full screen terminal emulation, shell or eshell are better choices.
However, if you decide to stick with ansi-term, press C-c C-j to go into line mode. Then you can move around normally with the usual cursor movement keys. Press C-c C-k to get back into char mode to interact with the terminal.
Alternatively, you can scroll backwards a screen at a time with C-c C-v and just enter text to scroll back to the terminal input point.
Take a look at the Emacs documentation on term-mode (most of which applies equally to ansi-term) for more information.
Shift-page up/down (in Emacs-speak, S-prior/S-next) will work using the default bindings.
(While the normal C-h m/C-h b don't work to see mode information and bindings in this mode, you can still use C-c M-x describe-mode/describe-bindings, or depending on your setup, use F1 or the help key instead of C-h.)
install evil-mode at first, press C-z to switch to vim key binding.
You can use C-f, C-b to scroll up and down
you can use 20% to jump to to the top 20% of the buffer
you can use /, ?, #, * to search the text in the buffer.
all the grep/filter commands now usable (occur, swiper, helm-swoop, .... just name a few)
you can narrow/widen the buffer
you can yank text
Have you tried: Page up, up arrow, Ctrl-V,Alt-V
For ansi-term, I have this in my .emacs:
(add-hook 'term-mode-hook
(function
(lambda ()
(define-key term-raw-map [?\C-c prior] 'scroll-down)
(define-key term-raw-map [?\C-c next] 'scroll-up))))
Then I can use C-c pgup and C-c pgdn to scroll.
I was having the same issue but with multi-term (zsh), and after reading the response from #muffinista (the C-v did not work for me) but the Alt-v worked to go 1 page, after that you can use the normal C-p and C-n to scroll up and down.
This worked for me, but it depends a lot on which term you are using and key bindings you might have.
Up and down are Ctrl-P and Ctrl-V. There's a whole long list here

Emacs copy with regex

I have a text file. Can Emacs select text based on regex and put it in kill-ring, so I can copy it somewhere else? Something like regex-kill-ring-save?
inspired by the already given comments (the Charles answer doesn't work as I would want it), I added a new function to the isearch/isearch-regexp mode map which puts only the matching string into the kill ring (whereas Charles proposal kills from current point to end of matching string):
(defun hack-isearch-kill ()
"Push current matching string into kill ring."
(interactive)
(kill-new (buffer-substring (point) isearch-other-end))
(isearch-done))
(define-key isearch-mode-map (kbd "M-w") 'hack-isearch-kill)
The nice thing about the isearch/isearch-regexp approach (which you can enable with C-s and C-M-s respectively) is that you can see your search string growing and you can copy it with M-w as soon as you are satisfied (and go back to where you have been before with C-u C-Space).
This works for me with Emacs 23.1. Don't know if it will work in all situations. Anyway I hope you find it useful :)
UPDATE: going through the emacswiki I stumbled over KillISearchMatch which suggests more or less the same (plus some more tips ...).
Cheers,
Daniel
I'm not sure if there is such a function already, but what you can do it with a keyboard macro:
Start recording a kbd macro: C-x (
Search for your regexp with search-forward-regexp
Move to the beginning of your match (the text you want to kill) with the various emacs navigation commands, e.g. search or backward-word etc.
Mark: C-spc
Move to the end of your match
Kill the text: C-w
You can then name the keyboard macro with M-x name-last-kbd-macro so that you can execute the macro with a name rather than with C-x e.
If you want to save the macro for future sessions, you can open your .emacs and insert the macro into the buffer with M-x insert-kbd-macro. After than you can bind a key to the macro just like you bind keys to normal emacs functions, e.g. (global-set-key "\C-c m" 'funky-macro-macro).
More about emacs keyboard macros
Isearch+ does this already. It optionally sets the region around the search target. You can use C-SPC C-SPC or M-= C-SPC at any time during Isearch to toggle this.
isearchp-deactivate-region-flag is a variable defined in isearch+.el.
Its value is t
Documentation:
Non-nil means isearching deactivates the region.
See also option isearchp-restrict-to-region-flag.
You can toggle this option using M-= C-SPC during Isearch.
You can customize this variable.

Improved tab in Emacs

I want to override the bad default tabbing scheme in emacs so that it will work like most other editors (eclipse, notepad++). I want to set it so that regardless of mode, tab will insert a tab, and pressing enter will keep me at my current tab depth.
I tried this, but it does nothing:
(global-set-key (kbd "TAB") 'tab-to-tab-stop)
(setq default-tab-width 4) ;; 8 is way too many
To make the Enter key take you to the next line and indent it automatically, you can put
(global-set-key (kbd "RET") 'newline-and-indent)
in your .emacs. [Or you can hit C-j instead of Enter.] Once you have that, you'll never need to insert tabs manually, because Emacs automatically indents a new line to extra depth after an opening brace, etc. If you do want to change the indentation, you can hit TAB until it takes you to the right indentation, then start typing from there. [And when you type a closing brace, Emacs is smart enough to take that brace one indentation level backwards.]
You should remove the (global-set-key (kbd "TAB") 'tab-to-tab-stop) for this to work.
Many major modes override the TAB binding, for example cc-mode binds TAB to 'c-indent-to-column.
The 'global-set-key that is suggested does nothing as almost every major mode has overridden the TAB.
One trick that might work for you is to copy the approach that 'pabbrev uses, and define a global minor mode that has the TAB bound. You could do that like so:
(defvar just-tab-keymap (make-sparse-keymap) "Keymap for just-tab-mode")
(define-minor-mode just-tab-mode
"Just want the TAB key to be a TAB"
:global t :lighter " TAB" :init-value 0 :keymap just-tab-keymap
(define-key just-tab-keymap (kbd "TAB") 'indent-for-tab-command))
However, this disables all TAB completion. You'll probably get best results by overriding each of the major-modes one by one (so as to avoid mussing up TAB completion).
This bugged me, too, when I first started using Emacs. I've come to love it, though. If I want to indent appropriately, I hit <tab>; if I want to insert a literal tab, I hit M-i (Meta and 'i' or <Alt>-<i> in some parlances) which is bound to tab-to-tab-stop.
I think trey jackson's answer is probably what you want, except possibly use 'self-insert-command instead of 'indent-for-tab-command. I personally prefer emacs' default behavior, but self-insert-command does what it says instead of trying to do anything fancy like make sure your code is well-formatted.
The few times I actually want to insert a tab (not indent) I press M-i.
You may be interested in this minor mode I created at http://github.com/vohrta/regtab.
It makes it so that when you press the tab key either a tab character (if indent-tabs-mod is not nil) or tab-width spaces will be placed at point. The mode is also capable of handling what you may consider regular behavior on a region of selected text and shift-tabbing to remove tabs at the beginning of the line (or set of lines).
You can enable or disable it at any time by pressing M-x regtab-mode.
C-j does the newline + indent functionality that you want out of pressing Enter.