Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs: set and toggle show-trailing-whitespace

Tags:

emacs

Two related questions using emacs 23.3.1 on linux:

First, why can't I set the value of show-trailing-whitespace to t with setq as shown below? When I put the setq version in my .emacs it does not change the value (as seen functionally and by using M-x describe-variable).

(setq show-trailing-whitespace t)  ; Does not change variable value or give error

(custom-set-variables              ; Sets show-trailing-whitespace as expected
 '(show-trailing-whitespace t))

Second, how can I toggle the value between t and nil? I thought that this answer was exactly what I needed, but it doesn't work in this case. I used:

(global-set-key "\M-ow" 'tf-toggle-show-trailing-whitespace)

(defun tf-toggle-show-trailing-whitespace ()
    "Toggle show-trailing-whitespace between t and nil"
    (interactive)
    (setq show-trailing-whitespace (if (= show-trailing-whitespace nil) t nil))
    (redraw-display))

When I hit M-ow I get an error Wront type argument: number-or-marker-p, nil. Any ideas?

like image 898
Tom Aldcroft Avatar asked Jul 28 '12 12:07

Tom Aldcroft


People also ask

How do I get rid of trailing whitespace in Emacs?

Type M-x delete-trailing-whitespace to delete all trailing whitespace. This command deletes all extra spaces at the end of each line in the buffer, and all empty lines at the end of the buffer; to ignore the latter, change the variable delete-trailing-lines to nil .

How do you ignore whitespace trailing?

You can prefix - to disable any of them (e.g. -trailing-space ): ### blank-at-eol treats trailing whitespaces at the end of the line as an error (enabled by default).

What is a trailing whitespace?

Trailing whitespace. Description: Used when there is whitespace between the end of a line and the newline.

What is leading or trailing whitespace?

A leading space is a space that is located before the first character (letter, number, punctuation mark) in a text entry field. A trailing space is a space that is located after the final character in a text entry field. You will not be able to save the text if this error message is showing.


1 Answers

First: as the describe-variable tells you show-trailing-whitespace is a buffer variable. This means that doing a setq only sets it for the current buffer and therefore has no effect when done in the .emacs file. To have something similar to what custom do you need to use setq-default instead of setq. This will work for all the buffers.

Second: For toggling you might want to use setq if you want to toggle on a buffer per buffer basis. The error you get is that you use = which is to test if two numbers are equal. A toggling is done in a cleaner fashion by using not. As a side note, the (redraw-display) command does not seem to be doing anything.

(defun tf-toggle-show-trailing-whitespace ()
  "Toggle show-trailing-whitespace between t and nil"
  (interactive)
  (setq show-trailing-whitespace (not show-trailing-whitespace)))
like image 131
Nicolas Dudebout Avatar answered Sep 20 '22 06:09

Nicolas Dudebout