简体   繁体   中英

ZSH: how can I run Vim-style substitute command in command line?

I forgot the array syntax while on Zsh-commandline:

$ hello=[1,2,3,4] %ERR: 

I want to fix the problem by substitution. In Vim, I would do :.s@,@ @g . So how can I edit the current line, or let call it a current buffer, by running a command on it?

[jkramer/sgi5k:~]# list=(1,2,3,4,5,6,7,8,9,10)
[jkramer/sgi5k:~]# !:gs/,/ /                  
list=(1 2 3 4 5 6 7 8 9 10)

See zshexpn(1) for more information of history completion/alternation.

Only using custom zle widget, for example:

function _-sedsubs()
{
    emulate -LR zsh
    local SEDARG="s"
    zle -R "Substitution: $SEDARG"
    local key=""
    read -k key
    local -r start=$key
    while (( (#key)!=(##\n) &&
             (#key)!=(##\r) )) ; do
        if (( (#key)==(##^?) || (#key)==(##^h) )) ; then
            SEDARG=${SEDARG[1,-2]}
        else
            SEDARG="${SEDARG}$key"
        fi
        zle -R "Substitution: $SEDARG"
        read -k key || return 1
    done
    BUFFER="$(echo $BUFFER | sed -r -e "$SEDARG")"
}
zle -N sedsubstitute                     _-sedsubs
bindkey "\C-o:s" sedsubstitute

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM