Search code examples
emacs

New Doom Emacs user - trying to set global-set-key


I've installed neotree in Doom Emacs. I would like to bind function key 8 ([f8]) using global-set-key. i've put

(global-set-key [f8] 'neotree-toggle)

in init.el, config.el, and custome.el. When I press f8, emacs says " is undefined".

if i type in ":neotree-toggle", it works.

Can you help?


Solution

  • In emacs-lisp (elisp), keys can be represented in different ways, vector or string. The usual way to represent them is to use the macro kbd (for keybinding of course) that transforms a simple string like "C-h f" to the key representing CTRL+h f.

    If you want to learn more about that you can read the emacs manual about keys here, or this blog post that is pretty good here.

    Finally, if you look at the description of the function global-set-key with C-h f RET global-set-key :

    global-set-key is an interactive compiled Lisp function in ‘subr.el’.
    
    (global-set-key KEY COMMAND)
    
      Probably introduced at or before Emacs version 1.4.
    
    Give KEY a global binding as COMMAND.
    COMMAND is the command definition to use; usually it is
    a symbol naming an interactively-callable function.
    KEY is a key sequence; noninteractively, it is a string or vector
    of characters or event types, and non-ASCII characters with codes
    above 127 (such as ISO Latin-1) can be included if you use a vector.
    
    Note that if KEY has a local binding in the current buffer,
    that local binding will continue to shadow any global binding
    that you make with this function.
    

    You have to give it a key than a command, so in your case, it'll be something like :

    (global-set-key (kbd "<f8>") 'neotree-toggle)

    See how special keys are dealt with with kbd here.