20140625-zsh-expand-alias.html 9.6 KB
Newer Older
GI's avatar
GI committed
1 2 3 4 5 6 7 8 9
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="author" content="Gautam Iyer">
    <title>
      Expanding aliases in zsh
GI's avatar
GI committed
10
    </title><link rel="stylesheet"
GI's avatar
GI committed
11 12 13
	href="/dev/static/bootstrap/3.3.7/css/bootstrap.css">
      <link rel="stylesheet"
	href="/share/css/pygments-theme.css">
GI's avatar
GI committed
14
      <link rel="stylesheet" href="/share/css/local.css"><!--[if lt IE 9]>
GI's avatar
GI committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.js">
      </script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.js">
      </script>
    <![endif]-->
  </head>
  <body style='position: relative' data-spy='scroll' data-target='#col-right'>
  <nav class="navbar navbar-default navbar-static-top">
    <div class="container">
      <div class="navbar-header">
	<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
	  <span class="sr-only">Toggle navigation</span>
	  <span class="icon-bar"></span>
	  <span class="icon-bar"></span>
	  <span class="icon-bar"></span>
	</button>
	<a class="navbar-brand" href="http://localhost:8000">Gautam Iyer</a>
      </div>
      <div id="navbar" class="navbar-collapse collapse">
	<ul class="nav navbar-nav">
GI's avatar
GI committed
35
	  <li>
GI's avatar
GI committed
36 37
	      <a href="http://localhost:8000/index.html">Research</a>
	    </li>
GI's avatar
GI committed
38
	  <li>
GI's avatar
GI committed
39 40
	      <a href="http://localhost:8000/teaching">Teaching</a>
	    </li>
GI's avatar
GI committed
41
	  <li>
GI's avatar
GI committed
42 43
	      <a href="http://localhost:8000/contact.html">CV/Contact</a>
	    </li>
GI's avatar
GI committed
44
	  </ul>
GI's avatar
GI committed
45
	<ul class="nav navbar-nav navbar-right">
GI's avatar
GI committed
46
	  <li class='disabled'>
GI's avatar
GI committed
47 48
	      <a href="#">Code</a>
	    </li>
GI's avatar
GI committed
49
	  <li class='active'>
GI's avatar
GI committed
50 51
	      <a href="http://localhost:8000/blog">Blog</a>
	    </li>
GI's avatar
GI committed
52
	  <li>
GI's avatar
GI committed
53 54
	      <a href="http://localhost:8000/dev/test">Test</a>
	    </li>
GI's avatar
GI committed
55
	  </ul>
GI's avatar
GI committed
56 57
      </div><!--/.nav-collapse -->
    </div>
GI's avatar
GI committed
58 59
  </nav><div class='container'>
      <div class='row'>
GI's avatar
GI committed
60
    <div class='col-md-9' id='col-main'>
GI's avatar
GI committed
61 62
      <div class='justify'>
    <h1>
GI's avatar
GI committed
63
	  Expanding aliases in zsh
GI's avatar
GI committed
64
	  <br />
GI's avatar
GI committed
65
	    <small>2014-06-25</small>
GI's avatar
GI committed
66
	  </h1>
GI's avatar
GI committed
67 68 69 70 71 72 73 74
      <p>I wanted to make some of my <a href="http://zsh.sourceforge.net/">zsh</a> aliases behave like <a href="http://www.vim.org/">vim</a> 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&rsquo;t want all aliases to be expanded by default.</p>
