4

I want to bind Ctlr+s to "save current buffer" (currently SPC f s). How can I do this?

When I search online, I find many complicated explanations of how to do very specific things, like how to bind keys in a certain mode or for a certain file type. It also seems like just binding them normally doesn't work, because the vim keys that spacemacs comes with override it.

Moreover, the default init.el file is very complex (580 lines) so I don't know where I would be putting the keybindings either. I'm hoping there's a place in it where I can put something like source keybindings.el and then put all of my bindings in keybindings.el, but I can't tell where or how to do this.

1 Answer 1

6

For spacemacs, the canonical way to make small modification as such is to put your configs inside the defun of dotspacemacs/user-config inside ~/.spacemacs file.

If this is the first time you make modifications to this function, it should look like this:

(defun dotspacemacs/user-config ()
  "Configuration function for user code.
This function is called at the very end of Spacemacs initialization after
layers configuration.
This is the place where most of your configurations should be done. Unless it is
explicitly specified that a variable should be set before a package is loaded,
you should place your code here."
  (define-key evil-normal-state-map (kbd "C-s") #'save-buffer)
  (define-key evil-insert-state-map (kbd "C-s") #'save-buffer))

Make sure you quit and restart emacs (server or daemon if you uses them) for this to work (Note that closing the emacsclient frame might not be sufficient if your setting allows the daemon/server to persist in background. Use SPC q q for a full restart). In this case, manually evaluate those define-key expressions will also work for your current session.

But you want to put them inside another file, so please continue your reading.


Explanation:

For most cases in spacemacs when you are inside an actual file where you would want to save, evil-mode is activated for sure. That why if you follow certain tutorial online, where you might be told to modify C-s inside global-map, it might not work especially if you are in evil-normal-state. That's because evil-mode is a minor mode, whose keymap has higher priority than global-map.

The above 2 define-key expressions set C-s in normal and insert state of evil-mode to command save-buffer, which should be enough for most of your cases.


For spacemacs, ~/.emacs.d/init.el belongs to spacemacs itself, thus you should not touch it.


If you wanna do something like source keybindings.el, you would most likely do the following (dotspacemacs/user-config is a good place to put these two lines):

(defun dotspacemacs/user-config ()
  "Configuration function for user code.
This function is called at the very end of Spacemacs initialization after
layers configuration.
This is the place where most of your configurations should be done. Unless it is
explicitly specified that a variable should be set before a package is loaded,
you should place your code here."
  (add-to-list 'load-path "/path/to/dir")  ;;; which contains `keybindings.el'
  (load "keybindings"))

where keybindings.el should contain the above two define-key expressions.

(NOTE: Other common methods like require together with provide requires more elisp knowledge at this time and please refer to the manual.)

7
  • Thank you for the detailed explanation! However, after putting those two define-key exprs in my user config block (in ~/.spacemacs.d/init.el), Ctrl+s still does an incremental search instead of saving.
    – Jessica
    Commented Jun 22, 2022 at 3:32
  • @Jessica It works for me. What does it output when you do the following in a file you've opened? 1. In evil-normal-state, do C-h k C-s ? 2. What about in evil-insert-state ?
    – TerryTsao
    Commented Jun 22, 2022 at 5:53
  • Both show me the help page for isearch-forward.
    – Jessica
    Commented Jun 22, 2022 at 5:55
  • @Jessica I see. We've got some differences in our settings. Could you elaborate some more for me, please? 1. Are you using helm or ivy? 2. In the help page, which keymap does it say C-s is coming from? (i.e. as in the phrase (found in ....-map))?
    – TerryTsao
    Commented Jun 22, 2022 at 5:59
  • 1. I think it's helm, but how do I tell? 2. I get (found in global-map).
    – Jessica
    Commented Jun 22, 2022 at 6:01

Your Answer

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

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