Clipboard Convert Tabs to Spaces

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Kagerjay
Posts: 9
Joined: 04 Nov 2017, 08:57
Contact:

Clipboard Convert Tabs to Spaces

07 Nov 2017, 13:47

I have a snippet of code I copied into my clipboard. Pasting it out looks like this. Where[tab] is an actual tab indent

Code: Select all

	
[tab]<header id="masthead" class="site-header">
[tab][tab]<h1>
[tab][tab][tab]<h2>
[tab][tab][tab][tab]<h3>
[tab][tab][tab][tab][tab]<h4>;
I want to press an autohotkey to automatically normalize the code snippet. So if there's a [tab] on every line, remove it.
Then convert each [tab] into 2 spaces [**]

Code: Select all

	
<header id="masthead" class="site-header">
**<h1>
****<h2>
******<h3>
********<h4>;
So the general workflow is:

1. Copy code to clipboard
2. Press an autohotkey
3. Paste the newly formatted contents

Pseudocode would look like

- Dig through every clipboard content line by line
- If every item shares an equal number of [tab] spaces, remove them entirely
- Line by line, convert [tab] to [**] 2 spaces
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Clipboard Convert Tabs to Spaces

07 Nov 2017, 21:40

Look at StrReplace(). The tab character would be detected with `t. See escape characters.

Copy and paste is done using the Send command.

You can do line by line, or just replace the entirety of the string at once. If you want to go line by line, you'd do something involving Loop, Parse.
Kagerjay
Posts: 9
Joined: 04 Nov 2017, 08:57
Contact:

Re: Clipboard Convert Tabs to Spaces

08 Nov 2017, 09:55

I have a working solution now :)

Press f9 to trigger clipboard changes

Code: Select all

F9::
clipboard := StrReplace(clipboard, A_Tab, A_Space A_Space)
position := RegExMatch(clipboard, "\S")
test := RegExReplace(clipboard, "m)^[ ]{" . position - 1 . "}")
clipboard := test

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], niCode and 93 guests