I want to change the color which in package import go defer in go-mode ,need a regex..these code only change the pacakge word color

    (defface my-font-lock-gokeyword-face
    `((t (:foreground "#ab4642"))) ;; change to desired color
    "Face for go keyword.")

    (font-lock-add-keywords 'go-mode
    ;; change to desired operator regex
    '(("package"  0 'my-font-lock-gokeyword-face)))

enter image description here

  • Do you want all keywords to be fontified differently, or only a subset?
    – Basil
    Apr 4 at 16:40

If you change "package" to "^package.*" I think it will match every line that starts with package to the end of the line. I don't know enough about go to know if that is always the right thing to do here.

  • thanks for your reply ,i update the comment. you can see this pic ,i think its better. Oct 11 '19 at 13:19
  • I don't get what you want. You just want the package word in another color than it already is? Oct 11 '19 at 18:19

If I understand correct, just add the other keywords to the list. Something like:

(font-lock-add-keywords 'go-mode
;; change to desired operator regex
'(("package"  0 'my-font-lock-gokeyword-face)
  ("go"  0 'my-font-lock-gokeyword-face)
  ("defer"  0 'my-font-lock-gokeyword-face)
  ("package"  0 'my-font-lock-gokeyword-face)))

I understood you want the same color for all four keywords, then this is fine. Otherwise you can change the faces to whatever color you want for the keywords.

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.