5

Its not essential but it bugs me a bit, here is the fragment from my .zshrc

a function/widget called add_sudo, that will go at the beginning of line, writes sudo there and then should go at the end of the line.

Its bind to ctrl+f

But it does not go at the end of the line, it ignores last command and sits there after it wrote sudo.

add_sudo() {
  zle beginning-of-line;
  zle -U "sudo ";
  zle end-of-line;
}

zle -N add_sudo
bindkey "^f" add_sudo

any solution to this?

paulmelnikow
  • 16,895
  • 8
  • 63
  • 114
DoTheEvo
  • 874
  • 1
  • 9
  • 21
  • 1
    You should probably modify `$BUFFER` directly (i.e., `BUFFER="sudo $BUFFER"`), then move `$CURSOR` (i.e., `(( CURSOR += 5 ))`). Pushing into the input stack is, as you see, totally unintuitive. – 4ae1e1 Oct 24 '15 at 10:00

1 Answers1

7

I can answer this one! I just joined, glad to help..... and I read the question wrong, but now I'm here to redeem myself, with the help of @4ae1e1, all credit to him for mentioning to use BUFFER= and CURSOR=

add_sudo (){
prefix="sudo"
BUFFER="$prefix $BUFFER"
CURSOR=$(($CURSOR + $#prefix + 1))
}
zle -N add_sudo
bindkey "^f" add_sudo

Does what you would like, and now I can use this after every time I forget to sudo, too!

EDITx2

Of note, this actually places the cursor back to wherever it was prior, my preferred use. You can, as 4aelel stated, use CURSOR+=5 to place it at the end of the line.

Also of note, again, I realized I truly haven't fulfilled the question, as it was how to do this with zsh and zli, rather than how to do this. If I come across an answer I'll append with both solutions.I'm new to zli and it's nuances, just recently moving to zsh.

joe modo
  • 86
  • 5
  • 2
    I noticed you have this tagged as Prezto, if you do in fact use it, it appears this functionality already exists in keyboard.zsh, see this commit https://github.com/sorin-ionescu/prezto/commit/9f8b41aaec63076d25b41b41c85c9244576c2cdb – joe modo Oct 24 '15 at 12:44