onrcnernl · 2022-04-16 10:07
Hey! I've switched to Emacs with Evil Mode and I've been using it for 3 weeks but I am missing a very important feature (at least for me it is important). In vim you can reformat text with "gq{motion}" to format lines. It uses external programs to do so. However in Evil Mode I cannot seem to find its equivalent. Is there any solution to that? I Googled the problem but couldn't find the solution there either. Thank you!
Bodertz · 2022-04-16 10:26
https://stackoverflow.com/questions/34002435/autopep8-reformat-a-region-in-emacs-spacemacs
Maybe that's useful. I don't use Evil, so I don't know.
grimscythe_ · 2022-04-16 12:02
I run a function like this:
```elisp
(defun cleanup-buffer-untabify ()
"Perform a bunch of operations on the whitespace content of a buffer and untabify buffer."
(interactive)
(indent-buffer)
(untabify-buffer)
(delete-trailing-whitespace))
```
Bind it to whatever you want. If you want tabs instead of spaces: instead of `untabify-buffer` use `tabify-buffer`. But frankly, you could just have `indent-buffer` and `delete-trailing-whitespace` and that would do as well.
EDIT:
Sorry didn't realise that `indent-buffer` was custom as well, here it is:
```elisp
(defun indent-buffer ()
"Fix indentation."
(interactive)
(indent-region (point-min) (point-max)))
```
konrad1977 · 2022-04-16 12:59
I just select my region and press tab in visual-state, maybe that's different?
TheFrenchPoulp · 2022-04-16 13:35
`gq` should already be supported by default w/ Evil. It looks like it only *fills* by default which is Emacs parlance (or does it come from somewhere else? I've only saw it used in Emacs contexts) for wrapping content under a specified width, see `C-h v fill-column`.
In your case, hopefully Evil uses `fill-paragraph-function` so you only need to customize it to the format function you want in the modes you want (untested)
mauguro_ · 2022-04-16 14:39
do you know which function is been called when you do tab?
[deleted] · 2022-04-16 18:33
[deleted]
konrad1977 · 2022-04-16 18:34
Sorry no. I'm new to Emacs. I'm using evil and evil-collection. How do I check?
mauguro_ · 2022-04-16 19:49
C-h k and then you press the key, it will tell you what function is being called
konrad1977 · 2022-04-17 06:43
`indent-for-tab-command`
organic-bookworm · 2022-04-17 19:24
I use Doom Emacs, which relies on [format-all](https://github.com/lassik/emacs-format-all-the-code) package for formatting.
You could make it into an Evil operator like this:
(evil-define-operator evil-format (beg end)
"Format text."
:move-point nil
:type line
(save-excursion
(save-restriction
(narrow-to-region beg end)
(format-all-buffer)
)
)
)
(evil-global-set-key 'normal "gq" #'evil-format)
organic-bookworm · 2022-04-17 19:38
>In your case, hopefully Evil uses `fill-paragraph-function`
Just tried it.
The `evil-fill` operator (actually, Emacs command) calls `fill-region`, which, apparently, can't be customized like this.