66 votes

How do I list all key-bindings available in a given buffer?

C-h b will list all the bindings available in a buffer. This is a mnemonic for help (C-h) bindings (b). You can also get a list of keybindings via C-h m, which is help for the major and minor modes ...
user avatar
  • 20.3k
63 votes
Accepted

How do I get the current major mode?

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
user avatar
  • 43.5k
49 votes
Accepted

How to override major mode bindings

There is a "shortcut" approach too for the same solution if you don't want to define your own minor mode (that I talk about in my first answer). You can install the use-package package available from ...
user avatar
  • 24.3k
36 votes

How to override major mode bindings

You can define your own minor mode and its key map and have that override all other modes (minor + major). That's exactly why I chose to write my own minor mode. Steps to have your key bindings ...
user avatar
  • 24.3k
35 votes
Accepted

How can I use Emacs to edit questions, answers, and comments here?

See the Stack Exchange for Emacs project. From their page: SX will be a full featured Stack Exchange mode for GNU Emacs 24+. Using the official API, we aim to create a more versatile experience ...
user avatar
  • 1,132
28 votes

How do I list all key-bindings available in a given buffer?

I use the command helm-descbinds, which is available via melpa. I mapped it to C-h b because it is basically a drop-in replacement for describe-bindings. The benefit is that it is easier to navigate ...
user avatar
  • 7,813
28 votes

How do I associate a file extension with a specific mode?

The full syntax for specifying file extensions for gnuplot-mode is: (add-to-list 'auto-mode-alist '("\\.g\\'" . gnuplot-mode)) The other point to verify is that you are using Bruce Ravel's recent (...
user avatar
  • 5,375
25 votes
Accepted

How do I automatically load a mode for a specific set of file extensions?

Add these lines to your .emacs file: (require 'yaml-mode) (add-to-list 'auto-mode-alist '("\\.ya?ml\\'" . yaml-mode)) The auto-mode-alist is a variable which emacs consults whenever a new file is ...
user avatar
  • 1,925
25 votes
Accepted

How can I quickly toggle between a file and a *scratch* buffer having the same major mode?

It will be convenient to bind the below function to a key binding of your choice. If you are currently working on a FILE buffer, calling the below function will toggle between FILE's major-mode ...
user avatar
  • 24.3k
22 votes
Accepted

Using a different font for each major mode

buffer-face-set and buffer-face-mode in Emacs 23 or later is designed for exactly this. From the Emacs wiki: ;; Use variable width font faces in current buffer (defun my-buffer-face-mode-variable ()...
user avatar
  • 3,799
21 votes
Accepted

Run command when opening a file of a specific filetype

In Emacs terminology, these are two different steps: Associate files with the .ts extension with the major mode typescript-mode. Run the function tss-setup-current-buffer when Typescript mode starts. ...
user avatar
21 votes
Accepted

How do I write a simple `completion-at-point-functions` function?

Documentation The API completion at point function can be found in the documentation of completion-at-point-functions Each function on this hook is called in turns without any argument and ...
user avatar
  • 7,298
20 votes

Why does *compilation* buffer show control characters?

These are indeed escape sequences which the terminal should interpret as orders to change the text color. Normally they shouldn't be used when the compiler is invoked from Emacs (the terminal type ...
user avatar
20 votes
Accepted

How to disable the org-mode in a buffer

Doing again an M-x org-mode does not turn it off. This convention is for minor-modes - Doing a M-x "minor-mode-name" again disables that minor mode. org-mode is a major mode. When you do this, emacs ...
user avatar
  • 571
19 votes
Accepted

How to edit jsx (react) files in emacs?

Solution 1: Step 1, Use https://github.com/felipeochoa/rjsx-mode Step 2, Install Emacs 25+, see https://github.com/mooz/js2-mode/issues/291 Step 3, Patch rjsx-mode with below code (defadvice js-...
user avatar
  • 4,611
16 votes
Accepted

Differences between setting major mode keys with a hook versus adding them to the mode map

The second approach is preferable as it modifies the mode's keymap just once. If you do it using the mode's hook then that will be called every time that mode is enabled in some buffer. Doing so ...
user avatar
  • 24.3k
16 votes

How to edit jsx (react) files in emacs?

In the upcoming release of Emacs 27, JSX support is now built into the default JavaScript mode for Emacs, js-mode. (Try it out! You can build from source using the Emacs 27 release candidate, or e.g....
user avatar
  • 1,168
16 votes
Accepted

How to start emacs in "nothing" mode (`fundamental-mode`)

When you use M-x find-file-literally Emacs will not invoke a mode that is based on the file name. Instead, it uses fundamental-mode as the major mode. From the command line you can use something like ...
user avatar
  • 3,241
14 votes

How do I get the current major mode?

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. ...
user avatar
  • 69.7k
14 votes

How to associate more than one file extension with a given mode in `use-package`

The following should work: (use-package js2-mode :mode (("\\.js\\'" . js2-mode) ("\\.jsx\\'" . js2-mode)) ... :ensure t)
user avatar
13 votes

Remember modes for files without extensions

There are a number of ways to identify the major mode for a file that don't rely on an extension, see Choosing File Modes in the manual. Depending on the kinds of files you are dealing with, perhaps ...
user avatar
  • 19.2k
13 votes
Accepted

Disabling electric-indent-mode for one mode (latex mode) only

The command you need is electric-indent-local-mode. You can use this to turn off electric-indent-mode in any buffer by calling it manually: M-x electric-indent-local-mode. This is a toggle, so calling ...
user avatar
  • 20.3k
12 votes

Specify major-mode for files with multiple extensions

The auto-mode-alist variable does what you need. Never bind anything to the major-mode variable. The documentation explains how you can set major-modes per file extension and, most importantly, it ...
user avatar
  • 22.4k
12 votes
Accepted

Why don't major modes just inspect the faces?

The problem is that it is not more robust. Firstly, the major modes are precisely the ones responsible for deciding what's a comment or a string. If they were able to successfully define them for the ...
user avatar
  • 22.4k
12 votes
Accepted

Use two major-mode-maps in the same buffer

This turned out to be simpler then expected. As suggested in the comments here and on the question: (with-eval-after-load 'erc (require 'markdown-mode) (require 'cl-lib) (setq erc-mode-map ...
user avatar
  • 22.4k
12 votes
Accepted

Inferior Lisp Mode vs Superior Lisp Mode

When you run a process inside Emacs, that process is "inferior" (i.e. a subprocess). IELM M-x ielm is the builtin Inferior Emacs Lisp Mode. It's relatively featureless, but can definitely come in ...
user avatar
  • 10k
11 votes
Accepted

How do I inherit from prog-mode, whilst still supporting older emacsen?

At the cost of an extra top-level symbol binding, there's a very neat solution which avoids repeating the define-derived-mode form: (defalias 'my-fancy-parent-mode (if (fboundp 'prog-mode) 'prog-...
user avatar
  • 2,771
11 votes

How can I use Emacs to edit questions, answers, and comments here?

edit-server might be of some help. It lets you edit any text field inside Chrome with Emacs and then send the text back to the browser with minimum effort. Not exactly what you wanted, but its an ...
user avatar
  • 261
11 votes
Accepted

How to change comment character from # (hash) to % (percent sign) for Octave major mode

Inside your hook put this: (setq comment-start "% "). Also, this will explain why: comment-region is an interactive compiled Lisp function. (comment-region BEG END &optional ARG) ...
user avatar
  • 10.9k

Only top scored, non community-wiki answers of a minimum length are eligible