Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

vim

zsh

buffer

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?

like image 719
hhh Avatar asked Apr 15 '10 16:04

hhh


2 Answers

[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.

like image 92
jkramer Avatar answered Nov 19 '22 12:11

jkramer


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
like image 45
ZyX Avatar answered Nov 19 '22 13:11

ZyX