emacscc-mode

Emacs cc-mode tab behavior


Pressing tab multiple time doesn't move text to the right. Is there is a way to make it behave like Visual Studio's smart indent? First tab indents, subsequent tabs move text to the next tab stop. Thank you.


Solution

  • Something like this?

    (defun even-more-tabby-indent (&optional arg)
      "This indent function tries to be more like Microsoft's IDEs
    than `C-INDENT-COMMAND' and does the following: If we're at the
    beginning of the line or `C-TAB-ALWAYS-INDENT' is true or `ARG'
    is non-nil, indent like a sensible text editor. Otherwise the
    user probably WANTS MOAR TABS. So call `C-INSERT-TAB-FUNCTION'."
      (interactive "P")
      (if (or c-tab-always-indent (bolp) arg)
          (c-indent-command arg)
        (funcall c-insert-tab-function)))
    

    You'll then want to bind tab insertion with something like

    (defun setup-tabby-indent ()
      (local-set-key (kbd "<tab>") 'even-more-tabby-indent)
      (setq c-tab-always-indent nil))
    
    (add-hook 'c-mode-hook 'setup-tabby-indent)
    

    I haven't used MS Visual Studio in many years, so I'm not sure whether this is exactly what you're after, but hopefully it's pretty clear how to modify.