DEV Community

Cover image for Use HotStrings in your Dev Life to increase your daily Productivity with AHK
Radu-Alexandru B
Radu-Alexandru B

Posted on • Edited on

Use HotStrings in your Dev Life to increase your daily Productivity with AHK

Cover Photo by Александар Цветановић on Pexels

Contents

What are AHK's HotStrings?

HotStrings in AutoHotKey "shortcuts" that are mainly used to expand (or auto-replace) abbreviations as you type them:

Using hotstrings abbreviation

Hotstrings can be also used to launch any scripted actions (like the macros used in the previous posts).


Hotstrings we are going to create

Let's see what Hotstrings we are going to create today:

  • Some basic HTML tags for anchors, lists
  • Autocomplete syntaxes for print, loops, conditionals, functions in your favorite programming language
  • Dates in any format that we want (eg. dddd, MMMM dd, yyyy, HH:mm)

Hotstrings types

Before we jump in to create some HotStrings, let's see a few differences between the two types of HotStrings:

  • Hotstrings that are triggered after pressing an ending character (eg Space, ., or Enter)
::forloop::for (let i = 0; i < arr.length; i++) {
Enter fullscreen mode Exit fullscreen mode
  • Hotstrings that are triggered right after you wrote them (they contain an * - asterisk)
:*:printc::printf('%d\n', num);

:*:<a>::<a href="" title=""></a>
Enter fullscreen mode Exit fullscreen mode

Okay, why is this important?
Well, if we are going to use only hotstrings that are triggered right after writing them (*), we have to make sure don't use keywords containing other used keywords inside them:

:*:printMe::console.log();
:*:printMeToo::System.out.println(); 
Enter fullscreen mode Exit fullscreen mode

The printMeToo hotstring will never be triggered, because printMe will always trigger before we can write 'Too'.

Using AHK for Java

Another example:

Using AHK Hotstrings for JavaScript

Note that hotstrings aren't case sensitive by default (you can read more here but it's not mandatory for our next hotstrings).

Another important aspect of these types of hotstring is their usage in different text editors. For example, we would use a hotstring triggered after Enter for for loops, because the editor will auto-close the brackets.

Using Hotstrings for JavaScript


Creating our Hotstrings

Okaay, we already saw a few hotstrings. Let's jump into creating more! As we noticed, a hotstring starts with ::, followed by their triggering keyword, then :: again, and ends with the actual string used. So the syntax looks more or less like this:

::Keyword::String
:*:Keyword2::String2
Enter fullscreen mode Exit fullscreen mode

Also, it's important to note that if we want to create hotstrings that use multiple lines, we need to use parentheses ( ):

::quickfox::
(
The quick brown fox jumps over the lazy dog.
But no one asks how the fox feels...
)
Enter fullscreen mode Exit fullscreen mode

Autocomplete quick brown fox

Or another example:

:*:mysignature::
(
Yours faithfully,
John Doe

john.doe@domain.com
www.johnnydoe.com
)
Enter fullscreen mode Exit fullscreen mode

Autocomplete signature in mail

  • Good, let's quickly write some auto-completion for HTML tags:
:*:<a>::<a href="" title=""></

::<ulli>::
(
<ul class="navbar-nav">
<li class="nav-item"><a href=""></</
<li class="nav-item"><a href=""></</
<li class="nav-item"><a href=""></</
</
)
Enter fullscreen mode Exit fullscreen mode

Autocomplete HTML tags using AutoHotKey

However, the hotstrings for HTML tags really depend on the text editor that is currently used... For example, Sublime Text auto-closes opened tags after writing </.



Hmm, okay:

  • Let's write a print statement in the following languages: C, Matlab, JavaScript, Java, C#:
:*:printc::printf('%d\n', num);
:*:printm::fprintf('%d\n', num);

:*:logjs::
{
Send, console.log();{Left}{Left}
Return
}

:*:printjava::
{
Send, System.out.println();{Left}{Left}
Return
}

:*:writec#::
{
Send, Console.WriteLine();{Left}{Left}
Return
}
Enter fullscreen mode Exit fullscreen mode

Print statements using AutoHotKey

Nice. "Debugging" will be a little easier now.

Debuggig using prints

Okay sorry for that. Note that for JS, Java and C# we used multiple instructions (macros) instead of a simple string (we used these a lot in previous parts of this post).

  • Let's see how we will write some loops in C, JavaScript, Python, and Matlab.
::forC::
(
for (int i = 0; i < n; i++) {
)

::forJs::
(
for (let i = 0; i < arr.length; i++) {
)

::forPy::for i in range(0, len(arr)):

:*:forMatlab::
(
for i = 1:step:length(arr)

end
)
Enter fullscreen mode Exit fullscreen mode

Loops autocomplete using AutoHotKey

  • Finally, let's see a really useful feature of hotstrings, namely: Date types. Let's write the following scripts:
:*:datenow::
{
  FormatTime, DateString, , dddd, MMMM dd, yyyy
  Send %DateString%
  Return
}

:*:timenow::
{
  FormatTime, DateString, , HH:mm
  Send %DateString%
  Return
}
Enter fullscreen mode Exit fullscreen mode

Shortcut for Date and Time

Here's more info about dates and time formating in AHK.

Let's see another useful scenario for using dates:

:*:datetoday::
{
  FormatTime, DateString, , yyyy-MM-dd
  Send %DateString%
  Return
}

:*:datetmr::
{
  Date += 1, Days
  FormatTime, DateString, %Date%, yyyy-MM-dd
  Send %DateString%
  Reload ; Return and clear variables
}
Enter fullscreen mode Exit fullscreen mode

Shortcut for Date and Time Excel

Note that for datetmr we now used AHK variables such as %Date% in our script, and we also used Reload instead of Return. If we use Return, we won't clear the created %Date% variable, and each time we wrote datetmr hotstring, we will get the next day of the day before and so on.


The End graphics
"The End" Movie Vector by Vecteezy

This was the last part of my AutoHotKey series for increasing productivity. I hope that I got some of you into using macros and how beneficial they could be in multiple scenarios, or at least you got some other ideas for automating boring stuff with them.

Okay done. Happy coding and keep improving!
R.B.

Top comments (1)

Collapse
 
ruind124 profile image
ruind124

I knew that solving my problem won't cost me much time and resources. Finding an online APA paper writer turned out to be the best possible solution. I always thought that hiring online assistants was too expensive. Fortunately, I got rid of these stereotypes forever. BestCustomWriting is a company providing custom writing solutions to students of all academic levels. APA style is widely used in academic settings, so no matter who you are, what your background is, and what degree you're pursuing. This company can offer outstanding help to everyone.