¿Cómo crear una función bash que regrese al mensaje actual?

¿Cómo crear una función bash que regrese al mensaje actual?

¿Es posible crear una función bash que regrese a la línea actual?

Por ejemplo

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

Entonces

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

Respuesta1

A partir de Bash 4.0puedes hacer esto cambiando las variables READLINE_LINEy READLINE_POINTdentro de la función. (El 'punto' es la posición actual del cursor). Por ejemplo:

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

Respuesta2

Puedes usar "esperar"

Aquí tienes un ejemplo detallado: https://stackoverflow.com/a/77953253/5521600

información relacionada