Improved tab in Emacs - 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.

Related

How to override the keybindings for Emacs Org mode org-property-next-allowed-values and org-property-previous-allowed-value

S-RIGHT is the default keybinding for (org-property-next-allowed-values) and
S-LEFT for (org-property-previous-allowed-value) according to https://orgmode.org/manual/Property-Syntax.html. These keybindings are used to cycle through the TODO keywords as well as schedule dates.
I want to change the keybinding to C-RIGHT and C-LEFT, respectively so that they don't interfere with my normal editing. (I use S-LEFT, S-RIGHT for text selection).
I tried to add the following lines to my init.el:
(define-key org-mode-map (kbd "<S-left>") nil)
(define-key org-mode-map (kbd "<S-right>") nil)
The above 2 lines successfully disabled the 2 keybindings.
Then I added the following 2 lines to remap the keybindings:
(define-key org-mode-map (kbd "<C-left>") 'org-property-previous-allowed-value)
(define-key org-mode-map (kbd "<C-right>") 'org-property-next-allowed-values)
However, emacs (I use AquaEmacs on Mac) doesn't recognize the "org-property-previous-allowed-value" and "org-property-next-allowed-values".
I wonder what did I do wrong here?
Thanks
<S-right> is bound to org-shiftright by default, which does different things depending on context. It only does org-property-next-allowed-value if you cursor is on a property.
I don't know why you are having problems with the rebinding, hence my comment asking for clarification. But I thing that you probably don't want to redefine keys at all. Try adding
(setq org-support-shift-select 'always)
to your init file instead. The doc string for org-support-shift-select says (among other things - you should do C-h v org-support-shift-select RET and read the whole thing if you decide to go this way):
In Emacs 23, when ‘shift-select-mode’ is on, shifted cursor keys
start selecting a region, or enlarge regions started in this way.
In Org mode, in special contexts, these same keys are used for
other purposes, important enough to compete with shift selection.
Org tries to balance these needs by supporting ‘shift-select-mode’
outside these special contexts, under control of this variable.
...
If you set this variable to the symbol ‘always’, then the keys
will not be special in headlines, property lines, item lines, and
table cells, to make shift selection work there as well. If this is
what you want, you can use the following alternative commands:
‘C-c C-t’ and ‘C-c ,’ to change TODO state and priority,
‘C-u C-u C-c C-t’ can be used to switch TODO sets,
‘C-c -’ to cycle item bullet types,
and properties can be edited by hand or in column view.
However, when the cursor is on a timestamp, shift-cursor commands
will still edit the time stamp - this is just too good to give up.

How and where to change mouse button behaviour in a special mode

In Markdown mode with flyspell enabled, mouse-2 bound to mouse-yank-primary is also bound to flyspell-correct-word, the flyspell function that displays in a menu correction propositions. If the buffer is empty, the menu is displayed, if not, its content is yanked. Grrr.
I spent some time trying to find where to change this second binding but as I'm not an emacs guru, I just got lost. I tried (global-set-key "S-mouse-2" #'flyspell-correct-word) in ~/.emacs.d/init.el but I didn't manage to get anything working properly.
So the question is how and where to bind say S-mouse-2 to that function. A better solution would be to keep both bindings but to prioritize the menu over yanking when the mouse is over a word that needs correction.
On my GNU Emacs 25.2.2 the command (executed from the *scratch* buffer)
(global-set-key "S-mouse-2" #'flyspell-correct-word)
pops up the debugger. However,
(global-set-key [S-mouse-2] #'flyspell-correct-word)
works, as also
(global-set-key [S-mouse-2] 'flyspell-correct-word)
You can check the effect with:
(global-key-binding [S-mouse-2])

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 auto-complete.el switch tab to spacebar

I've used auto-complete.el in Emacs for ages. I'm using Emacs a bit more these days and my left pinky is starting to get RSI from hitting TAB and ctrl all the time. I've done some keyboard remapping to alleviate the situation but every few characters I'm generally pressing tab to complete a word. Is there any way to switch auto-complete.el from using TAB for completion to spacebar with my less used and stronger thumbs?
Edit2: I was using a really old version of auto-complete.el which meant that #hd1 suggestion didn't work for me immediately.
Going to the source and scrolling down to line 235 shows that if you set the variable ac-trigger-key you can change the key that's used to trigger completion. You should set this variable in your custom-set-variables block in your .emacs file.
I think ac-complete-mode-map is a keymap used when the auto-complete
menu is shown. How about trying something like this instead (this is
my setting):
(define-key evil-insert-state-map (kbd "C-SPC") 'auto-complete)
If you don't use evil, you need to find another keymap. Perhaps a
mode-specific map, or even global-map will work.

Making Emacs tabs behave exactly like vim's

I'm learning currently Emacs and I'm trying to set up my initialization file.
Currently it looks like this (found it somewhere in the web):
(setq indent-tabs-mode t)
(setq-default indent-tabs-mode t)
(global-set-key (kbd "TAB") 'self-insert-command)
(setq default-tab-width 4)
(setq tab-width 4)
(setq c-basic-indent 4)
But it does not behave like Vim's style of tabs.
I just want it to behave like Vim when using tabs.
That means not substituting tabs with spaces (I think Emacs does this by default).
So that everyone can edit files in their preferred tab width. I generally use 4 for the tab width. And that when I press Backspace it will go the same number backwards that means if I've set tab to 4 and I press Tab it shall go back by 4 chars after I've pressed Backspace.
It should also always use 4 spaces for tab. Because sometimes in emacs it does not do that.
Vim's tab handling can be configured, so it's not a good description of what you want to do, but the rest of your description has enough information, for the most part.
The easiest way to cope with tabs is never to use them. So don't be surprised if setting up tabs in the way you like them takes a bit of work.
You've set up the Tab key to insert a tab character. That's not the custom in Emacs: usually the Tab key is used to indent the current line. What you've done is enough for the default, but language-specific modes may still make Tab indent. I presume from your inclusion of c-basic-indent that you're working on C code; so you need to tell C mode that you don't want Tab to indent. This should do it:
(eval-after-load "cc-mode"
'(define-key c-mode-map (kbd "TAB") 'self-insert-command))
Another thing you've run into is that by default, the Backspace key tries to move back by one column rather than one character. The following should make it delete one character:
(global-set-key (kbd "DEL") 'backward-delete-char)
(setq c-backspace-function 'backward-delete-char)