reddit / emacs

setting tabs, instead of spaces and shortening the tab lengths.

igoryon · 2022-09-21 00:04
I am just learning emacs, but learn it to the point, that I would like to start using it over other editors, that I've been using so far, but I can't figure out,: 1) how to make emacs to use tabs, instead of spaces. When it indents lines, it puts spaces, but I need to make it to insert tabs instead. 2) Also, how can I make tabs to be of 2 space value, instead of the default 4 or 8, I am not sure?
7890yuiop · 2022-09-21 00:54
You need a few things. 1. Firstly, know that this is likely a bit more complicated than you might have expected (but there are reasons for that). 2. `indent-tabs-mode` must not be `nil` for the buffers in question. By *default* this is already true (the default value is `t`), so you should take note that something in your own config is already overriding this -- you should investigate that (I suggest you `M-x rgrep` your config). This variable has buffer-local values which are often set via a mode hook, but you *probably* just need to find where your config is clobbering the default and either prevent that from happening (preferably), or re-clobber it later on. 3. `tab-width` must be set to the desired value 2. This variable also has buffer-local values. The default value is 8. 4. This is where it gets slightly fiddly.... The Emacs approach is to know how to *auto-indent*^1, and this means that you will also need to configure the modes you're using, and how you do that may vary arbitrarily between modes (which are free to implement their indentation rules however they wish); so there is no single place to configure this. In practice there are common patterns to look for, but expect some outliers as well. You should do three things: a) Use `M-x customize-group` to peruse the user options for each feature you're interested in. Its indentation controls will almost invariably be exposed through that UI -- however in some cases (derived modes, especially) some of the options may be inherited from (and therefore configured via) some other customize group. b) Use `M-x apropos-variable RET indent\|offset` and look for relevant variables. Maybe start with `indent-level\|basic-offset` which is almost guaranteed to be a list of highly-relevant variables, but the more general search may turn up other things as well (amongst lots of false-positives). Note that some C-like language modes will defer to `c-basic-offset`. c) Check the documentation. For instance, the cc-mode manual `C-h i g (ccmode)` has entire sections on indentation, which are relevant to that family of major modes. Many libraries will document things in the Commentary section at the top of the elisp file. Some may have their own readme file. Others may have their own info manual, or be covered in the standard manual. 5. Understand that a consequence of wanting to use tabs *exclusively* is that you cannot allow a mode to ever indent something by anything other than an exact multiple of your `tab-width` value, because otherwise Emacs is forced to use spaces to make up the final amount. In your case, any indentation to a column that isn't a multiple of 2 would end with a single space. For modes which indent using alignment to any arbitrary column (which is the case for elisp code, for example) you essentially *cannot* have tab-only indentation (unless `tab-width` is 1, of course). For (1) and (2) setting the default value for the user option (e.g. via `M-x customize-option`) *should* cover many cases; but it's entirely possible for something to be setting the values buffer-locally for particular modes, at which point you need to address that (possibly with your own mode hook function). ---- ^1 The benefit to these complications is, of course, that (once configured correctly) you never have to manually adjust indentation for anything at all. You can simply ask Emacs to reindent a region after any edit, and know that it's going to do what you wanted.
igoryon · 2022-09-21 07:43
Thank you, i think, it did it. I only tested it on perl files yet and in perl files it works. Also, you've helped me with the config on how to find options and set them.