Home Download Buy Blog Forum Support

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

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

Postby handycam on Sun May 05, 2013 11:00 pm

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.
Mac OS 10.9.3
ST 3 (3059)
handycam
 
Posts: 271
Joined: Tue Jan 03, 2012 2:42 pm

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

Postby sadbuttrue on Tue May 07, 2013 7:27 am

Why don't you use " +" for detecting 'more than 1 instance'?
sadbuttrue
 
Posts: 2
Joined: Tue May 07, 2013 7:25 am

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

Postby jps on Tue May 07, 2013 7:40 am

You can use Convert Indentation to Tabs after pasting the code in.
jps
Site Admin
 
Posts: 3077
Joined: Wed Mar 19, 2008 12:33 pm

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

Postby adzenith on Tue May 07, 2013 3:44 pm

sadbuttrue wrote:Why don't you use " +" for detecting 'more than 1 instance'?

Because this would replace eight spaces with one tab instead of two tabs.
adzenith
 
Posts: 1217
Joined: Mon Oct 19, 2009 9:12 pm

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

Postby FichteFoll on Mon May 13, 2013 11:32 am

Try
Code: Select all
s/(?<=\s) {2,4}/\t/g
(will also match two or more spaces within the line)
or
Code: Select all
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. :/ "(?<=^\s*) {2,4}" would be too easy I guess.
FichteFoll
 
Posts: 407
Joined: Fri Mar 16, 2012 11:49 pm
Location: Germany


Return to General Discussion

Who is online

Users browsing this forum: Google [Bot] and 31 guests