Site icon R-bloggers

Getting the pipe shortcut to work in Positron

[This article was first published on Data Analytics and AI Archives - Giles, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

TLDR: For the life of me, I couldn’t get Ctrl+Shift+M to work for inserting pipes (|>) when editing a Quarto document in Positron visual mode. After countless hours fiddling with keybindings.json, I developed a functional, but obtuse workaround for Windows. It’s not pretty, but it works. This post outlines the approach in case it helps others facing the same problem.

Note: this problem is already on the radar of the development team, so will no doubt be fixed soon. I’ve also only outlined a workaround for Windows. Feel free to reach out if you’ve found a better solution (or a solution that works on other platforms) and I’ll add this to the post.

The problem

I’m a big fan of Quarto as it provides a seamless way to mix data, code and narrative text without ever leaving my IDE. I’ve also been trying to move from RStudio to Positron as I find it snappier, works better with AI tools and works well with Python. Unfortunately, Positron is still experiencing growing pains as a newish IDE, including some shortcuts not working when editing Quarto documents in visual mode.

This isn’t a new issue (e.g: 1, 2 and 3), so is sure to be fixed in the not-too-distant future (likely just after I post this). But, after countless hours messing around with keybindings.json and hassling people from Posit (apologies, Isabella) I asked Claude if it had a solution.

It did.

And it works.

So, I thought I’d share it here, despite the fact it doesn’t spark joy with the minimalist inside of me:

Duct tape and thumbtacks: intercept the shortcut before it reaches Positron

As far as I can tell, the problem comes from Positron / VSCode intercepting some shortcuts when Quarto is in visual mode, which is probably why adjusting the settings did nothing for me. As a (temporary) solution to this, Claude suggested installing a program called AutoHotkey v2 and creating a script operates when Positron is active.

Obviously, as an independent-minded person I did what I’m told and here’s how you can do the same:

1. Install AutoHotkey v2

Get Version 2 from autohotkey.com (the code below will not run under v1).

2. Create the script

Open Autohotkey2 (or AutoHotKey Dash) and select New Script to create a new blank .ahk script in a secluded folder of your choice. Give it a name like ‘positron-keys.ahk’ so future you knows what it’s doing there and use a text editor to replace the contents of the file with the code below (or the equivalent depending on your shortcut preferences):

(SendText sends the literal characters to the Quarto document, so nothing gets re-interpreted by Positron, while #HotIf restricts the behavior to Positron, so it doesn’t interfere with other applications.)

#Requires AutoHotkey v2.0
#SingleInstance Force

#HotIf WinActive("ahk_exe Positron.exe")

^+m::SendText(" |> ")    ;Ctrl+Shift+M -> native pipe
!-::SendText(" <- ")     ;Alt+-        -> assignment arrow

#HotIf

3. Run it

To run the code and launch AutoHotkey, open the ‘positron-keys.ahk’ file. If it worked, a green H should appear in the system tray.

4. Test it

To test it you can now open Positron and try using Ctrl + Shift + M to insert a pipe in a Quarto when in visual mode. Hopefully it works, as this is the only idea covered in this post.

5. Make it persistent

To avoid needing to load the code each time you want to use Positron, you can then add a shortcut to the script that loads at startup. For Windows, you can open the startup folder by typing Win+R and executing the command ‘shell:startup‘. Right-drag positron-keys.ahk into the folder and choose Create shortcut here.

Note: if you have other solutions, please let me know. I’ll happily update this post if others find it useful.

AI use: Claude suggested this solution and wrote the first draft of this post, which I heavily edited.

The post Getting the pipe shortcut to work in Positron appeared first on Giles.

To leave a comment for the author, please follow the link and comment on their blog: Data Analytics and AI Archives - Giles.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Exit mobile version