Search code examples
fish

natural selection keybindings for fish shell


I am working on some natural selection keybindings.

For, Delete we know that it remove one char from the cursor, but if there is a selection then it removes the whole selection.

So, earlier my keybinding was bind -k dc delete-char. Now i am working on a function to solve this problem. My new keybinding is bind -k dc selection_aware_delete.

function selection_aware_backspace

    set -l cmdline
    if isatty stdin
        set cmdline (commandline --current-selection | string collect)
    end
    
    # if has selection then delete the whole selection else backward-delete-char

    if test -n "$cmdline"

    else
        commandline -f backward-delete-char
        return
    end

end

I have reached a point where i can identify if there is a selection. I can also remove char if there is no selection. I am not understanding how to delete selected text (The line after if test -n "$cmdline" and before else).

Please help.


Solution

  • There is a built-in key binding for deleting the selection, named kill-selection. So commandline -f kill-selection will delete the selection.

    You can get the list of bindings via bind --function-names, or via the bind docs.