The title contains the crux of my question. In go-mode, tabs and auto-indentation generally works well, except when I am editing strings.

When inside of a string, I'd like the tab key to self-insert. Right now, it does nothing.

  • 3
    C-q TAB. In general, whenever you need to input something that causes self-insert-command to do something other than just appending whatever you typed to the buffer, you could avoid that behavior by prefixing your input with C-q.
    – wvxvw
    Commented Jan 24, 2016 at 16:49
  • @wvxvw Yeah, I am familiar with that. It's just tedious. It seems like there should be a way to automatically turn self-insertion back on, being as that the syntax highlighter recognizes that I am in a string. Commented Jan 24, 2016 at 16:50
  • 1
    Mmmm... not really. What if you actually wanted to indent your code? I typically don't pay attention to where the point is, when I'm indenting the code. Maybe, specifically in Go one could reason that since saving the file will re-indent it anyway, this isn't very important, yet I do it regardless, especially when file is in the middle of adding changes to it.
    – wvxvw
    Commented Jan 24, 2016 at 17:03
  • 1
    Try (setq-local tab-always-indent nil) in go-mode-hook. This is not quite what you asked for (does not check if you are in a string), but it would make it easier to insert tabs. (It should be pretty easy to check if you are in a string using syntax-ppss, but I don't have the time to test it right now.) Commented Jan 24, 2016 at 17:11

1 Answer 1


The function below checks if the point is in a string using syntax-ppss and inserts a TAB if it is, otherwise calls indent-for-tab-command, which is what TAB is bound to in go-mode.

(defun my-indent-or-insert-tab (arg)
  "Insert TAB if point is in a string, otherwise call
  (interactive "P")
  (if (nth 3 (syntax-ppss (point)))
      (insert "\t")
    (indent-for-tab-command arg)))

Here's one way to get the behavior you need with the help of this function:

(add-hook 'go-mode-hook (lambda () (local-set-key (kbd "TAB") #'my-indent-or-insert-tab)))

Let me know if this works for you.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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