Help refining a script to adjust leading and trailing spaces

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
ndiaz
Posts: 73
Joined: 10 Jan 2017, 17:05
Contact:

Help refining a script to adjust leading and trailing spaces

05 Jul 2019, 10:31

Hello, I have the script below, which cuts the selected text and pastes it at a new location when I use Alt+Left or Alt+Right. What I would like to add to it is the ability to properly adjust leading and trailing spaces when the text is pasted so that I don't end up with extra or missing spaces. The program I use the script in doesn't adjust spaces natively like Word does.

Code: Select all

;Move selected words left or right
!Left::
!Right::
if(!x){
    send ^x
    hotkey,Alt up,paste,on
    x:=1
}
send % "^{" . subStr(a_thisHotkey,2) . "}"
return

paste:
hotkey,Alt up,paste,off
x:=0
send ^v
return
Thank you in advance for your help!
User avatar
rommmcek
Posts: 1480
Joined: 15 Aug 2014, 15:18

Re: Help refining a script to adjust leading and trailing spaces

05 Jul 2019, 12:52

May be you should take a look here (Padding with spaces) and here (To pad an integer with zeros or spaces...).
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Help refining a script to adjust leading and trailing spaces

05 Jul 2019, 13:42

To remove leading/trailing spaces you can use Trim:
Trim() / LTrim() / RTrim() - Syntax & Usage | AutoHotkey
https://www.autohotkey.com/docs/commands/Trim.htm

Adding leading/trailing spaces, to be consistent with nearby lines, may be more complicated, because you'd need info about nearby lines.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
rommmcek
Posts: 1480
Joined: 15 Aug 2014, 15:18

Re: Help refining a script to adjust leading and trailing spaces

06 Jul 2019, 09:48

After jeeswg's post and more thorough reading try:

Code: Select all

!Left::
!Right::
Send {Left}^+{Right}
; … the rest of your code
Otherwise, you should post before and after (wrong and desired) text!

Edit: If I'm right and you are after switching whole words, you might try:

Code: Select all

Send ^{Right}^{Left}^+{Right}
instead, which doesn't require having (but won't bother) marked word. It will switch the word near caret.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: oktavimark, Rohwedder and 267 guests