Git Product home page Git Product logo

expand-ealias.plugin.zsh's Introduction

Expanding aliases in zsh

code taken from here with slight modifications.

I wanted to make some of my zsh aliases behave like vim abbreviations: Namely, as soon as you press space, the alias is expanded and you see the expanded command before executing it. Since I have a large number of clunky aliases, I didn't want all aliases to be expanded by default.

Consequently, I came up with the following (based on something I first saw here).

typeset -a ealiases
ealiases=()

function permealias () 
{ 
  alias "$*";
  echo ealias "$*" >> ~/.oh-my-zsh/custom/zsh-ealiases.zsh
}
function ealias()
{
  alias $1
  ealiases+=(${1%%\=*})
}

function expand-ealias()
{
if [[ $LBUFFER =~ "(^|[;|&])\s*(${(j:|:)ealiases})\$" ]]; then
    zle _expand_alias
    zle expand-word
fi
zle magic-space
}

zle -N expand-ealias

bindkey -M viins ' '    expand-ealias
bindkey -M emacs ' '    expand-ealias
bindkey -M viins '^ '   magic-space     # control-space to bypass completion
bindkey -M emacs '^ '   magic-space
bindkey -M isearch ' '  magic-space # normal space during searches

To define an expandable alias use ealias. The syntax of ealias is the same as that of alias For example, I use

ealias gc='git commit'
ealias gp='git push'
ealias dsp='docker service ps'
ealias h='history | grep'

Create a separate file for your ealiases and place it in the following directory with the following name:

$ touch ${HOME}/.oh-my-zsh/custom/zsh-ealiases.zsh

and paste the above aliases or create your own To add an ealias from the command line you can use the permealias function:

$ permealias "dslogs='docker service logs -f --tail=100'"

The quotes order is important when entering an expandable alias because with more than one word because of the shell different evaluation of single quote and double quotes. Further reading

Now typing gc in a position where zsh expands a command will appear like you typed git commit.

Installation

Your preferred zsh plugin manager, or just clone into ${HOME}/.oh-my-zsh/plugins/ and then add it as the last plugin in the list of oh-my-zsh plugins

plugins=(zsh-nvm git docker brew node npm osx web-search zsh-syntax-highlighting ssh-agent expand-ealias)

License

This page is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.