Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I just tabify begnning of lines in emacs

Tags:

emacs

I like format all my code using tab instead of space, but I just want to convert spaces to tabs at the beginning of each lines.

Can tabify just convert space to tabs at the beging of lines?

like image 777
Tian Yong Avatar asked Jul 24 '12 03:07

Tian Yong


2 Answers

The documentation for tabify mentions a suitable value for operating on line-leading whitespace only. I used it to write this function which I find handy, but you could just set it in your init file and forego a separate function:

(defun tabify-leading (start end)
  "Call `tabify' with `tabify-regexp' set so that only leading
spaces are treated."
  (interactive "r")
  (setq tabify-regexp-old tabify-regexp)
  (unwind-protect
      (progn
        (setq tabify-regexp "^\t* [ \t]+")
        (tabify start end))
    (setq tabify-regexp tabify-regexp-old)))
like image 177
spacebat Avatar answered Sep 28 '22 12:09

spacebat


Take a look at SmartTabs

It'll add onto several modes (for several languages) and make it so code indentation are tabs only, while ensuring the display of code is correct regardless of the viewer's tab width.

Excerpt:

  1. Tabs are only used at the beginning of lines. Everything else, like ASCII art and tables, should be formatted with spaces.
  2. Tabs are only used for expressing the indentation level. One tab per “block” – any remaining whitespace is spaces only.

Together with this, you can "tabify" existing code using the tabify command.

like image 23
Jon Lin Avatar answered Sep 28 '22 11:09

Jon Lin