2

I installed the newest doom emacs on macOS. Also configed language packages as

$ emacs ~/.doom.d/init.el
:lang
(go +lsp)
:tools
lsp

$ ~/.emacs.d/bin/doom sync

When I open a .go file, the code became highlight. But after save the file, the code can't been formated automatically. I have installed gofmt and goimports. How to set emacs to use them?

I read this document: lang/go module. It seems it can do it: Auto-formatting on save (gofmt). Maybe because of environment path? So how to call system path from emacs? My GOPATH is ~/go, it set in ~/.zshrc. When I open shell via M+x, something is different from terminal. How to import .zshrc configuration completely?

warmwinter
  • 21
  • 1
  • 3
  • Try running `gofmt` from `M-x` and check this GitHub issue, someone posted a config where it will enable gofmt on save https://github.com/hlissner/doom-emacs/issues/4201 – Marcus Lee Apr 28 '21 at 02:24

2 Answers2

0

In your doom init.el file you need to enable format +onsave in the :editor section. See the snippet below or look at the example init.el (with format +onsave disabled) on the doom emacs git repo

   :editor
   (evil +everywhere); come to the dark side, we have cookies
   file-templates    ; auto-snippets for empty files
   fold              ; (nigh) universal code folding
   (format +onsave)  ; automated prettiness
   ;;god               ; run Emacs commands without modifier keys
   ;;lispy             ; vim for lisp, for people who don't like vim
   ;;multiple-cursors  ; editing in many places at once
   ;;objed             ; text object editing for the innocent
   ;;parinfer          ; turn lisp into python, sort of
   ;;rotate-text       ; cycle region at point between text candidates
   snippets          ; my elves. They type so I don't have to
   ;;word-wrap         ; soft wrapping with language-aware indent
0
$ emacs ~/.doom.d/init.el

:editor
(evil +everywhere)
(format +onsave)

:lang
(go +lsp)

:tools
lsp

$ ~/.emacs.d/bin/doom sync

Keys:

  • use SPC p s to format and save all project's buffers
  • use SPC c o to auto-remove from Go import () (or auto-add, but it mostly happens as you edit text)
  • use SPC m i z z to jump to the Go imports () block
  • use C-o to jump back (I'm not sure how this works so far)

There is no integration between import () and the go mod. For example adding a new package, does not add the corresponding module.

kubanczyk
  • 101
  • 1