Fun with AHK

@brimwats also provided 2 AHK scripts in Obsidian discord
https://discord.com/channels/686053708261228577/694233507500916796/798399078048202772

1st one is to remove PDF formatting (remove new line, remove double space,…)

; --------------Paste w/o formatting------------


^+v::
; Trim leading/trailing white space from empty lines 
Clipboard:=RegExReplace(Clipboard,"m)^[ \t]*$","`r`n")

; copied from http://ahkscript.org/docs/commands/StringReplace.htm
; Remove all blank lines from the text in a variable:
Loop
 {
   StringReplace, ClipBoard, ClipBoard, `r`n`r`n, --[ahkparagraphmarker]--, UseErrorLevel
   if ErrorLevel = 0  ; No more replacements needed.
     break
 }

;Replace all new lines with a space topreventjoinedwords 
StringReplace, ClipBoard, ClipBoard, `r`n, %A_Space%, All

; Remove all double spaces (useful for justified text)
Loop
 {
    StringReplace, ClipBoard, ClipBoard, %A_Space%%A_Space%, %A_Space%, UseErrorLevel
    if ErrorLevel = 0  ; No more replacements needed.
        break
 }

; re-create paragraphs again
StringReplace, ClipBoard, ClipBoard,--[ahkparagraphmarker]--,`r`n`r`n, All

; remove any leftover remaining leading spaces
Clipboard:=RegExReplace(Clipboard,"m)^[ \t]*")

Send ^v
Return

The 2nd script is auto formatting for highlighted text portion

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
/*
[script info]
version     = 2.5
description = wrap selected text in %symbols%
author      = davebrny
source      = https://gist.github.com/davebrny/088c48d6678617876b34f53571e92ee6

| hotkey          | hotstring |      result     |           notes          |
|-----------------|-----------|:---------------:|:------------------------:|
| alt + 2         | w2        | "text"          |                          |
| alt + '         | w'        | 'text'          | monospace text (MD)      |
| alt + `         | w`        | `text`          | in-line code block (MD)  |
| alt + 9         | w9        | (text)          |                          |
| alt + [         | w[        | [text]          |                          |
| alt + ]         | w]        | {text}          |                          |
| alt + ,         | w,        | <text>          |                          |
| alt + 5         | w5        | %text%          |                          |
| alt + .         | w.        | >>text<<        |                          |
| alt + 8         | w8        | *text*          | italic text (MD)         |
| alt + shift + 8 | w*        | **text**        | bold text (MD)           |
| alt + shift + # | w~        | ~~text~~        | strike through text (MD) |
| alt + -         | w-        | -text-          | strike through text (YT) |
| alt + shift + - | w_        | _text_          | italic text (YT)         |
| alt + k         | wkbd      | <kbd>text</kbd> | markdown key syntax      |
| alt + del       | wdel      | text            | del 2char either side    |
*/

sendMode input
return ; end of auto-execute
;---------------------------


!2::
::w2::
goTo, wrap_quote        ;   "text"

!'::
::w'::
goTo, wrap_apostrophe   ;   'text'

!`::
::w``::
goTo, wrap_grave        ;   `text`

!9::
::w9::
goTo, wrap_parenthesis  ;   (text)

![::
::w[[::
goTo, wrap_bracket      ;   [[text]]

!]::
::w]::
goTo, wrap_brace        ;   {text}

!,::
::w,::
goTo, wrap_angle        ;   <text>

!5::
::w5::
goTo, wrap_percent      ;   %text%

!.::
::w.::
goTo, wrap_2_angle      ;  >>text<<

!8::
::w8::
goTo, wrap_asterisk     ;   *text*

+!8::
::w*::
goTo, wrap_2_asterisk   ;  **text**

+!#::
::w~::
goTo, wrap_tilde        ;  ~~text~~

!-::
::w-::
goTo, wrap_hyphen       ;   -text-

+!-::
::w_::
goTo, wrap_underscore   ;   _text_

!k::
::wkbd::
goTo, wrap_kbd          ;   <kbd>text</kbd>

!del::
::wdel::
goTo, wrap_delete       ;   _text_  --->  text




;-----------------------




wrap_quote:
wrap_apostrophe:
wrap_grave:
wrap_parenthesis:
wrap_bracket:
wrap_brace:
wrap_angle:
wrap_percent:
wrap_2_angle:
wrap_asterisk:
wrap_2_asterisk:
wrap_tilde:
wrap_hyphen:
wrap_underscore:
wrap_kbd:
this_label := a_thisLabel
clipboard_text := get_clipboard()
for what, with in { "wrap_quote"       : """" clipboard_text """"
                  , "wrap_apostrophe"  :  "'" clipboard_text "'"
                  , "wrap_grave"       : "``" clipboard_text "``"
                  , "wrap_parenthesis" :  "(" clipboard_text ")"
                  , "wrap_bracket"     :  "[[" clipboard_text "]]"
                  , "wrap_brace"       :  "{" clipboard_text "}"
                  , "wrap_angle"       :  "<" clipboard_text ">"
                  , "wrap_percent"     :  "%" clipboard_text "%"
                  , "wrap_2_angle"     : ">>" clipboard_text "<<"
                  , "wrap_asterisk"    :  "*" clipboard_text "*"
                  , "wrap_2_asterisk"  : "**" clipboard_text "**"
                  , "wrap_tilde"       : "~~" clipboard_text "~~"
                  , "wrap_hyphen"      :  "-" clipboard_text "-"
                  , "wrap_underscore"  :  "_" clipboard_text "_"
                  , "wrap_kbd"         :  "<kbd>" clipboard_text "</kbd>" }
    stringReplace, this_label, this_label, % what, % with, all
new_text := this_label
goSub, send_wrap
return



wrap_delete:
clipboard_text := get_clipboard()
loop, 2
    {
    stringLeft, left_character, clipboard_text, 1
    stringRight, right_character, clipboard_text, 1
    if regExMatch(left_character, "[\Q'%*-_""~``([{><\E]")
        and if regExMatch(right_character, "[\Q'%*-_""~)``]}><\E]")  ; if  '%*-_"~`([{
        {
        stringTrimLeft, clipboard_text, clipboard_text, 1
        stringTrimRight, clipboard_text, clipboard_text, 1
        }
    else break
    }
new_text := clipboard_text
goSub, send_wrap
return



get_clipboard(){
    global
    if !inStr(a_thisHotkey, ":")    ; if hotkey was used
        {
        revert_clipboard := clipboardAll
        clipboard =
        send ^{c}
        clipWait, 0.3
        if clipboard is space
            clipboard =
        }

    return clipboard
}



send_wrap:
if !inStr(a_thisHotkey, ":") and if (clipboard = "")       ; if hotkey was used
     position := "{Left " round( strLen(new_text) / 2) "}" ; move cursor between symbols
else position := ""
clipboard := new_text
send % "^{v}" . position
sleep 150
clipboard := revert_clipboard
return
3 Likes