Modify Emacs tab behavior with Sweave documents - emacs

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).

Related

Emacs insert true tabs by default

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)
-*-

emacs switch to last active window?

I was wondering if there is a way in Emacs lisp to jump back to the last active window, just as popd would do in Linux?
The reason I ask is that in some environments for evaluating code (e.g. babel-repl) the editing area for the source code loses focus to the REPL once the REPL is launched. I'd like to change its behavior and switch the focus back to the editing area, e.g., by adding an additional command in elisp to jump back to the last active window before launching the REPL.
previous-window or get-mru-window should do what you want. You can then switch with
(select-window (previous-window))
If you are interested in switching just the active buffer, you can use:
(defun prev-window ()
(interactive)
(other-window -1))
(global-set-key [(f12)] 'prev-window)
(global-set-key "\C-cp" 'prev-window)
(global-set-key "\C-cn" 'other-window)
I have this in my ~/.emacs file. CTRL+C then p (or F12) to go to the previous buffer. CTRL+C then n to go to the next buffer. The function other-buffer lets you revisit the last visited buffer. By supplying a negative number you can reverse the order that it goes through the buffer list.

Highlighting trailing whitespace in emacs without changing character

I am trying to get emacs to highlight trailing-spaces. I have tried using WhiteSpace, and also tried setting show-trailing-whitespace variable to true, but in each case it changes the representation of newline and space characters to $ and · characters, as shown in this screen capture.
Ideally I would like to just see the trailing whitespace highlighted in red without any such characters.
Disclaimer: I'm new to emacs, so this may well be obvious.
I don't use any library. I just set show-trailing-whitespace to t and any trailing white space is shown in red. The representation of newline and space characters is NOT changed.
Actually, my ".emacs" file contains this simple line:
(setq-default show-trailing-whitespace t)
In case you don't want to edit your ".emacs" file, you may try:
C-h v show-trailing-whitespace RET then click the customize link
(or just M-x customize-variable RET show-trailing-whitespace RET)
Click the toggle button to set it to on (non-nil)
Click the menu button State > Set for Current Session
Click the menu button State > Save for Future Sessions
[EDIT] (thanks to Francesco Frassinelli for his comment)
With setq-default, the value is changed for every mode.
If you want to disable it for some mode (like term-mode for example), you have to:
find the mode name of the current buffer. Usually you can get it from within the buffer with M-x describe-mode RET (shortcut C-h m or <f1> m).
find the entry "hook" for this mode. Usually, it's the mode name with the suffix -hook. You can find it by searching "hook" in the buffer describing the mode. For example, you may read:
Entry to this mode runs the hooks on ‘term-mode-hook’
add the following to your ".emacs" file:
(add-hook 'term-mode-hook (lambda () (setq show-trailing-whitespace nil)))
or you may try:
M-x customize-variable RET term-mode-hook RET
Click the INS button
Paste (lambda () (setq show-trailing-whitespace nil))
Click the menu button State > Set for Current Session
Click the menu button State > Save for Future Sessions
Note that show-trailing-whitespace automatically becomes buffer-local when set with setq.
Change the value of the whitespace-style variable to
(face trailing)
You might need to restart whitespace-mode for the changes to take effect.
To set a variable, use M-xset-variableEnter.
Another answer is to use library highlight-chars.el (description: Highlight library).
Command hc-toggle-highlight-trailing-whitespace does what you request.
You can also turn on such highlighting automatically, either everywhere or in a given buffer or for a given mode.

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)

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.