Macros in Kate editor version 2.5.5

Update: For macros in Kate version 3.8.4, please see this post.
It turns out that despite appearances, it is possible to write macros in Kate, as it supports an ECMAScript (JavaScript) -based scripting language. What's more, these macros, if set up correctly, can be invoked with a couple of keystrokes only.

Although the official Kate handbook describes these scripts in detail, the documentation is for Kate version 3.4, whereas I'm using 2.5.5. Many of the functions listed in the handbook are not available, so I decided to post about the ones I found under 2.5.5.

My main aim was to create simple macros that insert some text at the cursor.

Location of the scripts

The scripts should be located in a share/apps/katepart/script folder. It might be necessary to search a bit, as the exact location might be different on each system. Scripts under Kate 2.5.5 do not have functions in them, so you'll have to name the files the way you'll invoke the macros. For example, if you want to run a macro by typing s or st, then the filename will have to be s.js or st.js. More on how to run a macro later.

Writing the script

This is a short script that can be used to insert some text at the point where the cursor currently is. The following should be the entire contents of the file.
text = "TextToBeInserted";
document.insertText(view.cursorLine(),view.cursorColumnReal(),text);

And that's it. You can use the usual things in the string, like \n or \".

If you want other functions or objects, you can look at the scripts already there in the directory, or look into the Kate source code (Google will do that for you), and use try and error.

Running the script / macro

You'll have to restart Kate after creating or modifying the script files. I don't think there's a way to tell Kate to reload them. After that's done, you can run a macro by typing its name in the command line. Press F7 to get to the comand line, type, for example, s or st, and hit ENTER. I know it's 3 (or more) keystrokes, and not one, but it's better than having no macros at all.

Popular Posts