59

If I am editing a .tex file in emacs, by default (for me) the bottom right corner of the frame will say "LaTeX/P". However, I won't get to that mode by typing "M-x LaTeX/P-mode"; I can only get to it by typing "M-x latex-mode".

Other than Googling it, how am I supposed to know the thing I type to get to that mode is "latex-mode"?

3 Answers 3

86

I think the simplest way is to check the value of the buffer-local major-mode variable, with either of:

  • C-hv major-mode RET
  • M-: major-mode RET
3
  • 1
    How to check if the major-mode is org-mode for example? (equal major-mode "org-mode") always return nil even when I'm in org-mode. Commented Apr 21, 2020 at 2:00
  • 5
    @Student That's because you're comparing two entirely different data types: "org-mode" is a string, whereas the value of major-mode is a symbol, so naturally they are not (cannot be) equal. (equal major-mode 'org-mode) would be t in org-mode buffers. Generally to test the current buffer's major mode you would use (derived-mode-p 'org-mode), which will return non-nil not only for org-mode itself, but also for any mode which was derived from org-mode. Commented Apr 21, 2020 at 2:07
  • 1
    As a emacs noob, I thank you for your answer! Also for those who are new, you can use (type-of major-mode) to check that it's actually a symbol but not a string! Commented Apr 21, 2020 at 2:14
18

C-h m gives you help on the current mode, and it typically tells you the name of the command that turns the mode on.

For example, in Emacs-Lisp mode C-h m tells you that you are in Emacs-Lisp mode. The command that turns the mode on is just emacs-lisp-mode.

C-h m also provides a link to the library that defines the mode, and if you click on that link it takes you to the definition of the mode command. For example, in Emacs-Lisp mode C-h m tells you:

Emacs-Lisp mode defined in `lisp-mode.el' 

And if you click the link lisp-mode.el then Emacs takes you to the definition of command emacs-lisp-mode, which is the command that turns the mode on:

(define-derived-mode emacs-lisp-mode prog-mode "Emacs-Lisp" "Major mode for editing Lisp code to run in Emacs. ...) 
5

It's possible to get the major mode just by evaluating this expression:

(print major-mode)

2
  • 3
    This seems to repeat @phils's answer: examine variable major-mode (emacs.stackexchange.com/a/18084/105). Commented Sep 9, 2018 at 23:15
  • 2
    Yes we are evaluating the same thing variable ofcourse, since that's what stores the value of the major-mode. Difference is just how you want to implement that function. maybe we can define a function, with this command in place and bind it with a key. So my point being elisp vs using emacs mode line. Commented Oct 5, 2018 at 20:12

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.