Autohotkey : Clipboard Convert Tabs to Spaces

心已入冬 提交于 2019-12-11 06:57:08

问题


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

回答1:


; 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




回答2:


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

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)


来源:https://stackoverflow.com/questions/47165942/autohotkey-clipboard-convert-tabs-to-spaces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!