Home –  Archive
Monthly Archives: Aug 2014

Swap line with line above/below with AutoHotkey

One of the thing that I enjoy more in coding is... coding fast. This probably release in my brain a great amount of adrenaline, and makes me thing that I am fantastically good, even when my coding is actually poor 😀

One way to increase speed coding is to use keyboard shortcuts. Instead of moving your hand from the keyboard to the mouse to do trivial task such as selecting a whole line or copy and paste something, one can just use shortcuts. However, different software environments may have different shortcut, or may miss some particular shortcut combination that you use in other environment.

For example, a really nice trick in Eclipse, Notepad++ and other editor is the possibility to swap the current line to the line immediately above or below it. Unfortunately, this option is not available in some environments, for example in MATLAB. Should we just give up? Well, NO.

This is when AutoHotkey comes into play. This is a fantastic software that allows us to create script and macro to remap keystroke and action on the computer. The potential of AutoHotkey is huge, and in this post I will just show one example, coding a little script to swap the current line with the line above or below, exactly like in Eclipse or Notepad++. But, this time, the shortcut will work in every environment! 🙂

Once AutoHotkey is installed on your computer (and will not take much, since the software is also extremely light), right click on the little icon on the startbar and select edit script. Then, just copy and paste the following script:

#ifwinactive, AutoHotkey.ahk - Notepad

~^s::
reload
return

#ifwinactive

LWin & Q::
if GetKeyState("LControl","P")=1
{
Send {Home}
Send {LShift Down}
X=%A_CaretX%
Y=%A_CaretY%
Send {End}
if (A_CaretX=X and A_CaretY=Y)
{
Send {Up}
return
}
Send {LShift Up}
Send ^x
Send {Up}
Send {Home}
Sleep 10
Send ^v
Sleep 10
X=%A_CaretX%
Y=%A_CaretY%
Sleep 10
Send +{End}
if (A_CaretX=X and A_CaretY=Y)
return
else {
Send ^x
Send {Down}
Send ^v
Send {Up}
Send {Home}
return

}

}

LWin & A::
if GetKeyState("LControl","P")=1

{
Send {Home}
Send {LShift Down}
X=%A_CaretX%
Y=%A_CaretY%
Send {End}
Send {LShift Up}
Send ^x
Send {Down}
Send {Home}
Sleep 10
Send ^v
Sleep 10
X=%A_CaretX%
Y=%A_CaretY%
Send +{End}
Sleep 10
if (A_CaretX=X and A_CaretY=Y)
return
else {
Send ^x
Send {Up}
Send ^v
Send {Down}
Send {Home}
return
}
}

Press ctrl+s, which will save and reload te script. That's it! You just bound the two combinations CTRL+WIN+Q and CTRL+WIN+A to the up/down swap action.

Try it on any editor and enjoy your new shortcut!

If you want you can change the combination used if you don't like it or, for example, because they collide with another combination you use in some environment, just change line 9/10 and 51/52 with whatever combination you prefer (here a list)

Please consider that this is my very first script with AutoHotkey, so if the style is not so elegant take this into account 😛

I also use some other cool shortcuts that you might found interesting, but I will explore them in one of the next posts.  Bye!