Specifying Tab-Width

Specifying Tab-Width?

I believe this blog post should help you out:

Here's a solution, it's not neat since it has to be done for every instance of a tab, but it makes the tabs take up less space and preserves the formatting for copying out of the browser (obviously replace "A SINGLE TAB HERE" with a real tab, this blog software automatically removes tabs from entries it seems):

<span style="display:none">A SINGLE TAB HERE</span><span style="margin-left:YOUR NEW TAB WIDTH"></span>

Basically, replace every instance of a tab in your code with that code snippet (after choosing a suitable width, you could do it in a stylesheet pretty easily). The code artificially inserts the margin whilst keeping the original tab in the code ready for copy/pasting.

Incidentally, it looks like tab stops made it into the CSS specification.

There's also another Stack Overflow question on this subject.

Tab width CSS property

tab-size is currently only implemented by Firefox and Opera using your given vendor prefixes.

For WebKit, there's a bug report requesting that the property be implemented. I believe work has already started on it, as can be seen in the comments on that report.

For IE, well, I can't find anything about an -ms-tab-size or tab-size implementation in IE9 or IE10. I suppose the IE team has been none the wiser.

How can I customize the tab-to-space conversion factor?

By default, Visual Studio Code will try to guess your indentation options depending on the file you open.

You can turn off indentation guessing via "editor.detectIndentation": false.

You can customize this easily via these three settings for Windows in menu FilePreferencesUser Settings and for Mac in menu CodePreferencesSettings or ⌘,:

// The number of spaces a tab is equal to. This setting is overridden
// based on the file contents when `editor.detectIndentation` is true.
"editor.tabSize": 4,

// Insert spaces when pressing Tab. This setting is overriden
// based on the file contents when `editor.detectIndentation` is true.
"editor.insertSpaces": true,

// When opening a file, `editor.tabSize` and `editor.insertSpaces`
// will be detected based on the file contents. Set to false to keep
// the values you've explicitly set, above.
"editor.detectIndentation": false

Atom - Force Tab Width 2

There is more than one tab setting

Each package (such as python-language) has its own tab setting(s). Whether the language uses the global default or its own default is up to whoever created the package, but you can generally override it.

In your screenshot, you have set the "Tab Type" to "soft". That will take care of using spaces rather than tabs. You have left the default tab width of 2. That is your global setting.

Now, if you look under "Packages" and search for "python" you will find a package named "language-python". Click on its settings button and you will find a number of syntax-specific settings.

  • Python Grammar
  • Python Console Grammar
  • Python Traceback Grammar
  • Regular Expressions (Python) Grammar

Each of those grammars has its own Tab Length setting. You can set them explicitly to 2 here to override the package's default. (You probably mostly care about the first one, Python Grammar.)

Python is different

In the case of Python, the package is explicitly configured to default to 4 spaces, probably because Python is very opinionated about whitespace, and PEP 8 recommends 4-space indents. You can see the default package setting here in the package's source:

https://github.com/atom/language-python/blob/master/settings/language-python.cson

'autoIndentOnPaste': false
'softTabs': true
'tabLength': 4

This overrides the global default. That's why Python Grammar does not honor the global tab width, the way that most packages do.

Sometimes there are package overrides

Additionally, certain packages will override your settings for syntax reasons. For example, language-make will override and use real tabs instead of spaces, because that is required by make.

