55

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"?

john smith
  • 653
  • 1
  • 5
  • 5

3 Answers3

79

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
phils
  • 48,657
  • 3
  • 76
  • 115
  • 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. – Student Apr 21 '20 at 02:00
  • 3
    @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`. – phils Apr 21 '20 at 02: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! – Student Apr 21 '20 at 02:14
17

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.
  ...)
Drew
  • 75,699
  • 9
  • 109
  • 225
4

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

(print major-mode)

sidharth arya
  • 235
  • 1
  • 7
  • 3
    This seems to repeat @phils's answer: examine variable `major-mode` (https://emacs.stackexchange.com/a/18084/105). – Drew Sep 09 '18 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. – sidharth arya Oct 05 '18 at 20:12