Use HotStrings in your Dev Life to increase your daily Productivity with AHK
Tags: showdev, productivity, autohotkey, tutorial
Published: 2020-11-07 13:21:34

*Cover Photo by [Александар Цветановић](https://www.pexels.com/photo/orange-cat-foot-on-laptop-keyboard-1440387/) on [Pexels](https://www.pexels.com/)*

## Contents
- [What are AHK's HotStrings?](#what-are-ahks-hotstrings)
- [Hotstrings we are going to create](#hotstrings-we-are-going-to-create)
- [Hotstrings types](#hotstrings-types)
- [Creating our Hotstrings](#creating-our-hotstrings)

## What are AHK's HotStrings?
[HotStrings](https://www.autohotkey.com/docs/Hotstrings.htm) in [AutoHotKey](https://www.autohotkey.com/) "shortcuts" that are  mainly used to expand (or auto-replace) abbreviations as you type them:

![Using hotstrings abbreviation](https://dev-to-uploads.s3.amazonaws.com/i/crhu97l30wk4tikr5oaf.gif)

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++) {
```

- Hotstrings that are triggered right after you wrote them (they contain an `*` - asterisk)

```
:*:printc::printf('%d\n', num);

:*:<a>::<a href="" title=""></a>
```

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(); 
```

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

![Using AHK for Java](https://dev-to-uploads.s3.amazonaws.com/i/ib2mf970r85dd5sv9ebj.gif)

Another example:

![Using AHK Hotstrings for JavaScript](https://dev-to-uploads.s3.amazonaws.com/i/emug8s6zmxkq6d87l0oz.gif)

Note that hotstrings aren't case sensitive by default ([you can read more here but it's not mandatory for our next hotstrings](https://www.autohotkey.com/docs/Hotstrings.htm)).

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](https://dev-to-uploads.s3.amazonaws.com/i/vl7tufanq79rut8zjq2z.gif)

---

## 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
```

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...
)
```

![Autocomplete quick brown fox](https://dev-to-uploads.s3.amazonaws.com/i/35fh0zrc60cnh14okf7a.gif)

Or another example:

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

john.doe@domain.com
www.johnnydoe.com
)
```

![Autocomplete signature in mail](https://dev-to-uploads.s3.amazonaws.com/i/12i7i3z1wzch8iil82el.gif)

<br/>

- 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=""></</
</
)
```

![Autocomplete HTML tags using AutoHotKey](https://dev-to-uploads.s3.amazonaws.com/i/z1kewlhrgpjoftx4f2lz.gif)

However, the hotstrings for HTML tags really depend on the text editor that is currently used... For example, [Sublime Text](https://www.sublimetext.com/) auto-closes opened tags after writing `</`.

<br/>
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
}
```

![Print statements using AutoHotKey](https://dev-to-uploads.s3.amazonaws.com/i/3mhycsibxwfuwc1zvv6k.gif)

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

![Debuggig using prints](https://dev-to-uploads.s3.amazonaws.com/i/j3n72czl2sllp6yc6ed5.jpg)

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).

<br/>

- 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
)
```

![Loops autocomplete using AutoHotKey](https://dev-to-uploads.s3.amazonaws.com/i/ewbj31huxilnmxr7ifss.gif)

<br/>

- 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
}
```

![Shortcut for Date and Time](https://dev-to-uploads.s3.amazonaws.com/i/jkewuta2lfzpzle855va.gif)

[Here's more info about dates and time formating in AHK](https://www.autohotkey.com/docs/commands/FormatTime.htm).

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
}
```

![Shortcut for Date and Time Excel](https://dev-to-uploads.s3.amazonaws.com/i/aur0yiwlnfjokybft6ss.gif)

Note that for `datetmr` we now used [AHK variables](https://www.autohotkey.com/docs/Variables.htm) 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](https://dev-to-uploads.s3.amazonaws.com/i/1s60py6uektux3mxxlln.jpg)
*<a href="https://www.vecteezy.com/free-vector/movie">"The End" Movie Vector by Vecteezy</a>*

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.