Skip to content

Instantly share code, notes, and snippets.

@EgZvor
Last active November 17, 2024 17:47
Show Gist options
  • Save EgZvor/0a6a1f4252e74f322073b07a5f15af94 to your computer and use it in GitHub Desktop.
Save EgZvor/0a6a1f4252e74f322073b07a5f15af94 to your computer and use it in GitHub Desktop.
Editing command line with scrollback using kitty and fish
# This is a modified version of `edit_command_buffer` distributed with fish.
function edit_command_buffer_with_scrollback --description 'Edit the command buffer in an external editor'
set -l f (mktemp)
or return 1
if set -q f[1]
command mv $f $f.fish
set f $f.fish
else
# We should never execute this block but better to be paranoid.
if set -q TMPDIR
set f $TMPDIR/fish.$fish_pid.fish
else
set f /tmp/fish.$fish_pid.fish
end
command touch $f
or return 1
end
commandline -b >$f
# compute cursor line/column
set -l offset (commandline --cursor)
set -l lines (commandline)\n
set -l line 1
while test $offset -ge (string length -- $lines[1])
set offset (math $offset - (string length -- $lines[1]))
set line (math $line + 1)
set -e lines[1]
end
set col (math $offset + 1)
__fish_disable_bracketed_paste
vim \
+"set previewwindow" \
+"norm! G" \
+"wincmd j" \
+"call cursor($line, $col)" \
+"resize 12" -o (begin; history --reverse; kitten @ get-text --extent all; end | psub) $f
set -l editor_status $status
__fish_enable_bracketed_paste
# Here we're checking the exit status of the editor.
if test $editor_status -eq 0 -a -s $f
# Set the command to the output of the edited command and move the cursor to the
# end of the edited command.
commandline -r -- (command cat $f)
commandline -C 999999
else
echo
echo (_ "Ignoring the output of your editor since its exit status was non-zero")
echo (_ "or the file was empty")
end
command rm $f
# We've probably opened something that messed with the screen.
# A repaint seems in order.
commandline -f repaint
end
@EgZvor
Copy link
Author

EgZvor commented Oct 31, 2024

The magic is here vim +$line +"norm! $col|" -o $f (kitten @ get-text --extent all | psub)

NOTE: kitty has to be started with --allow-remote-control for the kitten to work.

We open Vim with two buffers: one the usual temporary created file for the command line and the other is all the scrollback of the current kitty window.

This allows to complete items from the scrollback inside Vim! This is especially useful to complete the words from the output of previous commands.

@EgZvor
Copy link
Author

EgZvor commented Oct 31, 2024

@EgZvor
Copy link
Author

EgZvor commented Nov 1, 2024

Update: added some Vim magic to

  • place the scrollback window above the command-line one
  • show the bottom of the scrollback
  • close the scrollback window automatically without having to use :qa, :wqa
  • resize the command-line window to leave more space for the scrollback

@EgZvor
Copy link
Author

EgZvor commented Nov 17, 2024

Update: added fish history at the beginning of the scrollback buffer.

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