I have a function which basically inserts function () {};
but properly indented and the cursor positioned appropriately:
(defun insert-function-js ()
(insert "function () {
};"))
(define-key evil-normal-state-map (kbd "SPC dg")
(lambda ()
(interactive)
(call-interactively 'evil-insert)
(insert-function-js)
(evil-force-normal-state)
(call-interactively 'evil-visual-char)
(call-interactively 'evil-previous-line)
(call-interactively 'indent-region)
(call-interactively 'evil-open-below)))
This seems very cumbersome. I would guess there is a better way to write this functionality! One that better leverages elisp's capabilities.
Thanks for the help!
As an answer to your first question, you could use yasnippet
and a snippet based on the function
-snippet provided with Spacemacs [1] to do this for you:
# -*- mode: snippet; require-final-newline: nil -*-
# name: my-function
# key: fn
# --
function () {
$0
};
If you put this snippet in a new file in ~/.emacs.d/private/snippets/
, you can expand it by typing fn
and then pressing M-/ [2]. If yas-indent-line
is set to 'auto
(which it is by default in Spacemacs), then the function should be indented correctly, and the $0
in the snippet puts your cursor in that position after insertion.
The yasnippet
-expansion forms a single undo-action.
[1] The default function
-snippet can be found in ~/.emacs.d/elpa/yasnippet-<VERSION>/snippets/js-mode
[2] using the default Spacemacs bindings, this calls hippie-expand
, which in turn calls yas-expand
.