Skip to content

Commit d8f5433

Browse files
authored
feat: edit the current command line for bash, fish, or zsh (#253)
See [Command-line editing setup](https://github.com/mikesmithgh/kitty-scrollback.nvim/tree/main?tab=readme-ov-file#command-line-editing) for setup instructions. bash ([edit-and-execute-command](https://www.gnu.org/software/bash/manual/html_node/Miscellaneous-Commands.html#index-edit_002dand_002dexecute_002dcommand-_0028C_002dx-C_002de_0029)), fish ([edit_command_buffer](https://fishshell.com/docs/current/cmds/bind.html#additional-functions)), and zsh ([edit-command-line](https://zsh.sourceforge.io/Doc/Release/User-Contributions.html)) all have the ability to edit the current command in an external editor. The typical workflow is that you have a long command already entered in your shell that you need to modify and execute, this makes it easy to refine complex commands. After [setting up command-line editing in kitty-scrollback.nvim](https://github.com/mikesmithgh/kitty-scrollback.nvim/tree/main?tab=readme-ov-file#command-line-editing), you can open your current command in kitty-scrollback.nvim's paste window. The benefit of this approach compared to using a standard Neovim instance is that you still have access to the scrollback history and kitty-scrollback.nvim's [features](https://github.com/mikesmithgh/kitty-scrollback.nvim/tree/main?tab=readme-ov-file#-features) to help you quickly modify or execute the command. closes #245
1 parent 74fbb22 commit d8f5433

19 files changed

+871
-66
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ jobs:
8181
if: ${{ !(inputs.enable_debug_tmate || inputs.enable_debug_vnc) || (inputs.debug_kitty_version == matrix.kitty_version && inputs.debug_nvim_version == matrix.nvim_version) }}
8282
run: |
8383
sudo apt update
84-
sudo apt install -y xfce4 libxcb-xkb1 xsel tmux fish
84+
sudo apt install -y xfce4 libxcb-xkb1 xsel tmux fish zsh
8585
# homebrew is not used but is required to reproduce an issue for a test case
8686
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
8787

README.md

Lines changed: 190 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Navigate your [Kitty](https://sw.kovidgoyal.net/kitty/) scrollback buffer to qui
4949
- 🫡 [Commands](#-commands)
5050
- ⌨️ [Keymaps](#%EF%B8%8F-keymaps)
5151
- 🪛 [Optional Setup](#-optional-setup)
52+
- [Command-line editing](#command-line-editing)
5253
- [tmux (🧪 experimental )](#tmux--experimental-)
5354
- 👏 [Recommendations](#-recommendations)
5455
- 🤝 [Acknowledgements](#-acknowledgements)
@@ -168,11 +169,27 @@ Navigate your [Kitty](https://sw.kovidgoyal.net/kitty/) scrollback buffer to qui
168169
- `kitty-scrollback.nvim` automatically closes and executes the content of the paste window
169170

170171
</details>
172+
<details>
173+
<summary>😹 Edit the current command line (bash, fish, or zsh)</summary>
174+
175+
https://github.com/user-attachments/assets/6b4f177f-34c2-4ce7-9adb-15e0d3e19498
176+
177+
This requires extra steps to setup. See optional [Command-line editing setup](#command-line-editing).
178+
179+
- Start typing a command in your shell
180+
- Open the current command line in kitty-scrollback.nvim with the following keybind
181+
- bash: `<C-x><C-e>`
182+
- fish: `<M-e>` or `<M-v>` (where `M` is the alt key)
183+
- zsh: `<C-x><C-e>`
184+
- Modify the command and do any additional operations that you typically would perform in kitty-scrollback.nvim
171185

186+
</details>
172187
<details>
173188
<summary>😾 tmux support (🧪 experimental )</summary>
174189

175190
<!-- TODO: add demo -->
191+
This requires extra steps to setup. See optional [tmux setup](#tmux--experimental-).
192+
176193
- Open a tmux pane's scrollback history (default mapping `<C-b>[`)
177194
- That's it! You are in Neovim, navigate the scrollback buffer.
178195

@@ -258,7 +275,7 @@ sh -c "$(curl -s https://raw.githubusercontent.com/mikesmithgh/kitty-scrollback.
258275
'mikesmithgh/kitty-scrollback.nvim',
259276
enabled = true,
260277
lazy = true,
261-
cmd = { 'KittyScrollbackGenerateKittens', 'KittyScrollbackCheckHealth' },
278+
cmd = { 'KittyScrollbackGenerateKittens', 'KittyScrollbackCheckHealth', 'KittyScrollbackGenerateCommandLineEditing' },
262279
event = { 'User KittyScrollbackLaunch' },
263280
-- version = '*', -- latest stable version, may have breaking changes if major version changed
264281
-- version = '^6.0.0', -- pin major version, include fixes and features that do not have breaking changes
@@ -278,7 +295,7 @@ sh -c "$(curl -s https://raw.githubusercontent.com/mikesmithgh/kitty-scrollback.
278295
'mikesmithgh/kitty-scrollback.nvim',
279296
disable = false,
280297
opt = true,
281-
cmd = { 'KittyScrollbackGenerateKittens', 'KittyScrollbackCheckHealth' },
298+
cmd = { 'KittyScrollbackGenerateKittens', 'KittyScrollbackCheckHealth', 'KittyScrollbackGenerateCommandLineEditing' },
282299
event = { 'User KittyScrollbackLaunch' },
283300
-- tag = '*', -- latest stable version, may have breaking changes if major version changed
284301
-- tag = 'v6.0.0', -- pin specific tag
@@ -760,7 +777,7 @@ require("lazy").setup({
760777
"mikesmithgh/kitty-scrollback.nvim",
761778
enabled = true,
762779
lazy = true,
763-
cmd = { "KittyScrollbackGenerateKittens", "KittyScrollbackCheckHealth" },
780+
cmd = { 'KittyScrollbackGenerateKittens', 'KittyScrollbackCheckHealth', 'KittyScrollbackGenerateCommandLineEditing' },
764781
event = { "User KittyScrollbackLaunch" },
765782
config = function()
766783
require("kitty-scrollback").setup({
@@ -787,7 +804,10 @@ action_alias kitty_scrollback_nvim kitten /path/to/your/install/kitty-scrollback
787804
```
788805

789806
## 🧬 Environment Variables
790-
The environment variable `KITTY_SCROLLBACK_NVIM` is set to `true` while kitty-scrollback.nvim is active.
807+
The environment variable `KITTY_SCROLLBACK_NVIM` is set to `'true'` while kitty-scrollback.nvim is active.
808+
> [!NOTE]
809+
> `'true'` is a string because `KITTY_SCROLLBACK_NVIM` is an environment variable. Make sure to use a string `'true'` in Lua
810+
> instead of accidentally using a boolean `true`. Otherwise, the conditional checks will not operate as expected.
791811

792812
This can be used to in your Neovim configuration to provide kitty-scrollback.nvim specific behavior that may differ from a regular Neovim instance.
793813
```lua
@@ -831,10 +851,11 @@ require('kitty-scrollback').setup({
831851
## 🫡 Commands
832852
The API is available via the `kitty-scrollback.api` module. e.g., `require('kitty-scrollback.api')`
833853

834-
| Command | API | Description |
835-
| :-------------------------------------------------- | :----------------------------------------------------- | :---------------------------------------------------------------------- |
836-
| `:KittyScrollbackGenerateKittens [generate_modes]` | `generate_kittens(table<string\|'commands'\|'maps'>)?` | Generate Kitten commands used as reference for configuring `kitty.conf` |
837-
| `:KittyScrollbackCheckHealth` | `checkhealth()` | Run `:checkhealth kitty-scrollback` in the context of Kitty |
854+
| Command | API | Description |
855+
| :-------------------------------------------------- | :------------------------------------------------------------ | :------------------------------------------------------------------------------------------------ |
856+
| `:KittyScrollbackGenerateKittens [generate_modes]` | `generate_kittens(table<string\|'commands'\|'maps'>)?` | Generate Kitten commands used as reference for configuring `kitty.conf` |
857+
| `:KittyScrollbackCheckHealth` | `checkhealth()` | Run `:checkhealth kitty-scrollback` in the context of Kitty |
858+
| `:KittyScrollbackGenerateCommandLineEditing` | `generate_command_line_editing(string\|'bash'\|'fish'\|'zsh)` | Generate command-line editing commands used as reference for configuring `bash`, `fish`, or `zsh` |
838859

839860
## ⌨️ Keymaps
840861
The API is available via the `kitty-scrollback.api` module. e.g., `require('kitty-scrollback.api')`
@@ -857,6 +878,167 @@ The API is available via the `kitty-scrollback.api` module. e.g., `require('kitt
857878

858879
## 🪛 Optional Setup
859880

881+
### Command-line editing
882+
883+
> [!NOTE]
884+
> Command-line editing is only supported for `bash`, `fish`, or `zsh`
885+
>
886+
> Only `zsh` version 5.9 or greater is supported by kitty-scrollback.nvim for command-line editing. If you are using `zsh`,
887+
> please confirm you have a compatible version by running `zsh --version`
888+
889+
Generate the configuration and add it to the appropriate location. The comments in the configuration will provide
890+
additional setup instructions. `KittyScrollbackGenerateCommandLineEditing` requires one parameter, either `bash`, `fish`, or `zsh`.
891+
```sh
892+
nvim --headless +'KittyScrollbackGenerateCommandLineEditing <shell>' # replace <shell> with bash, fish, or zsh
893+
```
894+
895+
The generated configuration will mention the environment variable `KITTY_SCROLLBACK_NVIM_EDIT_ARGS`. This can be used to pass arguments to kitty-scrollback.nvim in command-line editing mode.
896+
This allows [Kitten Arguments](#kitten-arguments) setup specific to opening kitty-scrollback.nvim in command-line editing mode
897+
that you may want to configure differently than your standard kitty-scrollback.nvim setup.
898+
899+
### Example setups
900+
<details>
901+
<summary>bash</summary>
902+
903+
- Run `nvim --headless +'KittyScrollbackGenerateCommandLineEditing bash'`. You should see similar output to the following:
904+
905+
```bash
906+
907+
# add the following environment variables to your bash config (e.g., ~/.bashrc)
908+
# the editor defined in KITTY_SCROLLBACK_VISUAL will be used in place of VISUAL
909+
# for other scenarios that are not editing the command-line. For example, C-xC-e
910+
# will edit the current command-line in kitty-scrollback.nvim and pressing v in
911+
# less will open the file in $KITTY_SCROLLBACK_VISUAL (defaults to nvim if not
912+
# defined)
913+
export KITTY_SCROLLBACK_VISUAL='nvim'
914+
export VISUAL='~/.local/share/nvim/lazy/kitty-scrollback.nvim/scripts/edit_command_line.bash'
915+
916+
# [optional] pass arguments to kitty-scrollback.nvim in command-line editing mode
917+
# by using the environment variable KITTY_SCROLLBACK_NVIM_EDIT_ARGS
918+
# export KITTY_SCROLLBACK_NVIM_EDIT_ARGS=''
919+
920+
# [optional] customize your readline config (e.g., ~/.inputrc)
921+
# default mapping in vi mode
922+
set keymap vi-command
923+
"v": vi-edit-and-execute-command
924+
925+
# default mapping in emacs mode
926+
set keymap emacs
927+
"\C-x\C-e": edit-and-execute-command
928+
```
929+
- In this case, I will use the default mappings and not make any changes to `~/.inputrc`. Open,
930+
`~/.bashrc` and add the following:
931+
```bash
932+
# ~/.bashrc
933+
export VISUAL='~/.local/share/nvim/lazy/kitty-scrollback.nvim/scripts/edit_command_line.bash'
934+
```
935+
- Close and reopen your `bash` shell
936+
- Enter a command and press `<C-x><C-e>`, you should now be editing your command line with kitty-scrollback.nvim!
937+
938+
Since this configuration is making use of the `VISUAL` environment variable. The environment variable `KITTY_SCROLLBACK_VISUAL`
939+
can be used for cases where programs open `VISUAL` that do not involve command-line editing. By default, if `KITTY_SCROLLBACK_VISUAL`
940+
is not set, it will default to `nvim`. For example, if you open a file with `less` and press `v` to open the file with the `VISUAL`
941+
editor, it will open in the command defined in `KITTY_SCROLLBACK_VISUAL`. So, in this case open in `nvim` as you typically would expect.
942+
943+
</details>
944+
<details>
945+
<summary>fish</summary>
946+
947+
- Run `nvim --headless +'KittyScrollbackGenerateCommandLineEditing fish'`. You should see similar output to the following:
948+
949+
```fish
950+
# add the following function and bindings to your fish config
951+
# e.g., ~/.config/fish/conf.d/kitty_scrollback_nvim.fish or ~/.config/fish/config.fish
952+
953+
function kitty_scrollback_edit_command_buffer
954+
set --local --export VISUAL '~/.local/share/nvim/lazy/kitty-scrollback.nvim/scripts/edit_command_line.sh'
955+
edit_command_buffer
956+
commandline ''
957+
end
958+
959+
bind --mode default \ee kitty_scrollback_edit_command_buffer
960+
bind --mode default \ev kitty_scrollback_edit_command_buffer
961+
962+
bind --mode visual \ee kitty_scrollback_edit_command_buffer
963+
bind --mode visual \ev kitty_scrollback_edit_command_buffer
964+
965+
bind --mode insert \ee kitty_scrollback_edit_command_buffer
966+
bind --mode insert \ev kitty_scrollback_edit_command_buffer
967+
968+
# [optional] pass arguments to kitty-scrollback.nvim in command-line editing mode
969+
# by using the environment variable KITTY_SCROLLBACK_NVIM_EDIT_ARGS
970+
# set --global --export KITTY_SCROLLBACK_NVIM_EDIT_ARGS ''
971+
```
972+
973+
- In this case, I will map `<M-e>` to use kitty-scrollback.nvim and keep `<M-v>` with the default mappings. Open
974+
`~/.config/fish/conf.d/kitty_scrollback_nvim.fish` and add the following:
975+
976+
```fish
977+
# ~/.config/fish/conf.d/kitty_scrollback_nvim.fish
978+
function kitty_scrollback_edit_command_buffer
979+
set --local --export VISUAL '~/.local/share/nvim/lazy/kitty-scrollback.nvim/scripts/edit_command_line.sh'
980+
edit_command_buffer
981+
commandline ''
982+
end
983+
984+
bind --mode default \ee kitty_scrollback_edit_command_buffer
985+
bind --mode visual \ee kitty_scrollback_edit_command_buffer
986+
bind --mode insert \ee kitty_scrollback_edit_command_buffer
987+
```
988+
- Close and reopen your `fish` shell
989+
- Enter a command and press `<M-e>` (where `M` is the alt key), you should now be editing your command line with kitty-scrollback.nvim!
990+
991+
Since this example configuration is not rebinding `\ev`. You can still press `<M-v>` (where `M` is the alt key), to open then
992+
command-line buffer in the editor defined in the `VISUAL` environment variable. This gives you some extra flexibility in the fish shell!
993+
994+
</details>
995+
<details>
996+
<summary>zsh</summary>
997+
998+
- Run `nvim --headless +'KittyScrollbackGenerateCommandLineEditing zsh'`. You should see similar output to the following:
999+
1000+
```zsh
1001+
# IMPORTANT: kitty-scrollback.nvim only supports zsh 5.9 or greater for command-line editing,
1002+
# please check your version by running: zsh --version
1003+
1004+
# add the following environment variables to your zsh config (e.g., ~/.zshrc)
1005+
1006+
autoload -Uz edit-command-line
1007+
zle -N edit-command-line
1008+
1009+
function kitty_scrollback_edit_command_line() {
1010+
local VISUAL='~/.local/share/nvim/lazy/kitty-scrollback.nvim/scripts/edit_command_line.sh'
1011+
zle edit-command-line
1012+
zle kill-whole-line
1013+
}
1014+
zle -N kitty_scrollback_edit_command_line
1015+
1016+
bindkey '^x^e' kitty_scrollback_edit_command_line
1017+
1018+
# [optional] pass arguments to kitty-scrollback.nvim in command-line editing mode
1019+
# by using the environment variable KITTY_SCROLLBACK_NVIM_EDIT_ARGS
1020+
# export KITTY_SCROLLBACK_NVIM_EDIT_ARGS=''
1021+
```
1022+
- Open `~/.zshrc` and add the following:
1023+
```zsh
1024+
# ~/.zshrc
1025+
autoload -Uz edit-command-line
1026+
zle -N edit-command-line
1027+
1028+
function kitty_scrollback_edit_command_line() {
1029+
local VISUAL='~/.local/share/nvim/lazy/kitty-scrollback.nvim/scripts/edit_command_line.sh'
1030+
zle edit-command-line
1031+
zle kill-whole-line
1032+
}
1033+
zle -N kitty_scrollback_edit_command_line
1034+
1035+
bindkey '^x^e' kitty_scrollback_edit_command_line
1036+
```
1037+
- Close and reopen your `zsh` shell
1038+
- Enter a command and press `<C-x><C-e>`, you should now be editing your command line with kitty-scrollback.nvim!
1039+
1040+
</details>
1041+
8601042
### tmux (🧪 experimental )
8611043

8621044
> [!WARNING]\

0 commit comments

Comments
 (0)