Como criar uma função bash que retorne ao prompt atual?

Como criar uma função bash que retorne ao prompt atual?

É possível criar uma função bash que retorne à linha atual?

Por exemplo

#~/.bashrc
do-magic() {
    # do some magic and return the input to current prompt line
    # for example "hello"
}
bind -x '"\C-e":do-magic'

Então

$ echo <ctrl+e>
# become
$ echo hello

Responder1

A partir do Bash 4.0você pode fazer isso alterando as variáveis READLINE_LINE​​​​e READLINE_POINTdentro da função. (O 'ponto' é a posição atual do cursor.) Por exemplo:

_paste() {
    local str="Hello $(date +%F)!"
    local len=${#str}
    # Note: Bash 5.x wants the length in characters, but Bash 4.x apparently
    #       wanted bytes. To properly insert non-ASCII text, you used to need:
    # local len=$(printf '%s' "$str" | wc -c)

    # Insert the text in between [0..cursor] and [cursor..end]
    READLINE_LINE=${READLINE_LINE:0:$READLINE_POINT}${str}${READLINE_LINE:$READLINE_POINT}
    # Advance the cursor
    READLINE_POINT=$((READLINE_POINT + len))
}

bind -x '"\C-e": _paste'

Responder2

Você pode usar "esperar"

aqui você tem um exemplo detalhado: https://stackoverflow.com/a/77953253/5521600

informação relacionada