<p>Consequently, I came up with the following (based on something I first saw <a href="http://blog.patshead.com/2012/11/automatically-expaning-zsh-global-aliases---simplified.html">here</a>.</p>
<div class="codehilite"><pre><span></span><span class="nb">typeset</span> -a ealiases
<span class="nv">ealiases</span><span class="o">=()</span>

<span class="k">function</span> ealias<span class="o">()</span>
<span class="o">{</span>
    <span class="nb">alias</span> <span class="nv">$1</span>
GI's avatar
GI committed
75
    <span class="nv">ealiases</span><span class="o">+=(</span><span class="si">${</span><span class="nv">1</span><span class="p">%%</span><span class="se">\=</span><span class="p">*</span><span class="si">}</span><span class="o">)</span>
GI's avatar
GI committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
<span class="o">}</span>

<span class="k">function</span> expand-ealias<span class="o">()</span>
<span class="o">{</span>
    <span class="k">if</span> <span class="o">[[</span> <span class="nv">$LBUFFER</span> <span class="o">=</span>~ <span class="s2">&quot;\&lt;(</span><span class="si">${</span><span class="p">(j:|:)ealiases</span><span class="si">}</span><span class="s2">)\$&quot;</span> <span class="o">]]</span><span class="p">;</span> <span class="k">then</span>
        zle _expand_alias
        zle expand-word
    <span class="k">fi</span>
    zle magic-space
<span class="o">}</span>

zle -N expand-ealias

bindkey -M viins <span class="s1">&#39; &#39;</span>        expand-ealias
bindkey -M viins <span class="s1">&#39;^ &#39;</span>       magic-space     <span class="c1"># control-space to bypass completion</span>
bindkey -M isearch <span class="s2">&quot; &quot;</span>      magic-space     <span class="c1"># normal space during searches</span>
</pre></div>


<p>The last three bindkey commands are assuming you are primarily using VI mode. If you&rsquo;re not, you might need to either remove the <code>-M viins</code> above, or replace it with <code>-M emacs</code>, etc.</p>
<p>Finally, to define an expandable alias use <code>ealias</code>.
The syntax of <code>ealias</code> is the same as that of <code>alias</code>
For example, I use</p>
<div class="codehilite"><pre><span></span>ealias <span class="nv">gc</span><span class="o">=</span><span class="s1">&#39;git commit&#39;</span>
ealias <span class="nv">gp</span><span class="o">=</span><span class="s1">&#39;git push&#39;</span>
</pre></div>


<p>Now typing <code>gc</code> in a position where zsh expands a command will appear like you typed <code>git commit</code>.</p>
    <br />
    <h3>
      <span class='glyphicon glyphicon-comment' aria-hidden='true'></span>&emsp;Comments
    </h3>

    <h4>
      <span class='glyphicon glyphicon-pencil' aria-hidden='true'></span>&emsp;Leave a comment
      <small class='text-warning'>
	(Spammers beware: All comments are moderated)
      </small>
    </h4>
    <div id='comment-failed' class='alert alert-danger hidden'>
      Sorry. There was an error submitting your comment. Please try again, or contact me if the problem persists.
    </div>
    <div id='comment-form' class='well row small'>
      <form role='form' method='post'
	  action='javascript:submit_comment()'>
	<input type='hidden' name='page' value='20140625-zsh-expand-alias.md'>
	<input type='hidden' name='comment-number' value='1'>
	<div class="col-md-6 form-group">
	  <label class="sr-only" for="name">Name</label>
	  <input type="text" class="form-control" name="name" placeholder="Name (optional)">
	</div>
	<div class="col-md-6 form-group">
	  <label class="sr-only" for="email">Email address</label>
	  <input type="email" class="form-control" name="email"
	    placeholder="Email (optional, to generate an avatar; will not be published)"
	    title="Your email is only used to generate an avatar. It will not be published or used to contact you. Promise!">
	</div>
	<div class='col-md-12 form-group'>
	  <label class="sr-only" for="subject">Subject</label>
	  <input type="text" class="form-control" name="subject"
	    placeholder="Subject (REQUIRED)" REQUIRED>
	</div>
	<div class="col-md-12 form-group">
	  <label class="sr-only" for="email">Comment</label>
	  <textarea class="form-control" name="comment"
	    placeholder="Comment. (REQUIRED. Write your comment in Markdown)"
	    rows='4' REQUIRED></textarea>
	  
	</div>
	<div class="col-md-12 form-group text-right">
	  <button type="submit" class="btn btn-default">Submit</button>
	</div>
      </form>
    </div>
    <div id='comment-sending' class='alert alert-info hidden'>
      Sending comment; please wait.
    </div>
    <div id='comment-success' class='alert alert-success hidden'>
      Thanks. Your comment was successfully submitted.
      It will appear here shortly if it isn't spam.
    </div>
  </div>
GI's avatar
GI committed
159
</div>
GI's avatar
GI committed
160
    <div class='col-md-3' id='col-right'>
GI's avatar
GI committed
161
      <div data-affix-after='#create'>
GI's avatar
GI committed
162 163 164 165 166
    

  <br />
  <h4><span class='glyphicon glyphicon-bullhorn' aria-hidden='true'></span>&emsp;Recent posts</h4>
  <ol class='list-unstyled'>
GI's avatar
GI committed
167
    <li class='hide-xoverflow'>
GI's avatar
GI committed
168 169
	<a href='http://localhost:8000/blog/20171208-bibtex-custom-label.html'>Using custom labels with the `alpha` BibTeX style</a>
	(2017-12-08)
GI's avatar
GI committed
170
      </li>
GI's avatar
GI committed
171
    <li class='hide-xoverflow'>
GI's avatar
GI committed
172 173
	<a href='http://localhost:8000/blog/20171207-keyboard-settings.html'>Swapping control/caps lock, and using Apple keyboards on Linux</a>
	(2017-12-07)
GI's avatar
GI committed
174 175
      </li>
    <li class='hide-xoverflow'>
GI's avatar
GI committed
176 177
	<a href='http://localhost:8000/blog/20171114-bibtex-doi.html'>Getting DOI / arXiv links with BibTeX.</a>
	(2017-11-14)
GI's avatar
GI committed
178 179
      </li>
    <li class='hide-xoverflow'>
GI's avatar
GI committed
180
	<a href='http://localhost:8000/blog/20171021-setup.html'>Static Jinja2 Site Generator</a>
GI's avatar
GI committed
181
	(2017-10-21)
GI's avatar
GI committed
182 183
      </li>
    <li class='hide-xoverflow'>
GI's avatar
GI committed
184 185
	<a href='http://localhost:8000/blog/20160406-dropbox-git.html'>Using git and Dropbox</a>
	(2016-04-06)
GI's avatar
GI committed
186
      </li>
GI's avatar
GI committed
187 188
    </ol>
    </div></div>
GI's avatar
GI committed
189 190 191 192 193 194 195 196 197
  </div>
</div>

    <footer>
    <div class='container'>
      <hr>
      <div class='media'>
	<div class='media-body'>
	  <p>
GI's avatar
GI committed
198 199
	    <span class='text-nowrap'>&copy; Gautam Iyer 2015&ndash;2017.</span>
	    This site is licensed under the <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/">Creative Commons BY-NC-SA 4.0 License</a>.
GI's avatar
GI committed
200 201 202 203 204 205 206 207 208 209 210
	  </p>
	</div>
	<div class='media-right'>
	  <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/">
	    <img alt="Creative Commons License" class='media-object' style="border-width:0" src="/share/licence/cc-by-nc-sa-88x31.png">
	  </a>
	</div>
      </div>

      <span class='text-nowrap' title='Email'>
    <span class='glyphicon glyphicon-envelope' aria-hidden='true'></span>
GI's avatar
GI committed
211
    gautam@math.cmu.edu.</span>
GI's avatar
GI committed
212 213 214 215 216 217 218 219 220
      &emsp;
      <span class='text-nowrap' title='Office'><span class='glyphicon glyphicon-briefcase' aria-hidden='true'></span> <a title='Wean Hall' href='https://www.google.com/maps/place/Wean+Hall,+Carnegie+Mellon+University,+Pittsburgh,+PA+15213'>WEH</a> 6121.
  </span>
      &emsp;
      <span class='text-nowrap' title='Phone'>
	<span class='glyphicon glyphicon-phone-alt' aria-hidden='true'></span>
	412 268 8419.
      </span>
    </div>
GI's avatar
GI committed
221
  </footer><script src="/dev/static/jquery/1.12.4/jquery.js">
GI's avatar
GI committed
222 223
      </script>
      <script src="/dev/static/bootstrap/3.3.7/js/bootstrap.js">
GI's avatar
GI committed
224
      </script><script src='/share/js/local.js'></script></body>
GI's avatar
GI committed
225
</html>