[Music] hi everyone today I want to show you how you can use Zell line Editor to do some cool stuff with command line so let's start um first of all if you haven't used Zell I have a video introducing you to Zell you should see it somewhere above now and in my videos so what is the l e it stands for Zell line editor and this is an core editing engine in Zell itself it enables us to do things like manipulate command itself text in the command buffer history and so on another thing we're going
to talk about are custom widgets uh this is those are essentially functions that extend Zell functionality and we are going to build one of those ourselves um we can also use pattern expansion which Zell differently to bash has much more powerful uh pattern expansion braces expansion and so on again all those basic concepts are in the video that I mentioned earlier so the whole idea here is to be able to manipulate the current buffer what does it mean imagine that you have a command something simple like LS T lha and this is your buffer so
this inside of zle is captured inside of a buffer variable so here on the right hand side now there are other variables that are useful to us there's many more than what I'm showing you but those are enough for what I want to show you with using Z automate command so first we have left hand side of a buffer if you imagine that the cursor is where the space is right now between LS and the arguments then the L buffer will equal LS C csor position will be where our cursor is this is a number
and the right buffer will be from our cursor position towards the right so space t l a h so knowing this we can create widgets and functionality that manipulates our current buffer now why would we want to do it so taking advantage of the widget system means that you can have a customizable functions that enhance the Zell command line editing in order to list all the widgets you can execute zle T LA and you will see all the widgets let's give it a try set T LA and you can see there's a lot of various
widgets some of them for V some of them others that's the easy way to see them now as I mentioned earlier you can Define your own widgets bottom you can see an actual function from my inial RC file where I Define a function and I'm creating a widget right here at the bottom that essentially has the same name as a function and then I'm binding this widget to a shortcut in this case control alt o so you can see how widgets can be created so we have so far learned about the current line and various
variables that c uses to capture what you type and then widgets that are essentially invocable functions that you can bind to H shortcut so this is one way of doing it let's look in depth how the widget flow looks like so having the widget defined earlier as you've seen a moment ago let's come back to this real quick so the widget is called open file G staged and it invokes this function right here and it's binded to this key so whenever the system detects the trigger key in this case it would be Control Alt o
then the widget is being triggered and the widget has an expansion function it checks for whether the P pattern is matched in the case that I showed you earlier it's just a simple call to a function but we're going to see how we can manipulate the actual buffer so if you imagine having a typed something then the widget has access to this and then you can manipulate it in line before pressing enter so when the pattern that we are looking for is found then we can um apply any function in our case what we are
going to do we are going to apply um pattern that I called abbreviations it's not an official official term it's simply an abbreviation that expands to something else so let's see this in action so you have better understanding how it works here you can see on the left hand side I have a numbers file that is unsorted and it's called numbers txt so if I want to sort it I can do sort and numbers and then as expected you would see the numbers sorted all right if I want to sort file in place well I
have to do a little bit of typing because we cannot actually use sort to sort sort the same file so we would have to redirect the output to a different file and then pull it back to our original file or move them rename them by moving them to make this thing a little bit easier there is a command that belongs to core ues package I have made a video about it so you can watch it it should be somewhere above right now and sponge is quite neat utility that enables you to manipulate files in place
so I can use sponge in order to change the file in place so let's do this so if I do this I can you can see from history sponge and when I hit enter this file has just changed there was an empty line at the bottom so it reverted the whole order okay so where is the Zell here so now if I want to you can see that I had to type again PPE sponge and again the name of my file that I'm sorting in this case but wouldn't it be nice if I can have
abbreviation that would essentially expand including my file so that's where our system comes into play so I can type underscore that's how I start my abbreviations and then SPO and when I hit space you can see that we essentially expanded to the same command as you've seen earlier and it would behave in the same way okay so that's useful it saves maybe 10 seconds of typing but you can of course see that in this way you can do different things I have a few other abbreviations that I can show you let's imagine that I want
to copy this numbers file and make a backup of it I can start doing CP then numbers but now I have to come up with the backup name again it's not that difficult but I can do another obliviation b a key b a k and when I hit space you can see that the CP command transforms and it takes the same file and then it uses isal expansion to add the backup suffix so you can imagine how this can function when you type something in the command line and instead of editing the command line going
into Vim edit mode or just going back and changing parameters you can essentially have those suffixes or abbreviations as I call them and do this much easier so since we've seen how this works let's see how is it put together together so the whole script is very simple the first part is essentially a dictionary that's called in dell and Bash associative array and you can probably imagine What's Happening Here we have abbreviation which we used SPO B AK and I have a few others and we have a corresponding function so this simply matches the abbreviation
to a function we are creating a diction AR of our abbreviations so in future this is easily expandable and we can keep adding new abbreviations and corresponding functions so let's look at the sponge expand abbreviation first we Define internal local variable that's captures the left buffer so this is the left hand side of what we are typing and if you remember switching back quickly to our example here if I do CP and then I type a file so in this case numbers my cursor is right here at the end of the command so after a
space so everything from my cursor towards the left is actually the L buffer in this case we could also use the whole buffer because that's everything but we're essentially capturing it into variable now since files cannot have spaces we can safely split everything into words so we take the whole left buffer and in our case those are two words CP and the name of the file and the last last part is the variable that uses again a powerful Zell expansion system that captures all the words and then gets only the last word so minus one
is the array we are indexing an array and we are capturing the last word those are Z specific you can learn about them and they are really powerful and the last one is essentially outputting to the screen the P left buffer and then appending sponge with the last word which as you've seen earlier results in our abbreviation all right so how does it how is it put together so since we have this function that essentially expands to whatever we type so copy file name and then it adds pipe operator and then sponge with the same
file name so this is the engine a very small lightweight engine of the whole solution we have a function called expand abbreviation and this function essentially stores the pattern we try to match and it when executed it says oh is the local buffer matching the name of the pattern so in other words when we are typing this so underscore spoe this function will take everything on the left hand side so spoe is our name of the function and using this rather complex looking RX would trim everything except of the last argument and thus the buffer
will equal our much so then it says if we found this in our abbreviation associative array which is our dictionary then we are simply executing it so we are calling abbreviation and our mat so that's a very simple abstraction over how we can create more and more abbreviations so we don't need need to change this engine this engine will always accommodate new obstructions finally at the beginning I said you can create your own widget that's exactly what we are doing here with minus capital N we are creating our own widget which is called expand abev
one more time this is this function and now we are binding space to triggering it so as You' see me doing here when I hit space suddenly everything changes and the new command replaces thecore spoe abbreviation we could add here tab or a combination of keys but I thought for me space works well the last element is a bit special it's if you would go into history and would like to find the command inside the history which the I search uh is for and hit space there then um we would essentially bind to this widget
which we don't want so the last line of this code takes care of exactly this so let's look real quick at the patterns themselves so the percent percent and then #t M this removes the longest match of a pattern from the end of a string and M enables group matching so it is a Zell Rex engine uh Zell uses Pearl Rex engine and then it enables here us to capture the group the second one that we've seen is a character class so we are capturing underscore and characters from A to Z and so on and
the last one is our dictionary call so we've seen how this can be useful I am using this way of creating abbreviations for a while and it really helps me with my flow I'm going to show you the whole script so you can see here parts of this we've seen this is for example a different one here we have into a new directory which essentially creates a directory before executing a command and it follows a very similar pattern here all of those are Zell expansion patterns and they are really nice to know this is as
always available in my DOT files if you want to take it uh and play around with it so what are the best practices of doing it first of all it's important to keep patterns memorable especially if you have a lot of them you can also add autoc completion so if you hit tab or some other key they can autocomplete that's possible I didn't have a need for it yet consistent naming is is important so in my case it starts from underscore so I'll remember those are abbreviations and um document them with comments or however you
want it's important to test edge cases because sometimes times those abbreviations are meant to work with different commands that's the whole power of those so in case of creating a directory before a command it has to work with with tar it has to work with copy with move and so on and so forth so there are some test cases edge cases need to work and always consider in what context you're using those let's look at some resources so the First Resource that is important is the man page of zsh itself uh if you want to
learn about sponge again you can watch my video about sponge or also have man cor utils and then um another Another zsh Man pages so how can we access those easy here I have a very simple uh alas that I use instead of uh man so it's a very simple function as you can see here it just uses fzf and it searches Man pages with whatever I put it there and then it uses neovim instead of regular pager to display the output so let's try if I type man instead of usual output I am seeing
now all the fzf and I can say Z zsh Le and that's man page when I hit enter it shows me everything in Neo which is much nicer to search and um a widget widgets for example you know I can I can go and um read about it so this is just an easier way of doing it same the same could be with coru or more U is for example a parallel command from more U so that's just a way how you could use the man man page again this function is in my do files
as everything I encourage you to read especially about the widgets and how set works it offers a lot of power if you're a z user and I feel it's underutilized um in typical workflows so that's it for today um as always I hope you enjoy the video thank you for supporting the channel thank you for watching and see you in next video [Music]