Sublime Forum

How to replace spaces with tabs when pasting on a view with translate_tabs_to_spaces set to false?

#1

The problem is that I will certainly not notice when I paste something indented with spaces instead of tabs. This is problem because for some file types as .sublime-settings files (or a Makefile), which has the setting translate_tabs_to_spaces set to false, so I would expect to all .sublime-settings files to be indented with tabs, not spaces.

The setting translate_tabs_to_spaces set to false works fine until I paste something on a setting’s files which is indented with spaces, instead of tabs. This is a problem because as I am over git versioning, I can easily create files with mixed tabs and spaces on the history, and some day later Sublime Text will fix the indentation to tabs, which will cause noise on the git history due the tab/space conversion war.

I think this can have a performance problem as when I am pasting something big on Sublime Text. Then to perform the conversion on the would not be easily possible and it should be performed afterwards by the user. Now Sublime Text should warn the user when he is pasting something indented with spaces instead of tabs in a file which is expected to be indented with tabs?

The detection of whether the contents of the clipboard should not be expensive as we should just check some lines (which would not cover the cases where there is already mixed indentation on the clipboard contents). But there would be a performance problem when pasting something with very big first lines. On this case a threshold should stop Sublime Text from looking forward and ceases the detection as inconclusive for this paste and just paste like it is.

Related threads:

  1. #9969 Any way to replace spaces with tabs when pasting in code?
  2. #14140 Tab to space behavior on copy-paste
0 Likes

#2

Tried the status bar button on the bottom right corner? Change to spaces and then back to tabs, it should convert them

0 Likes

#3

I don’t understand, why would you not have translate_tabs_to_spaces set to true for YAML files, when tab characters are invalid?

0 Likes

#4

Sorry, I mistake the name sublime-syntax with sublime-settings. It actually seems that some times Sublime Tex converts them, but other times don’t.

It works if I know Sublime Text pasted spaces instead of tabs. But I cannot always be checking whether the contents are tabs instead of spaces manually. It is too tedious work. Sublime Text should just convert them automatically.

0 Likes

#5

Then maybe this:
https://packagecontrol.io/packages/Expand%20Tabs%20on%20Save
On Package Control you usually find something for this kind of things, better look there first.

0 Likes

#6

Thanks, I had searched there and on the internet, but I was looking for conversion to tabs on paste, not on save. This seems better as if will fix my current file if it is already using the wrong indentation. But I am not sure about the performance impact of calling expand_tabs or unexpand_tabs every time I save any file, independent of its size. Perhaps this is not a good ideia, and I should look for writing a package which does the conversion only for the pasted contents which are indented on spaces, but the current file is indented on tabs. Therefore, also not perform the conversion if the contents are bigger then a threshold. On this case, just warm the user the pasted contents are different from the current file indentation type. Then the user call the conversion by himself, or not.

0 Likes

#7

I don’t use that package myself, and converting on paste would be definitely better. Anyway are you sure translate_tabs_to_spaces doesn’t always work? Only problem I ever had is if indentation size is different with same indentation size. Also you can check this

https://packagecontrol.io/packages/Stupid%20Indent

0 Likes