After a long time looking for solutions to force Emacs to use tab as real characters in C++ code, I realized that the only robust solution is to insert tabs using the "CTRL+Q TAB" key sequence. See https://stackoverflow.com/a/5146702/225186 .
Is there a way to make this the default?
That is to output a real TAB character when pressing the TAB key, without the need of typing CTRL+Q TAB.
Is there a way to activate this option from an Emacs "modeline" (first line of text file enclosed by //-*- and -*-)?
In C++ mode (and many other modes) TAB is considered as a command instead of a character. In c++ mode it is bound to c-indent-line-or-region and it's behaviour is
Indent active region, current line, or block starting on this line. In
Transient Mark mode, when the region is active, reindent the region.
Otherwise, with a prefix argument, rigidly reindent the expression
starting on the current line. Otherwise reindent just the current
line.
You can either use Ctrl+Q Tab or M-i (a bit shorter than Ctrl+Q Tab) commands to insert tab character.
If you want to insert real tab character on pressing TAB (overriding binding for indentation) then you can add following on top of your file to set buffer specific variables.
// -*- eval: (define-key c++-mode-map (kbd "<tab>") 'tab-to-tab-stop)
-*-
or you can bind it to Shift-Tab to insert TAB character (not intuitive though):
// -*- eval: (define-key c++-mode-map (kbd "<backtab>") 'tab-to-tab-stop)
-*-
Related
In Sweave mode pressing tab doesn't move the cursor and kills my auto-complete options (when I hit tab to complete the code the completion disappears as opposed to completing the snippet).
If I space over to the center of the buffer, pressing tab snaps the cursor back to leftmost side of the file.
When I am editing a 'foo.tex' or 'foo.r 'file tab indents and autocomplete behave normally. Is there a way in which I can define tab behavior for Sweave (.rnw) files?
You might try to set this in your init file.
(setq ess-tab-complete-in-script t)
(add-hook 'ess-mode-hook
'(lambda()
(local-set-key (kbd "<tab>") 'ess-indent-or-complete)
))
When you are inside an R code chunk, the first TAB hit will indent the line if it is not properly aligned, otherwise it will auto-complete the word. You can further customise this behaviour with the variable ess-first-tab-never-complete and decide if the completion should occur only at end of lines, end of words etc. (see variable description).
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)
I'm using Emacs 23.1.1 on Ubuntu with Emacs starter kit. I primarily work in the lua-mode.
Is there a way to stop Emacs being so smart about indentation? I'm used to the dumb editors, and press all the required keys manually.
I want to use two spaces per indent, tabs-to-spaces.
When I press RETURN, the new line indentation must match the previous line.
When I press TAB on the leading whitespace, the line contents must be indented by one indentation unit.
When I press TAB on the beginning of empty line, the cursor must move one indentation unit to the right.
Oh, and I'd like to get soft word wrap on 80th column and trim-trailing-spaces on save as well.
Update:
(Would put this in a comment, but it needs formatting)
If I use Thomas's solution, auto-indent on RETURN is "fixed", but TAB still indents weirdly:
local run = function(...)
x
"x" marks the spot where cursor appears after I type the first line and hit RETURN, TAB.
Emacs has a concept of modes, which means that depending on what type of file you're editing it provides special functionality that is useful for that file. Every buffer has one major mode associated and optionally a number of minor modes.
Indentation is one of the things that is typically mode-dependent. That is, you may have to configure indentation separately for every major-mode, because otherwise when you load a new file, its associated major mode may override your indentation settings. It's possible though to write a function that configures indentation and set up Emacs in a way that the function is invoked whenever a new major-mode is started.
In order to realize the settings you want, you'll need to run a few lines of elisp code. (Unfortunately your description of what should happen when you hit TAB leaves out some details, I've implemented the simplest version I could think of below -- if it's not what you want, that can be changed, of course.)
Put the following code in the file named .emacs in your home directory (~):
(setq-default indent-tabs-mode nil) ; use spaces for indentation
(defvar my-indentation-width 2
"The number of spaces I prefer for line indentation.")
(defun my-enter ()
"Inserts a newline character then indents the new line just
like the previous line"
(interactive)
(newline)
(indent-relative-maybe))
(defun my-indent ()
"When point is on leading white-space of a non-empty line, the
line is indented `my-indentation-width' spaces. If point is at
the beginning of an empty line, inserts `my-indentation-width'
spaces."
(interactive)
(insert (make-string my-indentation-width ? )))
(defun my-indentation-setup ()
"Binds RETURN to the function `my-enter' and TAB to call
`my-indent'"
(local-set-key "\r" 'my-enter)
(setq indent-line-function 'my-indent))
(defun delete-trailing-whitespace-and-blank-lines ()
"Deletes all whitespace at the end of a buffer (or, rather, a
buffer's accessible portion, see `Narrowing'), including blank
lines."
(interactive)
(let ((point (point)))
(delete-trailing-whitespace)
(goto-char (point-max))
(delete-blank-lines)
(goto-char (min point (point-max)))))
;; make sure trailing whitespace is removed every time a buffer is saved.
(add-hook 'before-save-hook 'delete-trailing-whitespace-and-blank-lines)
;; globally install my indentation setup
(global-set-key "\r" 'my-enter)
(setq indent-line-function 'my-indent)
;; also override key setting of major-modes, if any
(add-hook 'after-change-major-mode-hook 'my-indentation-setup)
This works for me in Emacs 23, although I may have missed some edge cases. However, these changes are so fundamental that I predict you will run into incompatibilities sooner or later with some major-modes that expect indentation to work they set it up. If you really want to get into Emacs it's worthwhile adapting the habits you inherited from other editors to the way Emacs does things.
For soft word-wrap there is a minor-mode called "longlines" which you can download from here: http://www.emacswiki.org/cgi-bin/emacs/download/longlines.el I haven't used it so I can't tell you how well it works.
Fixing TAB and RETURN:
(global-set-key "\t" 'self-insert-command)
(global-set-key "\r" 'newline-and-indent)
Fill column (haven't tried): say ESC x customize-var, enter fill-column, set to 80.
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.
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.