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