In the case of Python, there is an override to use spaces. The language-python settings page offers a spot for you to change the indentation level, but it does not offer a way to switch to using tab characters. (That's probably justifiable, as tab characters and mixed indentation in Python are a very common cause of difficult-to-debug syntax errors.)

You might need to reload

Lastly, sometimes settings don't take effect completely until you reload the Atom window. You can use the Window: Reload command to do so. Or using the keyboard:

  • Mac: CtrlOptCmdL
  • Windows/Linux: CtrlAltR

How to change tab width in git diff?

This actually has nothing to do with git diff.

git diff actually renders a tab, which is later converted by your terminal emulators (for instance, gnome-terminal) to spaces.

Go to the preference of your terminal emulator to change that setting.


Also, git may use a pager, so you might want to configure it like that:

git config --global core.pager 'less -x1,5'

More information here: setting tabwidth to 4 in git show / git diff

How do I change tab size in Vim?

Expanding on zoul's answer:

If you want to setup Vim to use specific settings when editing a particular filetype, you'll want to use autocommands:

autocmd Filetype css setlocal tabstop=4

This will make it so that tabs are displayed as 4 spaces. Setting expandtab will cause Vim to actually insert spaces (the number of them being controlled by tabstop) when you press tab; you might want to use softtabstop to make backspace work properly (that is, reduce indentation when that's what would happen should tabs be used, rather than always delete one char at a time).

To make a fully educated decision as to how to set things up, you'll need to read Vim docs on tabstop, shiftwidth, softtabstop and expandtab. The most interesting bit is found under expandtab (:help 'expandtab):

There are four main ways to use tabs in Vim:

  1. Always keep 'tabstop' at 8, set 'softtabstop' and 'shiftwidth' to 4 (or 3 or whatever you prefer) and use 'noexpandtab'. Then Vim will use a mix of tabs and spaces, but typing and will behave like a tab appears every 4 (or 3) characters.

  2. Set 'tabstop' and 'shiftwidth' to whatever you prefer and use 'expandtab'. This way you will always insert spaces. The formatting will never be messed up when 'tabstop' is changed.

  3. Set 'tabstop' and 'shiftwidth' to whatever you prefer and use a |modeline| to set these values when editing the file again. Only works when using Vim to edit the file.

  4. Always set 'tabstop' and 'shiftwidth' to the same value, and 'noexpandtab'. This should then work (for initial indents only) for any tabstop setting that people use. It might be nice to have tabs after the first non-blank inserted as spaces if you do this though. Otherwise aligned comments will be wrong when 'tabstop' is changed.

How can I set the tab width in a monaco editor instance?

The answer has just been discussed in a corresponding GitHub issue. The trick is not to update the options on the editor directly, but on the underlying model. To extend the snipped above:

const editor = monaco.editor.create(
document.getElementById("editor"), {
language: "html",
value: "<p>Hello World!</p>",
});

editor.getModel().updateOptions({ tabSize: 2 })

This works for me (™) in the Monaco Playground.

All credit for this goes to the monaco devs — I absolutely love their editor, and this improves it even further.

How to set the tab size of terminal (without tabs or expand)

Rather than use control sequences which require both row and column to be specified, your terminal probably supports one which moves within the current row. Referring to XTerm Control Sequences:

CSI Pm `  Character Position Absolute  [column] (default = [row,1])
(HPA).

which would be

printf '\033[%d`' $col

in the shell. Also, your text may overwrite previously-written data after the text. You can clear that with another control sequence:

CSI ? Ps K
Erase in Line (DECSEL).
Ps = 0 -> Selective Erase to Right (default).
Ps = 1 -> Selective Erase to Left.
Ps = 2 -> Selective Erase All.

sed is a little awkward for producing escape characters. Here is a suggestion in awk:

TEXT="line1
line2
line3"
echo "$TEXT" | awk '{ printf "\033[3`%s\033[K\n", $0; }'

How to change Chrome's tab indention width?

I'm afraid support for the aforementioned solution's been dropped as of Chrome 33. See the following post: http://www.reddit.com/r/chrome/comments/1ymfgw/

After some cursory snooping through angry discussion lists, I concluded there was no way of customising the view-source page's formatting short of writing a full-blown extension. Which, of course, is overkill for something as menial as tweaking indentation width.

As a (hopefully temporary) fix, I've simply added the following bookmarklet to my bookmarks bar, and habitually run it whenever I find myself developing vertigo poking through hideously misaligned source code:

javascript:(function(){document.body.style.tabSize = 4;}());

Just change "tabSize = 4;" to whatever your desired indentation width is, and voilà! ;) Sure as heck not the most elegant or ergonomic approach, but certainly the quickest and easiest.


EDIT: Here's a variant of the bookmarklet that works globally (rather than limiting itself to Chrome's "view-source" view):

javascript:(function(){for(var i=0,l=document.all.length;i<l;++i)document.all[i].style.tabSize=4;}());

I've moved the bookmarklet to my main bookmarks bar and assigned it a neat, conservative name so I can hit it repeatedly while browsing GitHub. :) Who, mind you, could really do with implementing a "Tab Size" setting for repositories - so developers can tailor the reader's viewing experience based on their coding habits.



Related Topics



Leave a reply



Submit