From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 1301 invoked by alias); 17 May 2016 15:43:40 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: X-Seq: 21573 Received: (qmail 27667 invoked from network); 17 May 2016 15:43:39 -0000 X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM, T_DKIM_INVALID autolearn=ham autolearn_force=no version=3.4.1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex.ru; s=mail; t=1463499099; bh=ZCpG6fBeo0RRvmlc5BF0PXGspnJH0rlm7AbanrfBmOI=; h=X-Yandex-Sender-Uid:X-Yandex-Hint:X-Yandex-Hint:From:To: In-Reply-To:References:Subject:MIME-Version:Message-Id:X-Mailer: Date:Content-Transfer-Encoding:Content-Type; b=CXQvk80EXbBL5PfxjKozib6lD6RfgphI0NwWM75KcLhL0kS1GbBTjU3b1KXKFy88Q iUYzboPCm/yLoZIDdOGAhIkm5XIuFA1nlHDOzR7RRqNp2TeVT90Xn/liSYbACqQe6T 1d3Y3izJUHJj1yXN3cmu+FAn64ZfFFqi20t3KHlU= Authentication-Results: mxback10m.mail.yandex.net; dkim=pass header.i=@yandex.ru X-Yandex-Suid-Status: 1 0,1 0,1 20735984 X-Yandex-Sender-Uid: 9151298 From: "Nikolay Aleksandrovich Pavlov (ZyX)" To: Bart Schaefer , Zsh Users In-Reply-To: References: <86inydgbp6.fsf@student.uu.se> Subject: Re: sudo user-command-1; also-sudoed-command-2 MIME-Version: 1.0 Message-Id: <45531463499099@web19m.yandex.ru> X-Mailer: Yamail [ http://yandex.ru ] 5.0 Date: Tue, 17 May 2016 18:31:39 +0300 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=utf-8 17.05.2016, 13:44, "Bart Schaefer" : > On Mon, May 16, 2016 at 10:22 AM, Emanuel Berg wrote: >>  How can I execute a block of arbitrary commands >>  with sudo, but only having to type sudo once, >>  and still be able to use the user functions? > > Why not > >   sudo ZDOTDIR="${ZDOTDIR:-$HOME}" zsh -c "...." > > ?? That should cause zsh to read the current user's .zshrc etc. files > before executing the "...", thereby obtaining the alias and function > definitions. Why should it read zshrc without -i? If I am not mistaking using `sudo zsh -ic "…"` should be enough (assuming sudo keeps both $HOME and $ZDOTDIR, I do not remember this). --- I would not recommend using $=BUFFER in your case: this is going to break in many cases (i.e. if inside quotes there is any sequence of whitespace characters, except a sequence containing exactly one space this will break). The easiest way to fix this is ```zsh accept-line () { emulate -L zsh local words words=( ${(z)BUFFER} ) case $words[1] in (zudo) BUFFER="zudo-f ${(q-)words[2,-1]}" ;; esac zle .accept-line } ``` (changed `$=` to `$(z)`). This will not run cycles though. You may change zudo-f to ```zsh zudo-f () { emulate -L zsh sudo ZDOTDIR="${ZDOTDIR:-$HOME}" zsh -ic "$1" } ``` , but this has different downside: if alias or function was defined in user configuration everything may be fine. But if it was defined in the interactive session, it will not be used. Also this is going to be slower then your variant.