24

I understand that nil, 0 and/or -1 are used for disabling some features and t, 1 and/or +1 are used for enabling some features. When assigning variables or enabling/disabling minor modes, which should I choose?

I have seen all of these variations used at one point or another. It leads me to wonder if there's actually a difference between them. I have seen that sometimes using 0 doesn't work for me, while nil does. Are there differences of where they are used?

2
  • 1
    I have re-written the question to more clearly state what I think you are asking. Feel free to revert the edit if I am wrong.
    – nispio
    Oct 22, 2014 at 16:17
  • @nispio Your edits are just fine. Thank you very much. :)
    – greduan
    Oct 23, 2014 at 13:53

3 Answers 3

20

TL;DR

Before you set any variable, you must know how that variable is to be interpreted. Similarly, before you call any function (inluding those used to toggle minor modes), you must know how the arguments of that function are interpreted.

Use C-hf and C-hv to look at the documentation for the function or variable in question. It should specify the values that are expected.

"Non-nil" means literally anything that is not nil. This includes 0 and negative numbers.


Minor Modes

Let's take a specific example. Type C-hfblink-cursor-mode and hit RET to see the function documentation for blink-cursor-mode:

(blink-cursor-mode &optional ARG)

Toggle cursor blinking (Blink Cursor mode). With a prefix argument ARG, enable Blink Cursor mode if ARG is positive, and disable it otherwise. If called from Lisp, enable the mode if ARG is omitted or nil.

We can enable Blink Cursor mode in any of the following ways:

(blink-cursor-mode)           ; Omitted argument
(blink-cursor-mode 1)         ; Positive argument
(blink-cursor-mode t)         ; True argument
(blink-cursor-mode nil)       ; nil argument (don't use this)

Notice that an argument of t will work, even though the doc string didn't specifically mention it. While this is often the case, your safest bet is to use what the doc string tells you to use, which in this case is a positive value.

Also, notice that an argument of nil will work. I would strongly recommend against nil in this way because it makes your intention unclear. If I were skimming over your lisp code and I saw a nil argument, I would assume that you wanted to disable the minor mode.

We can also disable blink-cursor-mode in the following ways:

(blink-cursor-mode 0)         ; Non-positive argument
(blink-cursor-mode -1)        ; Negative argument

Notice again that nil is not one of the ways to disable this minor mode. This is true of almost any minor mode you will encounter.


Variables

Now let's look at an example of a variable. Type C-hvtruncate-lines and hit RET to look at the documentation for the variable truncate-lines:

truncate-lines is a variable defined in `C source code'.

Non-nil means do not display continuation lines. Instead, give each line of text just one screen line.

You can turn on truncation in any of the following ways:

(setq truncate-lines t)       ; Boolean true value (non-nil)
(setq truncate-lines 1)       ; Positive value (non-nil)
(setq truncate-lines 0)       ; Zero value (non-nil)
(setq truncate-lines -1)      ; Negative value (non-nil)

It may surprise you that the 0 and the -1 will work. Again, I would recommend against using them because it makes your intentions unclear.

The only way to disable truncation is this:

(setq truncate-lines nil)     ; nil value

In other words you can set truncate-lines equal to numbers, letters, strings, lists, or anything else you want, as long as it does not evaluate to nil it will enable truncation. (But you should really stick with t or 1).

1
  • 1
    This answered my question. Thank you. :)
    – greduan
    Oct 22, 2014 at 17:50
0

These are all different things. 0, 1, and -1 are different numbers; nil is a symbol.

They are each used all over the place, for many different things. To find out what each is used for in a particular context, consult the doc for that context. And that includes contexts of turning different modes on or off, enabling and disabling different things using variables, and lots of other contexts.

In sum:

  • Your question is far too broad to be useful.
  • You need to consult the doc: (a) in general, to learn a little about Emacs Lisp, and (b) in particular contexts (e.g., the doc for a particular mode).

The doc for a given mode generally tells you how to turn it on and off. If it tells you to use 1 or -1 or nil or non-nil for something, then that's what it means. There are some general rules for turning modes on and off (interactively and from Lisp code). But it sounds like you really need to start by getting some general background.

Consult the Emacs doc to learn how to ask for help and other information from Emacs itself: how to check the value of a variable, how to see the doc of a variable or a function (including a function that turns a mode on/off), and so on.

You can start by using C-h C-h (Ctrl-h Ctrl-h), to learn about the Emacs help system. And build from there.

C-h r puts you into the Emacs manual. Try the Emacs tutorial: C-h t. And check the Emacs Wiki Newbie page and the page about LearningEmacs.

To start learning about Emacs Lisp: C-h i, then choose the manual named Emacs Lisp Intro, and start reading. And check the Emacs Wiki page about Learning Emacs Lisp.

7
  • 2
    Thanks for your answer. :) I guess I was asking about what contexts they're used in. Should have made that clearer. Thank you. :) I'm aware of the main differences between the symbols, like how nil is the logical equivalent of false and t of true and the rest are just numbers. But I wasn't sure of what contexts they were usually used in.
    – greduan
    Oct 22, 2014 at 2:30
  • See what I wrote about general information about modes (linked to the manual). In spite of such general information, you should always start by checking the doc for the particular mode you are interested in. It will tell you what to use to turn it on or off. If it does not, then the general behavior described in the manual applies, but start with the particular mode doc. The point is not the differences between those things in and of themselves. The point is differences in how they are used in particular contexts. For that, start by asking the context.
    – Drew
    Oct 22, 2014 at 2:34
  • 2
    OK. Thank you very much. :) Cool that Emacs got it's own SE site eh?
    – greduan
    Oct 22, 2014 at 2:40
  • 1
    P.S. Don't worry. I'm sure that others will spill the beans here about some of the general rules. ;-) I prefer to (a) get you to consult the doc about this, because it was written with just this learning in mind, and (b) get you to start by always asking the particular mode about itself. The general rules do not trump mode particulars, and if you ignore the latter then you will miss things.
    – Drew
    Oct 22, 2014 at 2:42
  • What will be cool will be when SE gets its own Emacs mode. ;-)
    – Drew
    Oct 22, 2014 at 2:43
0

It seems that in most cases you should use 1 and -1. But sometimes this doesn't work and you need to use t and nil instead. Your safest bet is to use what the docstring, which is available by typing C-h f for a function and C-h v for a variable, tells you to use.

Just in case, here are quotes from other Emacs users that I found most helpful:

"As with any mode defined using define-global-minor-mode, the only valid values are numbers, nil, and toggle. The fact that t works is only due to the fact that prefix-numeric-value doesn't choke on it and treats it like 1." - Lindydancer

- global-auto-revert-mode doesn't seem to work?

"I've run into some modes where you have to pass t or nil instead of numbers." - Joseph Garvin

"@JosephGarvin It might be worthwhile to find out which those modes are... and may be file a bug report to fix that inconsistency." - Kaushal Modi

- global-auto-revert-mode doesn't seem to work?

"Your safest bet is to use what the docstring tells you to use." - nispio

- How to choose between nil and 0, or t and 1 when setting variables or enabling modes

And here is an example:

(global-auto-revert-mode &optional ARG)

If called interactively, enable Global Auto-Revert mode if ARG is positive, and disable it if ARG is zero or negative. If called from Lisp, also enable the mode if ARG is omitted or nil, and toggle it if ARG is ‘toggle’; disable the mode otherwise.

;; So, to put in other words,
;; t, 1, nil, or if used without argument - enables global-auto-revert-mode
;; 0, -1                                  - disables it
(global-auto-revert-mode 1)

See also: https://stackoverflow.com/q/49370733

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.