From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.3 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 28825 invoked from network); 18 Dec 2021 22:41:28 -0000 Received: from zero.zsh.org (2a02:898:31:0:48:4558:7a:7368) by inbox.vuxu.org with ESMTPUTF8; 18 Dec 2021 22:41:28 -0000 ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20210803; t=1639867288; b=WkRZulnRlF9ruZ5X5SpLnJHn6BAS7YiAZWH8ojQR7Hn2FwMRVduxiJ4bkGTWdFXHHGxz7tgwxo u5/62ep1Ys3engglMZJXbh1UVG0zpQRqNFYbGWKCeLZmOgTdKjrepmcKkoyGmJcZG+yMQofyHF KIJb4NYYbVuSIeYlmcExwERoCxWb4SGiMA6m/meKog/yGZvcZ+ENzxNgCLraqxRViWmhYSlkdL RB8B8jtgYbIu3b133P35AQObTOOzliIE1xLt9viX83a28OonlpcBEAF/FVCr+ct6vd54kvORUg mB+Mp0+DmYk+5gkSF85RgD6d9V1K8u2aaQRVV9fBAcZoaw==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (mail-yb1-f180.google.com) smtp.remote-ip=209.85.219.180; dkim=pass header.d=brasslantern-com.20210112.gappssmtp.com header.s=20210112 header.a=rsa-sha256; dmarc=none header.from=brasslantern.com; arc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed; d=zsh.org; s=rsa-20210803; t=1639867288; bh=XAs3AFx+sak4DPQZ2+57HH4dLYCp+FfFZxTopjX/DNU=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:Content-Type:Cc:To:Subject:Message-ID:Date:From:In-Reply-To: References:MIME-Version:DKIM-Signature:DKIM-Signature; b=AUFzdh0vd4zuAfMPjPsJ7tikyyvwV3GbGDBYCNdhJtoBZN5cjkx2jU592X94Q8R4ar1jT1bmgR ouUWHYEsbTDJI2HpUzxRY7eKJeDPzkeAizYrtO8p1p7/zE5RsTFuU1sKXGyxvulxC1PErApwMF vwto1G+8fIRQNGElV4n48qd+8vBaaNUU5NkZYkNM7Y+xYYx/3u0vbfLTdMX7jySD8/nxCQ5lVv fWMMGG7RjnnLperlg0P2QZq0Oft63trZKGc0a2JnQ+ZABBm8Hnbk1PeFRS+XvzPz0iYjav1BO/ snryruf98uknA0NhAUBZ+VybeBgCRiXpflNESaAILPhDZA==; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=zsh.org; s=rsa-20210803; h=List-Archive:List-Owner:List-Post:List-Unsubscribe: List-Subscribe:List-Help:List-Id:Sender:Content-Type:Cc:To:Subject:Message-ID :Date:From:In-Reply-To:References:MIME-Version:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID; bh=1biCs4s0yJCMh/NBV1wFkWaef9bT3ck3uIjaG2hrHYM=; b=IyPDVC/qudKoUXhZ+x4AGGzqaw CFotdAVwCnf5vTQxoRJiKr/zcH3Lgaeh7wvepBG0ydQs2TqycctJ/Ra5aR0hHiIlhY+bgR7NtNskh FOIHBq7bk6DaSllor3bgtsijNdkGkVOSC+YrwNe/iQ3iy9s4v1g7XYUcjT6BG9TyEKNbLWe1DWK8F UTCfpV3RAbFcUADQwLxViN9vW5ZQeLC2JDJD/NEL0E8PtGeGpsxBFjZzIBVjSdW+AF1afyAnTRHKd V4bjQM4Bu1ypft1duCcVQIrXKlmbNV3fuu4jFnFHQQWCFF4pHQhDD7+0ACawrgLoOQaQl4cmkI7Jv WPSmosGQ==; Received: from authenticated user by zero.zsh.org with local id 1myiOL-000DRb-R4; Sat, 18 Dec 2021 22:41:25 +0000 Authentication-Results: zsh.org; iprev=pass (mail-yb1-f180.google.com) smtp.remote-ip=209.85.219.180; dkim=pass header.d=brasslantern-com.20210112.gappssmtp.com header.s=20210112 header.a=rsa-sha256; dmarc=none header.from=brasslantern.com; arc=none Received: from mail-yb1-f180.google.com ([209.85.219.180]:35822) by zero.zsh.org with esmtps (TLS1.3:TLS_AES_128_GCM_SHA256:128) id 1myiNN-000CkD-76; Sat, 18 Dec 2021 22:40:26 +0000 Received: by mail-yb1-f180.google.com with SMTP id f186so17044401ybg.2 for ; Sat, 18 Dec 2021 14:40:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=brasslantern-com.20210112.gappssmtp.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=1biCs4s0yJCMh/NBV1wFkWaef9bT3ck3uIjaG2hrHYM=; b=PhVD90cgf5rHXu0AgV3CskShwRVGVaEsyZkszqe8Rz6w7rHKHrv4RP+seGWKDNM13x ofFp73pSty+m/gcGXgZDIleEdthigTpA0rQCv4WCV1sbb86GBcR/Z12OE5opwsl6w7D5 l6bCBgj6J5LGN4qZqM9bspor0ICDB7/QUIHN8IoEnAKdAqnUxNyPtD28giVCj71XaUMH pEugEMuDbhuOgaYt+b+tYjjQy2UxIogXDeEQrkfIvcdm6bo/6X002a97P22O39XQ8vaT huxr/36IoiA+cMpzZhdZPHSa8J3ZukmizssOowU9A+aso6V0MdvrqfWnI33/4OprQ/Z/ hfhA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=1biCs4s0yJCMh/NBV1wFkWaef9bT3ck3uIjaG2hrHYM=; b=acoJPhB3B9WU5VsSE7i6TXLZHoFGMH5I3K0vLk2CXMkJl6rN6uVbLqjeZTYTI4CtDv eu++AK2NbqERRSnyVyBvlAJmdKicwP0RRQIrgA2WoszdOu58tTjj7TzeXsnCYVWRlXw1 sK1ebOqkUMtbPyeEqrScCwzkHv6gRxe3xna+J+Cswuyezh7tdBo4ITRTDzY+BG4aSgJG KrhJOSktlFWnZg6LdlcOH7YTcVHkCs+s/VMpTG4L0s5Tvgvz6jVub+dNQteOFq7yNp+x JRGHXP9MsXfjXLiQMNWuZgpfn45Mokduf+fHSAYjsF124c2bcc65VNsDQyoW3/TIhzvI AzLg== X-Gm-Message-State: AOAM533Jo2L4YWUEDwmlOFONKWsLEkjSrc1SbSMyxZPn79D0i3Q3I9Zg diitXctxp0LAKh4Ccijy+ewMAabp93/m5yHmUNiWtw== X-Google-Smtp-Source: ABdhPJxugWv2OUCa2FphYLo1X1ybkOs8AFlw4yeyedxmDXRo9azF/c9J0FbM3ppNqSTF38y2i0/uJYUt7OMoR6f051U= X-Received: by 2002:a25:e792:: with SMTP id e140mr14002046ybh.679.1639867223473; Sat, 18 Dec 2021 14:40:23 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Bart Schaefer Date: Sat, 18 Dec 2021 14:40:12 -0800 Message-ID: Subject: Re: Global Aliases, but as a function? To: Zach Riggle Cc: Zsh Users Content-Type: text/plain; charset="UTF-8" X-Seq: 27421 Archived-At: X-Loop: zsh-users@zsh.org Errors-To: zsh-users-owner@zsh.org Precedence: list Precedence: bulk Sender: zsh-users-request@zsh.org X-no-archive: yes List-Id: List-Help: List-Subscribe: List-Unsubscribe: List-Post: List-Owner: List-Archive: On Sat, Dec 18, 2021 at 2:38 AM Zach Riggle wrote: > > Is there any way to define something similar to a "global alias" but > which can modify the original command? > > There's a lot of use-cases for this, but the one that I'm wondering > about specifically is to pass anything with "--help" in the command > line into a pager that does a best-effort colorization IFF the output > is not already colored. The only way to approximate this is by hooking into the ZLE editor to modify $BUFFER before passing it to the lexer/parser. (Aliases modify the single substituted word, during lexical analysis.) There are a bunch of ways to do this, but mainly there are three choices, any/all of which involve creating a custom widget: 1/ Explicitly invoke your widget with a key binding. This gives you the most flexibility because you can edit the result or undo it. (In this case, though, you might instead want to consider using the run-help binding with a suitable function.) 2/ Override the accept-line widget (or the keybindings for it) with your custom widget (and then invoke the ".accept-line" widget at the end). This may be tricky to get right, and to avoid having it interfere with other plugins etc. Be prepared to handle $PREBUFFER and other multi-line-input situations. 3/ Add your widget to the zle-line-finish hooks. This is more likely to play well with plugin managers and you don't need .accept-line, but you still need to handle multi-line input. All of these (except run-help) end up putting the whole command including the appended pipeline into the history. If you want to avoid that, look at zshaddhistory hooks. Naive/incomplete example of #3: page-the-help() { if [[ -z $PREBUFFER && $BUFFER = *--help* ]] then RBUFFER+=" | less" fi } zle -N page-the-help add-zle-hook-widget zle-line-finish page-the-help