Emacs indent when press return - emacs

I had many markdowns files in my project and I need to change they everytime.
For example, in a changelog.md I has this format:
## 1.11.3 (2016-10-01)
- Fix bug
- New feature....
When I press return, the emacs add a new tab in the current line and add another tab in the new line created.
How can I deactive this indentation only for markdown files?

I do it by defining a function to disable electric-indent-mode and adding it to the markdown-mode-hook:
(defun my-disable-electric-indent ()
"Disable electric indenting."
(electric-indent-local-mode -1))
(add-hook 'markdown-mode-hook
#'my-disable-electric-indent)

Related

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.

Tab indentation in CoffeeScript using Emacs

I hope this is within the reasonable scope of this site and not too trivial, this is my first post. I am new to Emacs and am trying to set up the environment so that when I start a new line in coffee-mode the automatic indentation is in the form of tabs. As I understood the documentation of coffee-mode all I need to do is set coffee-indent-tabs-mode to t. I've appended my init file with the code below:
(custom-set-variables
'(coffee-tab-width 2)
'(coffee-indent-tabs-mode t))
However when I start Emacs and open a .coffee file, though it gets the tab width right, when I press enter it indents with spaces. Quibbles about whether I need to indent with tabs aside, what am I doing wrong?
In the coffee-mode I find in GNU ELPA, there is no coffee-indent-tabs-mode. I recommend you simply do:
(add-hook 'coffee-mode-hook
(lambda ()
(set (make-local-variable 'tab-width) 2)
(set (make-local-variable 'indent-tabs-mode) t)))
This should work for pretty much any major mode.

Modify Emacs tab behavior with Sweave documents

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

Different tab indent settings in different modes

I'm currently using whitespace-cleanup in my save hook. Using indent-tabs-mode, I'm able to save files without any tabs.
All is well, I don't want tabs in my files. But.
Makefiles do need tabs. That is my problem. How do I change my settings for makefile-mode?
I tried to setq either indent-tabs-mode (the doc says it becomes buffer-local) or whitespace-style, it does not work.
OK, my bad. Files were loaded before changing the mode.
The following code works fine, provided it is loaded in the .emacs before opening any file (I have my own project manager which re-opens last files).
(defun my-tabs-makefile-hook ()
(setq indent-tabs-mode t))
(add-hook 'makefile-mode-hook 'my-tabs-makefile-hook)
I've had the same problem, and it seems that this is a bug in Emacs (as of 24.2). Try this, using the following .emacs:
(setq-default indent-tabs-mode nil)
(add-hook 'after-save-hook 'whitespace-cleanup)
If you open a file, save it, and then open a Makefile, you'll have the problem you described. But if you open a Makefile first, save it, and then open another type of file, you'll have the opposite problem: 8 spaces will be replaced by tabs.
The problem is that indent-tabs-mode is buffer-local, but in whitespace.el it is set to a regular variable called whitespace-indent-tabs-mode. Hence, the first value that's seen is the one that counts.
Here's another workaround that solves some other problems too. Add this to your .emacs:
(defadvice whitespace-cleanup (around whitespace-cleanup-indent-tab
activate)
"Fix whitespace-cleanup indent-tabs-mode bug"
(let ((whitespace-indent-tabs-mode indent-tabs-mode)
(whitespace-tab-width tab-width))
ad-do-it))
The best solution I have found for whitespace is ethan-wspace. It cleans up any whitespace that you made dirty yourself but leaves other whitespace intact.
It works for tabs in makefiles and avoids the messy diff problem where there are lots of diff lines that are just whitespace changes

How to use ctrl-i for an emacs shortcut without breaking tabs [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How do I bind a command to C-i without changing TAB?
I want to redefine the emacs keyboard shortcut control-i to be "MOVE CURSOR UP"
To do this, I added the following line to my .emacs file:
(global-set-key (kbd "C-i") 'previous-line)
What I then discovered is that the tab key, by default, does whatever is bound to control-i, which is obviously not what I want. So, to restore normal tab behavior, I added this to my .emacs file
(global-set-key (kbd "<tab>") 'indent-for-tab-command)
This mostly works. BUT, tab no longer works for auto-completing commands in the mini buffer. How can I fix that? Or is there a better way of going about this?
Thanks.
Control-i and TAB are usually considered the same (in a terminal for instance). However Emacs makes a distinction and allows a separate binding.
See Emacs TAB and C-i.
You can also set a local binding with (local-set-key key binding).
You could create an (interactive) command in your .emacs that would set the local binding, and call that command only in the buffers of interest.
Edit
Example: put this in your .emacs, or in a new buffer and then do M-xeval-current-buffer
(defun mybinding ()
(interactive)
(local-set-key [tab]
'(lambda () (interactive)
(message "hello"))))
Then go to a buffer of interest and M-xmybinding and then press TAB to see the result ("hello" should be displayed as a message in the minibuffer).
Try C-f to open a new file and press TAB which has the same completion behavior as usual.
Using a post in this thread:
How do I bind a command to C-i without changing TAB?
I was able to find a solution:
;; Translate the problematic keys to the function key Hyper,
;; then bind this to the desired ctrl-i behavior
(keyboard-translate ?\C-i ?\H-i)
(global-set-key [?\H-i] 'previous-line)