현재 줄로 돌아가는 bash 함수를 만드는 것이 가능합니까?
예를 들어
#~/.bashrc
do-magic() {
# do some magic and return the input to current prompt line
# for example "hello"
}
bind -x '"\C-e":do-magic'
그 다음에
$ echo <ctrl+e>
# become
$ echo hello
답변1
배쉬 4.0부터함수 내에서 READLINE_LINE
및 변수를 변경하여 이를 수행할 수 있습니다 . READLINE_POINT
('포인트'는 현재 커서 위치입니다.) 예를 들면 다음과 같습니다.
_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'
답변2
"기대"를 사용할 수 있습니다
여기에 자세한 예가 있습니다. https://stackoverflow.com/a/77953253/5521600