Initially, I had the following defined in my .emacs.d/init.el
(defun go-mode-setup ()
(setq compile-command "go build -v && go test -v && go vet && golint")
(define-key go-mode-map (kbd "C-c C-c") 'compile)
)
While in go-mode, everything else seemed to work fine, but his did not seem to set or respect my Cc Cc command, instead producing
C-c C-c is undefined.
So, I added an explicit hook:
(add-hook 'go-mode-hook (lambda () (define-key go-mode-map (kbd "C-c C-c") 'compile)))
This now respects my Cc Cc kbd shortcut, but still ignores the compile-command I set. Unfortunately, it seems to compile the entire folder rather than just the main.go file I am working on.
Compile command: make -k
How can I set Cc Cc to use the compile-command I set?
Your code works for me. The only thing missing is an add-hook
. You probably want to set the compile-command
to be buffer-local as well.
Here's my code that definitely works:
(defun jpk/go-mode-hook ()
(make-local-variable 'compile-command)
(setq compile-command "go build -v")
(define-key go-mode-map (kbd "C-c C-c") #'compile))
(add-hook 'go-mode-hook #'jpk/go-mode-hook)
I strongly recommend against redefining compile
like in @jdc's answer. If you must define your own command, do it with a different name.
You may want to check out the multi-compile package, it allows you to set multiple compile commands (optionally major-mode specific).
This should work:
(add-hook 'go-mode-hook (lambda ()
(defun compile ()
(setq compile-command "go build -v && go test -v && go vet && golint"))
(define-key go-mode-map (kbd "C-c C-c") 'compile)))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.