Skip to content

Instantly share code, notes, and snippets.

@davebrny
Last active March 3, 2024 11:50
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save davebrny/088c48d6678617876b34f53571e92ee6 to your computer and use it in GitHub Desktop.
Save davebrny/088c48d6678617876b34f53571e92ee6 to your computer and use it in GitHub Desktop.
๐Ÿ“‡ (autohotkey) - wrap selected text in *symbols*
/*
[script info]
version = 2.5
description = wrap selected text in %symbols%
author = davebrny
source = https://gist.github.com/davebrny/088c48d6678617876b34f53571e92ee6
*/
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
@davebrny
Copy link
Author

text wrap

wrap selected text in <symbols>

ย 

hotkey:

  • select a word or sentence
  • press alt + 2
  • e.g. selected text will change to "selected text"

ย 

hotstring:

  • copy a word or sentence to the clipboard first
  • place the cursor where you want the text to be pasted
  • type w then 2
  • then spacebar or enter to send the hotstring

ย 

hotkey hotstring wrapped
alt + 2 w2 "text"
alt + ' w' 'text' monospace text (MD)
alt + ` w` `text` literal 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) / bold text (YT)
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 deletes up to 2 characters on either side

MD - markdown, YT - YouTube

ย 

  • if no text is selected when using the hotkey then the symbols will be pasted and the cursor will be moved between them

  • alt + del deletes up to two wrap characters if they are on either side: %text% to text

@jaydeepkarena
Copy link

Super useful.
I will start using in Visual Studio from NOW.

Thanks a lot.

@davebrny
Copy link
Author

davebrny commented Oct 2, 2018

@jaydeepkarena youre welcome!

@Adrianrff
Copy link

This is fantastic, thank you!

I was wondering what should I modify in this script in order to trim leading and trailing spaces from the selected text, and then wrap the text around the desired symbols. I try to figure it out my self, but i'm still green in the AHK world.

Thanks in advance.

@Adrianrff
Copy link

I'm also noticing that, in some programs (such as memoQ) the key combination inserts the clipboard instead of wrapping the text. This is apparently random; sometimes it wraps the text and sometimes it pastes the clipboard.

@davebrny
Copy link
Author

davebrny commented Jun 17, 2019

sorry @adrianff i dont remember getting a notification about this comment. have you figured it out these problems since?
the trailing spaces can be fixed easily be using the trim() function:

string := " abc "
new_string := trim(string)
msgbox, >%string%< `n>%new_string%<

im not sure about the other problem. maybe the sleep could be could be changed to 100 or 200... it might be reverting the clipboard back to its previous state before the paste command has time to send it

@davebrny
Copy link
Author

@jaydeepkarena @adrianff
ive rewritten this script from scratch to make it easier to add and manage different types of wraps. the new script also uses a two stage hotkey so you dont have to worry about conflicts with other hotkeys as much:
https://gist.github.com/davebrny/cd98cb10d4c944b065fda67b707cfd0b

(i didnt add support for hotstrings into the new script though which is why im leaving this script here in case anyone still wants that feature)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment