Sublime Forum

Any way to replace spaces with tabs when pasting in code?

#1

Lots of times I need to paste code from other documents or web pages. They are usually indented with spaces, although in my documents I use tabs – so this code is sloppy looking.

Is there any built-in way or extension to clean this up?

I often resort to find/replace " {4}" / “\t” but it’s a pain and doesn’t do anything for 3 or 2 space indents.

1 Like

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

Why don’t you use " +" for detecting ‘more than 1 instance’?

1 Like

#3

You can use Convert Indentation to Tabs after pasting the code in.

1 Like

#4

Because this would replace eight spaces with one tab instead of two tabs.

1 Like

#5

Try s/(?<=\s) {2,4}/\t/g (will also match two or more spaces within the line)
or s/^(\s+) {2,4}/\1\t/g (requires you to reapply for every indent level because they are consumed).

Lookbehinds are just not powerful enough. :confused: “(?<=^\s*) {2,4}” would be too easy I guess.

1 Like