Table of Contents
- Why this post
- Before we begin | BASIC GOOD TO KNOW
- Making of our custom widget
- My words to you dear friend
- Refrences
Why this post
I was always wondering why many of emacs keybindings work in
terminal. Being dependent on key combinations ctrl+e
, ctrl+a
,
alt+t
, ctrl+t
(my favorite)… every time i am fooling around the
terminal, it felt so bad๐ฟ if someone is navigating around pressing arrow keys
โฌ
๏ธโฌ
๏ธโฌ
๏ธโฌ
๏ธ…so maaaaanyyyy times, swagging them these nifty shortcuts is so
fun. Until my friend/binaysakya ๐จโ๐ป told me you should write about
these things. I realized I know nothing what those bindings
really meant ๐ค
That’s how this post happened.
next candidate post, Alt +w , Ctrl + y, Ctrl +w , Ctrl + K
— เคฎเคฟเคฒเคจ เคฅเคพเคชเคพ (@thapakazi_) December 14, 2017
I would sharing my learning below more aliened to ZLE. I have copied the lines from the reference links below ๐๐๐
Before we begin | BASIC GOOD TO KNOW
Here we would look good to know theory that this post revolves around, skip๐ these theory at your risk.
Readline
Its a library used by bash and many of CLI programs to
edit/interact via command line. Readline uses Emacs
style
shortcuts as default, that’s why your Ctrl+a
,
Ctrl+e
… shortcuts works almost same in most of terminals, even the editors
derivations like sublime respects๐ the style. And yes, because of
this it super easy to jump along the text interface with such a
ease โบ๏ธ
ZLE
I have shifted to zsh like few years back, no speical reason still playing back and forth between bash and zsh. Just love the random theme from robbyrussell/oh-my-zsh.๐ till yesterday(22 dec 2017) when I found this thing called zle(ZSH LINE EDITOR) then I was like oh my… its so ๐๐๐
ZLE is a powerful line editor, what that means… ? Every shell provides some kind of editing capability although it could be basic stuffs provided by os like enter character, delete line, delete word. ZLE extends your capability to interact with your shell with custom functions and custom key strokes, with ease(will share it how in section below โฌ๏ธ).
Example you want to change some text you just typed in terminal to UPPER Case
. What you
can do with zle widget is just like in your editor:
sample demo of both modes please play along with your shell while you are going via this ๐ฃ
VI
Set your key-maps to vi with
bindkey -v
, and now you can use your terminal like your vii
:interactive mode andESC
to command line modeecho "hello world" [ESC] [b] [b] [gUw] [w] [gUl] [Enter]
[ESC] : switch to cmd line mode [b] [b] : jump back 2 words [gUw] : where gU: activate to uppercase widget : w: apply to a word forward [w] : jump a word forward [gUl] : apply uppercase to a first char only EMACS (default):
$ echo "hello world" [alt+b] [alt+b] [alt+u] [alt+c] [crtl+j]
alt+b alt+b: jump back by 2 words alt+u : UPPERCASE hello alt+c : Uppercase W in world
ZLE in simple words:
- ZLE is a bridge between you and your zsh shell0
- ZLE provides a easy interface to extend the capability of zsh with our custom functions a.k.a
widgets
- it’s scope is only within interactive shell
- comes with tons of default keybindings, supports both emacs(default),vi ~keymaps~(modes of editing, collection of keystrokes and interaction mode)
- comes with goodies like:
where-is
which help us quick look about keymapszle -la
: quick list all the widgets- I am still learning, i am liking many small things like
[alt+x]
Making of our custom widget
Lets write a widget to find the top 5 memory savages on my machine at this moment. you can simply copy paste the following on your running zsh or source it somewhere in your rc file.
[step1]: To start with lets define a function, that invokes ps with necessary formatting.
_exec_ps_top_5(){ echo "top 5 memory saveges on my machine..." PS_FORMAT=%mem,pid,ppid,cmd,comm,etime ps -e|sort -r|head -6 }
[step2]: define a widget
zle -N _exec_ps_top_5 # define a new widget bindkey '^@' _exec_ps_top_5 # bind it to key ctrl+@
[step3]: execute the widget
first check if widget is available or not
zle -la|grep exec_ps [alt+x] where-is [enter] _exec_ps[tab] # expected output: _exec_ps_top_5 is on "^@"
that means our widget is ready to use ๐
Just press
[ctrl+@]
, you will see sth like this:top 5 memory saveges on my machine... %MEM PID PPID CMD COMMAND ELAPSED 4.1 1223 549 /opt/google/chrome/chrome - chrome 13:32 3.2 415 1 /opt/google/chrome/chrome chrome 13:41 2.1 875 549 /opt/google/chrome/chrome - chrome 13:36 1.9 618 415 /opt/google/chrome/chrome - chrome 13:37 1.9 1743 549 /opt/google/chrome/chrome - chrome 13:18
๐๐๐ Congrats, yeah that’s what it feels like ๐
My words to you dear friend
Please go via the reference url below and explore for your self
what awesome things we can do with our shell. ZLE is kinda
simple&nice, lets make use of it to do tiny stuffs we repeatedly do
in our daily like maybe look after logs like tail -f log/*.log
,
start our docker containers, run our specs, yeah its could be
anything, I have so many alias,functions I depend on everyday, now
I would have fun binding them to keystores of my choice ๐
Future Notes: maybe I would try write about readline in days to come, still lot to learn ๐
Refrences
- highly recomended: A closer look at the zsh line editor and creating custom widgets
- Chapter 4: The Z-Shell Line Editor
- Arch wiki::ZSH
- Adam’s UNIX shell environment so many goodies to learn from