Skip to content

Instantly share code, notes, and snippets.

@bluedragon1221
Created August 8, 2024 15:30
Show Gist options
  • Save bluedragon1221/925a475241a3c599aedccfc611b4478a to your computer and use it in GitHub Desktop.
Save bluedragon1221/925a475241a3c599aedccfc611b4478a to your computer and use it in GitHub Desktop.
File picker thing with kitty splits, helix, and yazi

You'll need to install yazi (terminal file browser), and jq (for json queries)

create a directory at ~/.config/yazi/filetree_config. This is where we'll store the configs for this file picker so it doens't mess with the main config.

in filetree_config/config.toml:

[manager]
ratio = [ 0, 8, 0 ]

this forces yazi to only use one line per entry.

Next, we'll add a custom keybind to yazi for our file picker:

[[manager.prepend_keymap]]
on   = [ "<Enter>" ]
run  = "plugin --sync smart-enter"
desc = "Enter the child directory, or open the file"

This will launch a plugin called smart-enter, but that doesn't exist yet. So lets create it.

In filetree_config/plugins/smart-enter.yazi/init.lua:

return {
	entry = function()
		local h = cx.active.current.hovered
		if h.cha.is_dir then
			ya.manager_emit("enter", { hovered = true })
		else
			local hx_command = '\'\\e helix ' .. tostring(h.url) .. ' \\r\''
			local command = 'kitten @ send-text --match neighbor:right ' .. hx_command
			os.execute(command)

			ya.manager_emit("quit", {})
		end
	end,
}

Now the yazi part is done. Next, we'll create a bash script to launch it. make sure the script is in your $PATH:

#!/bin/bash
desired_width=25

YAZI_CONFIG_HOME="${HOME}/.config/yazi-filepicker" yazi

# Use jq to filter the JSON output based on the specific window ID
current_width=$(kitty @ ls | jq --arg window_id "$KITTY_WINDOW_ID" '.[].tabs[].windows[] | select(.id == ($window_id | tonumber)) | .columns')

increment=$((desired_width - current_width))

# Open window on the left
kitten @ resize-window --increment $increment --axis horizontal

The wierd jq stuff is just so it will be the correct width relative to the window width.

Lastly, we'll add a kitty keybind to launch the bash script:

allow_remote_control yes
enabled_layouts splits:split_axis=horizontal
map ctr+` launch --location before --cwd current --title tree open_file_tree.bash

now it works!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment