4

I switched from Sublime Text a little over a month ago and I prefer emacs, but something that I haven't been able to find are external packages that do more than just give you more than correct indentation.

For example these packages for sublime:

I haven't been able to find the equivalent for in emacs. Do any packages like this exist for emacs?

If they don't, the interesting thing about some of these packages is that they work off running already installed nodejs modules, and I'm curious if I can somehow assign my own keybindings to manually run those nodejs programs.

  • I doubt you really need to invent a package for this given the existence of M-u M-| and a suitable filter program that transforms the given standard input to an alternate form and pushes it to standard output. – wasamasa Sep 8 '15 at 19:11
  • @wasamasa I've never heard of that command. Could you give an example of how you would use it? – m0meni Sep 8 '15 at 19:32
  • "...do more than just give you more than correct indentation..." such as? Might help those of us who have no familiarity with those sublime packages you mention. – Emacs User Sep 8 '15 at 20:19
  • for example if you have something like a+b it'll make it a + b. If you forgot to put a space between your curly brace and parenthesis foo(){ it'll make it foo() { and much more. – m0meni Sep 8 '15 at 20:32
  • @AR7 I believe it will help you get precise solutions if you break down your question into multiple questions and explaining in detail in each question (with the help of well formatted examples) on how you want the auto-formatting to be done. – Kaushal Modi Sep 9 '15 at 2:02
4

For autoformatting code you can use these packages:

  • py-autopep8
  • py-isort
  • web-beautify
  • web-mode (optional)

Autoremove trailing whitespaces and indent regions:

(defun format-current-buffer()
  (indent-region (point-min) (point-max)))
(defun untabify-current-buffer()
  (if (not indent-tabs-mode)
      (untabify (point-min) (point-max)))
  nil)
(add-to-list 'write-file-functions 'untabify-current-buffer)
(add-to-list 'write-file-functions 'delete-trailing-whitespace)  

Autosorting python imports:

(add-hook 'before-save-hook 'py-isort-before-save)
(setq py-isort-options '("-sl")) ;; One module per line

Settings for web-mode:

(add-hook 'web-mode-hook (lambda ()
                           (setq web-mode-markup-indent-offset 4)
                           (setq web-mode-css-indent-offset 4)
                           (setq web-mode-code-indent-offset 2)
                           (setq web-mode-indent-style 4)
                           (setq web-mode-enable-auto-pairing t)
                           (setq web-mode-enable-css-colorization t)
                           (setq web-mode-enable-current-element-highlight t)
                           (setq web-mode-enable-current-column-highlight t)
                           ))

Auto-beautify buffer before save (js2-mode, web-mode, css-mode, you can add more), NodeJS and package js-beautify required (npm install -g js-beautify):

(add-hook 'js2-mode-hook (lambda ()(add-hook 'before-save-hook 'web-beautify-js-buffer t t)))
(add-hook 'web-mode-hook (lambda ()(add-hook 'before-save-hook 'web-beautify-html-buffer t t)))
(add-hook 'css-mode-hook (lambda ()(add-hook 'before-save-hook 'web-beautify-css-buffer t t)))

(eval-after-load 'js2
  '(add-hook 'js2-mode-hook
             (lambda ()
               (add-hook 'before-save-hook 'web-beautify-js-buffer t t))))

(eval-after-load 'js2-mode
  '(add-hook 'js2-mode-hook
             (lambda ()
               (add-hook 'before-save-hook 'web-beautify-js-buffer t t))))

;; Or if you're using 'js-mode' (a.k.a 'javascript-mode')
(eval-after-load 'js
  '(add-hook 'js-mode-hook
             (lambda ()
               (add-hook 'before-save-hook 'web-beautify-js-buffer t t))))

(eval-after-load 'json-mode
  '(add-hook 'json-mode-hook
             (lambda ()
               (add-hook 'before-save-hook 'web-beautify-js-buffer t t))))

(eval-after-load 'sgml-mode
  '(add-hook 'html-mode-hook
             (lambda ()
               (add-hook 'before-save-hook 'web-beautify-html-buffer t t))))

(eval-after-load 'css-mode
  '(add-hook 'css-mode-hook
             (lambda ()
               (add-hook 'before-save-hook 'web-beautify-css-buffer t t))))

(eval-after-load 'web-mode
  '(add-hook 'web-mode-hook
             (lambda ()
               (add-hook 'before-save-hook 'web-beautify-html-buffer t t))))

(eval-after-load 'html-mode
  '(add-hook 'html-mode-hook
             (lambda ()
               (add-hook 'before-save-hook 'web-beautify-html-buffer t t))))

Autopep8 (require autopep8 on system side, install or update with pip):

(add-hook 'python-mode-hook 'py-autopep8-enable-on-save)

P. S. My .emacs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.