Sublime Forum

Tabs to spaces is making me crazy!

#1

I wan’t Sublime Text 3 to use spaces for indentation, but I don’t want it to convert the tabs in pasted text to spaces automatically — I want it to leave my pasted text verbatim (spaces left as spaces, tabs left as tabs — especially internal tabs that have nothing to do with indentation).

Is it possible to indent with spaces while still leaving pasted text unmolested? Having to remember to disable “indent using spaces” every time I paste a table is becoming a real pain.

0 Likes

#2

The way things currently work tie together tab to space conversion and space indentation, ie. when you press tab it inserts a tab anyway, but that gets converted to spaces. So there’s not really a built-in way to achieve what you’re looking for.

I suggest leaving a feature request on our issue tracker: https://github.com/sublimehq/sublime_text/issues

0 Likes

#3

If the goal is to allow pasted tabs even when tab translation is turned on, but otherwise use spaces, simplistically you can achieve a goal like that with something like the following plugin and key combination that overrides the paste key binding when translate_tabs_to_spaces is turned on, allowing it to inject tabs directly.

import sublime
import sublime_plugin

# or super+v if you're on MacOS
# { "keys": ["ctrl+v"], "command": "tab_paste",
#    "context": [
#      { "key": "setting.translate_tabs_to_spaces", "operator": "equal", "operand": true },
#    ]
# },

class TabPasteCommand(sublime_plugin.TextCommand):
    """
    Do a paste, turning off tab to space translation temporarily.
    """
    def run(self, edit):
        self.view.settings().set("translate_tabs_to_spaces", False)
        self.view.run_command("paste")
        self.view.settings().set("translate_tabs_to_spaces", True)
4 Likes

#4

That look’s pretty handy as I’m about to get into makefiles, and for those (if I’ve understood correctly) need tabs.
Apparently coders who use spaces (however) make more money:
Or so these folks say
I agree with the OP that not having the option to do both is Unuseful.
Would starting up an OdatNurd fan club be weird ?

0 Likes

#5

Probably worth mentioning that settings like tab_size and translate_tabs_to_spaces are settings that can be made syntax specific, and in Sublime, Makefiles/Makefile.sublime-settings looks like this:

{
	"translate_tabs_to_spaces": false
}

So regardless of the setting in general, tabs are always used in a Makefile (the name of the settings file comes from the name of the Syntax, not the name of the file you’re editing).

1 Like

#6

Thank you very much for the plugin @OdatNurd — not only that solved that problem in particular, but also I finally learned to write a plugin in ST3!

Turns out, there’s already an issue on the tracker @bschaaf… from 2016: #1349.

0 Likes