reddit / emacs

Can anyone please help me on how to write this function or key mapping in emacs evi?

LowCom · 2022-05-19 08:09
This is the functionality I need: [i should insert space before the word and then exit insert mode of evil. A count given before [i, like 3[i should insert 3 spaces before the word. (This is similar to the mappings of unimpaired vim (which has an emacs port) where [SPC inserts a new line above). My knowledge of elisp is very little and I need ideas on how to write such a function and then bind it to this key mentioned above.
staticJulia · 2022-05-19 10:46
I can not help with the evil part but you can start with those links for the elisp [http://xahlee.info/emacs/emacs/elisp\_universal\_argument.html](http://xahlee.info/emacs/emacs/elisp_universal_argument.html) [https://www.gnu.org/software/emacs/manual/html\_node/elisp/Point.html](https://www.gnu.org/software/emacs/manual/html_node/elisp/Point.html) [https://www.gnu.org/software/emacs/manual/html\_node/elisp/Motion.html](https://www.gnu.org/software/emacs/manual/html_node/elisp/Motion.html)
pwnedary · 2022-05-19 11:33
Here is a starting point for you: (defun my-command (count) (interactive "p") (save-excursion (cl-destructuring-bind (beg _end _) (evil-a-word 1) (goto-char beg) (dotimes (_ count) (insert-before-markers ?\ )))))
marlgl · 2022-05-19 11:35
This doesn't seem to require evil at all. The following should accomplish your task: (defun add-SPC-at-symbol-start (count) (interactive "p") (save-excursion (goto-char (car (bounds-of-thing-at-point 'symbol))) (dotimes (_ count) (insert-before-markers " ")))) (define-key evil-normal-state-map (kbd "[i") #'add-SPC-at-symbol-start)
LowCom · 2022-05-19 12:19
Can you please comment on what each line and keyword does?
SlowValue · 2022-05-19 14:18
In above example, every word after an opening round bracket `(` runs an elisp function. (this is true for but two exceptions `(count` and `(_`). Emacs can tell you what those functions are doing. Just put your cursor onto the word, press `F1 f` and press `enter`. Emacs will then open a help buffer with a documentation to that function. Do that for every line an you get an idea, what is going on.