Autohotkey : Clipboard Convert Tabs to Spaces

2019-08-02 21:15发布

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

[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 [**]

<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 for autohotkey would look like this

  • 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

2条回答
何必那么认真
2楼-- · 2019-08-02 21:38
; convert each tab into 2 spaces:

clipboard =
(
    <header id="masthead" class="site-header">
        <h1>
            <h2>
                <h3>
                    <h4>;
)
clipboard := StrReplace(clipboard, A_Tab, A_Space A_Space)

https://autohotkey.com/docs/commands/StringReplace.htm

查看更多
聊天终结者
3楼-- · 2019-08-02 21:38

okay problem solved I have everything I want now. I took the other source code snippet here on autohotkey forum

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

As a reference, need to highlight the full line on code snippet like so

enter image description here

So all I have to do now is:

  1. Copy the full code
  2. Press F9
  3. Paste into my notetaking app (dynalist.io with codesnippet support)
查看更多
登录 后发表回答