I need a keybinding (either a key like F12 or a mnemonic key sequence like SPC f12) in Spacemacs. When hitting the keybinding in a window containing a file in its buffer the shell script script in the directory the file is located in shall be executed.

Which one of the alternatives to define key bindings should I use? I thought about calling a function when the key binding is triggered. How could I get the directory the file in the current window buffer is located in when this function is executed? But probably there is another way to solve this problem.

  • And what is the question?
    – Tobias
    Apr 13, 2019 at 7:29
  • I added questions which relate to a possible solution I thought about. I hope this makes the overall question more clear.
    – thinwybk
    Apr 13, 2019 at 9:30
  • 1
    Buffers with non-nil (buffer-file-name) have the variable default-directory set to the directory of the file. In most cases that is what (file-name-directory (buffer-file-name)) returns. Do you mean that by "root directory of the file in buffer"? Note that for a Linux user the wording "root directory of file in buffer" looks somewhat strange. AFAIK, there the root directory always is /.
    – Tobias
    Apr 13, 2019 at 23:33
  • The docs - Reserved prefix command for user state that SPC o and SPC m o are guaranteed to never conflict with default bindings and which are reserved for users. Can be used instead of e.g. (global-set-key [f12] 'run-script).
    – thinwybk
    Apr 15, 2019 at 14:16

1 Answer 1


For an executable script called script the solution is to put the following code into the user-config section of the ~/.spacemacs file. The reserved prefix command for users is either SPC o or SPC m o. These bindings are guaranteed to never conflict with default key bindings. Hitting SPC o show the user specific key bindings. Hitting SPC o s executes the script.

(defun dotspacemacs/user-config ()


  (defun run-script ()
    (shell-command "$PWD/.script"))
  (spacemacs/set-leader-keys "os" 'run-script)



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.