mikesmithgh / kitty-scrollback.nvim

😽 Open your Kitty scrollback buffer with Neovim. Ameowzing!
Apache License 2.0
375 stars 11 forks source link

feat: make the current cmdline editable in place #245

Open IndianBoy42 opened 1 month ago

IndianBoy42 commented 1 month ago

Sometimes I start typing a long complicated command and then I get annoyed with the lack of vim keybindings. My shell (fish) has a keybinding to open the current commandline in $EDITOR but then I lose sight of the scrollback, so its suboptimal.

kitty-scrollback.nvim has the potential to be useful here. Currently adding to the current commandline is easy, but there is no easy way to select, edit or replace the existing commandline.

mikesmithgh commented 1 month ago

Hi @IndianBoy42, I think this is a good idea.

What binding are you using to do this in fish?

I am currently doing the same thing for but with bash edit-and-execute-command (C-x C-e).

I need to figure out if there is a way to grab the current command in a shell agnostic way (or maybe just support bash, zsh, fish). Kitty may have something around this related to shell integration, I'll take a look.

IndianBoy42 commented 1 month ago

In fish it's Alt+e

Shell agnostic is ideal, but if not then covering bash zsh and fish should cover 99% of users.

mikesmithgh commented 1 month ago

In first it's Alt+e Shell agnostic is ideal, but if not then covering bash zsh and fish should cover 99% of users.

Thanks! Looks like zsh doesn't have a default binding but does offer it as well.

https://unix.stackexchange.com/questions/6620/how-to-edit-command-line-in-full-screen-editor-in-zsh

autoload -z edit-command-line
zle -N edit-command-line
bindkey "^X^E" edit-command-line

I'll have to think about the implementation of this a bit, but can hopefully come up with something 👍

IndianBoy42 commented 1 month ago

After checking it out a bit I think direct shell integration is necessary. OSC133 doesn't mark the cmdline editable area.

The user modifies the edit command binding so that it is something like EDITOR=kitty-scrollback-editor edit-command-line. From the scrollback overlay we send the keys to the underlying window, which doesn't even open neovim it just stashes that filename somewhere for the overlay to find and blocks waiting for the overlay to close. From the scrollback overlay we use that filename to get the current commandline, find where it is in the buffer (we know its near the cursor so shouldn't be a costly search), and make just that region editable. Or make a popup. When the scrollback is closing we send the potentially updated commandline to that temporary file and unblock the underlying window.

mikesmithgh commented 1 month ago

Thanks! I agree, this sounds like the approach to take.

When the scrollback is closing we send the potentially updated commandline to that temporary file and unblock the underlying window.

I'm not sure how long the temporary file is available but we could also exit 1 for the EDITOR command and just use that input file as reference of the cmdline as well. Then prefill that prompt window. I think it would just need to clear the existing command before executing.

There is an after_ready callback that may make sense to hook into for this.

Maybe something like

EDITOR='kitty @ kitten /Users/mike/gitrepos/kitty-scrollback.nvim/python/kitty_scrollback_nvim.py --config ksb_builtin_edit_command_line' edit-command-line

Would need to maybe create a helper script or a way to pass the filename and then in the ksb_builtin_edit_command_line config have the callback after_ready that prefills and opens the paste window with the command.

I've been busy of late, so not sure when I'll have time to dedicate to implementing. But, I think we are going down the right track.

mikesmithgh commented 1 month ago

Hey @IndianBoy42 I have a draft PR #253 for this feature that I am still working on. It is in a good state, I just need to document and write tests. If you'd like to try it out and give me feedback, use the branch feat-command-line-editing-mode.

You will have to run the command :KittyScrollbackGenerateCommandLineEditing fish and it will generate the fish config that you can use for this plugin. If you are using lazy.nvim make sure to add KittyScrollbackGenerateCommandLineEditing to the commands like:

  {
    'mikesmithgh/kitty-scrollback.nvim',
    -- ...
    cmd = { 'KittyScrollbackGenerateKittens', 'KittyScrollbackCheckHealth', 'KittyScrollbackGenerateCommandLineEditing', },
    -- ...
  }

It will look something like this (but have updated paths for your machine):

# add the following function and bindings to your fish config (e.g., ~/.config/fish/config.fish) 

function kitty_scrollback_edit_command_buffer
  set -lx VISUAL '/Users/mike/gitrepos/kitty-scrollback.nvim/scripts/edit_command_line.sh'
  edit_command_buffer
  commandline ''
end

bind --mode default \ee kitty_scrollback_edit_command_buffer
bind --mode default \ev kitty_scrollback_edit_command_buffer

bind --mode visual \ee kitty_scrollback_edit_command_buffer
bind --mode visual \ev kitty_scrollback_edit_command_buffer

bind --mode insert \ee kitty_scrollback_edit_command_buffer
bind --mode insert \ev kitty_scrollback_edit_command_buffer

Please let me know if it works for you or you have any issues 👍

Demo:

https://github.com/mikesmithgh/kitty-scrollback.nvim/assets/10135646/a0fbd46d-a5cf-42a6-bf95-de0da4bb60a5

IndianBoy42 commented 1 month ago

Very cool! checking it out now.

A cool tip for fish integration is that every fish script in ~/.config/fish/conf.d/ is automatically sourced before config.fish. Thats how fish plugins are generally implemented

I'm not sure how zsh plugins work but I suspect there is somewhere you could the bindings file automatically so it Just Works TM for users

bash... :shrug: