Sublime Forum

Please add a real indent with spaces mode

#1

translate_tabs_to_spaces is the devil.

It’s enabled when you press Indent with spaces, and the description in the preferences file says: Set to true to insert spaces when tab is pressed

But that’s not what it does is it? Instead it’s a naive hack that turns any and all tabs into spaces, indentation or otherwise, whether the tab key is involved or not.

The keybind that lets you specifically input tabs with shift+tab is broken. It inputs spaces instead.

Ever try to work on a table or spreadsheet in a text editor? I have an easier time of it in notepad than in sublime, because sublime nukes all the tabs.

A text editor who’s indentation settings mangle pastes. WTF?

While this option has it’s fans, it is definitely not what you expect when you click “Indent with spaces”. Indent with spaces should (As the name suggests) add spaces when indenting, instead of tabs.

There are also issues with the tab stops and reindent on lines with preexisting tabs, but translate_tabs_to_spaces makes them worse.

Please add a real indent with spaces mode

1 Like

Error while copy and paste lines with tabs
#2

Not really sure that there’s any problem here except that Sublime’s way of doing things may not fit your workflow.

But that’s not what it does is it? Instead it’s a naive hack that turns any and all tabs into spaces, indentation or otherwise, whether the tab key is involved or not.

It doesn’t convert anything; it only affects new indents. When ‘Indent using Spaces’ is selected enabled. Sublime will indent and outdent spaces to tabstop positions. If text is pasted with leftmost tab indents, the leftmost tabs will be converted to spaces of the current buffer’s tabstop size. From a programming perspective, I like this behaviour because it keeps leftmost indents consistent as tabs or spaces. But I can see that it might cause issues pasting other data where you want to preserve leftmost tabs/spaces.

Apart from pasting, no other conversion happens when switching between these modes; it just sets the indentation mode for new indents in the file (with 1 caveat, see below). That seems perfectly logical. So you can switch mode in a file and it won’t touch what’s already there. The only wrinkle being Sublime’s “intelligence” in terms of trying to match the indentation of any previous code. So if you switch from ‘Indent using spaces’ to ‘Indent using tabs’ Sublime will continue to use spaces if any unclosed indented block above is indented with spaces. This seems counter-intuitive but makes sense in practise; mixing tabs and spaces for indenting has never ended well for me in the past. A workaround to avoid this is having a few characters at column position 0 on the line above the cursor.

I prefer to work with all spaces or all tabs for indenting, depending on the file type. The ‘convert indentation to spaces/tabs’ will convert all indents based on tabstop size and are intelligent enough to only convert leftmost whitespace (ie. indents) and no other whitespace in the file.

shift+tab behaviour tries to follow the indent mode, but I can see how it’s confusing since the keybindings would suggest otherwise.

I don’t really understand the last comment, since Sublime does have a ‘real indent with spaces’ mode.

0 Likes

#3

This is incorrect. If you take a line of text with no leading whitespace and a tab in the middle of it, it will convert the tab into spaces. Whether you paste, duplicate_line… In fact as far as I’ve tested there is no way to add a tab to a buffer at all as long as translate_tabs_to_spaces is enabled.

If text is pasted with leftmost tab indents […] From a programming perspective, I like this behaviour

Agreed, but this behaviour would be better confined to the paste_and_indent command

I prefer to work with all spaces or all tabs for indenting, depending on the file type.

So do I. If only my co-workers felt the same :slight_smile:

The good news is that I think I’ve found a way to fix this behaviour through a plugin, I should have it done today

1 Like

#4

Just thought I’d add a link to Questions about settings, where the shift+tab keybinding regex is highlighted

0 Likes

#5

I can see that having text like hello<tab>world and then using Convert Indentation to Spaces from the status bar converts the tab to spaces, which is indeed not indentation, and thus shouldn’t… this seems to be caused by the command: expand_tabs {"set_translate_tabs": true} from Packages/Default/indentation.py. although as it does it when pasting as well, maybe it’s not related and is a core ST bug? probably worth logging it at https://github.com/SublimeTextIssues/Core/issues

0 Likes

#6

Didn’t know there was an issue tracker. Nice, I’ll do that

0 Likes

#7

You are correct; in truth I hadn’t noticed that before. But what I meant by “it doesn’t convert anything” was that it doesn’t convert anything already in the buffer. Sorry for the confusion.

Again, speaking from a programming perspective, I don’t see this as a bad thing per se. If I paste something that’s been tabbed out on a line (typically line based comments in the case of programming) into an ‘indent using spaces’ mode buffer, I would want the tabs replaced by spaces. But I get the point, it’s not ideal in some use cases. Having such intelligence configurable would definitely be preferable.

0 Likes

#8

Made a plugin to fix it: https://packagecontrol.io/packages/TabNukerNuker

1 Like

#9

I love it when people write a plugin for changing the ST behavior instead of writing for long hours how things should be !

Kuddos JnvSor!

3 Likes

#10

I considered a plugin before writing this post and wrote it off as not doable. Turns out it is as long as you don’t make heavy use of macros with tabs (But it’s still a hack and ST should really fix it)

0 Likes

#11

You might want to know about view.settings().erase, which removes view-specific settings and makes it fall back to other sources, such as the user preferences file.

The downside is that there is currently no definitive way of knowing where a setting’s value is sourced from, so you might end up removing an actual view-specific setting in the process.

Just putting this out here. Your way is imo the better one in this situation.

0 Likes