156 lines
16 KiB
Plaintext
156 lines
16 KiB
Plaintext
|
|
Episode: 2453
|
||
|
|
Title: HPR2453: The power of GNU Readline - part 2
|
||
|
|
Source: https://hub.hackerpublicradio.org/ccdn.php?filename=/eps/hpr2453/hpr2453.mp3
|
||
|
|
Transcribed: 2025-10-19 03:19:03
|
||
|
|
|
||
|
|
---
|
||
|
|
|
||
|
|
This episode of HPR is brought to you by Archive.org.
|
||
|
|
Support Universal Access to All Knowledge by heading over to archive.org forward slash donate.
|
||
|
|
Hello everybody. Welcome to Hacker Public Radio. My name is Dave Morris.
|
||
|
|
Today I'm talking about Gnu Readline. This is show number two in what I imagine will be a series of maybe three or four episodes.
|
||
|
|
Number two is, well it starts off with a recap of number one which was July 2016.
|
||
|
|
In that episode I went through a number of the keys, the key sequences that you are probably aware of.
|
||
|
|
And maybe a few you're not, but just a few of them. I'm not going to do too many because it gets a bit tedious for too many.
|
||
|
|
Anyway I mentioned Control D as a way of deleting the character under the cursor.
|
||
|
|
You can use the delete key if you have one of those, one marked del or delete on your keyboard.
|
||
|
|
But at least that's what works on my keyboard. I'm not sure I can extrapolate to everybody but works on me.
|
||
|
|
But I mentioned it and it does do that thing and it does it fine.
|
||
|
|
It does a thing I didn't cover in that episode. If you have, if your cursor sat on an empty line, it's at the start of the line.
|
||
|
|
There's nothing else on the line. It's completely empty. You've not typed a thing. Control D sends an end of file signal.
|
||
|
|
It's usually written EOF in capitals.
|
||
|
|
Now if you are running a program or a script, then it could well cause that program or script to stop.
|
||
|
|
Or at least it signals that there's no more data if it's waiting for data.
|
||
|
|
If you're in a terminal, a terminal emulator, Control D will often just log you out of it. So it'll close it down.
|
||
|
|
So thought it was worth clarifying that. I tend not use Control D to delete stuff so I've forgotten at the time I did it.
|
||
|
|
There's a written recap in the longer notes for this. Just talking about the way we name the various keys that are used here but I won't go into that.
|
||
|
|
The other thing I wanted to raise was depending on what desktop you actually use, you might find some of the key sequences mentioning the read line manual.
|
||
|
|
And in this episode to be slightly different, I'm not quite sure what it's hard to do a server.
|
||
|
|
I haven't got all possible implementations of Linux. I haven't got all different types of keyboards and stuff here to test.
|
||
|
|
So just warning you, you need to just double check. One of the areas of confusion is between the backspace key and the delete key.
|
||
|
|
Certainly it was very, very confusing back in the early days of Unix and I used a lot of them.
|
||
|
|
Just a wee bit in many cases but I have actually used them. SonOS, Solaris, DomainOS, HPUX, etc. is a list here but I won't read it.
|
||
|
|
So I did point to an link which talks about the history of these things because they go back to the days of paper tape and had certain effects of over printing and deleting stuff in on paper tape.
|
||
|
|
So there are a little bit, I have been through a slightly odd history.
|
||
|
|
Anyway, the main subject I want to talk about today is doing things on the command line.
|
||
|
|
What we're really talking about here is an editor, a type of editor which is built into the command line that you use and that can be the command line of your shell.
|
||
|
|
Also be the command line that you're in if you've invoked say MySQL or some other tool, FTP or something from the bash or whatever command line.
|
||
|
|
And it also uses read line.
|
||
|
|
The term kill is used in the documentation which actually means deleting but also saving it away.
|
||
|
|
The corresponding term that's used is yank. It means reinsert the deleted text back again.
|
||
|
|
These terms didn't catch on I guess and they're certainly not consistent with what's with their use which they do still exist in said and vim.
|
||
|
|
But so I'm not going to use them. I might refer to them on and off but cut and paste is what's normally referred to these days.
|
||
|
|
What I did or cut or kill text is stored in a place called the kill ring. So you can't get away from this term totally.
|
||
|
|
And consecutive kills cause the text to be accumulated into one unit which can then be paste back all at once.
|
||
|
|
So if you're on a command line and you do a series of deletions then they're all going to be collected together and available to be reused.
|
||
|
|
As soon as you use a command that doesn't do any killing then that stops the collection of text relating to the last sequence of kills or cuts which makes sense.
|
||
|
|
When you do it it makes sense but when I say it doesn't seem to make a huge lot of sense.
|
||
|
|
So I'm going to go through a bunch of key sequences.
|
||
|
|
There's only six here which I'm going to just talk about briefly and then give some examples.
|
||
|
|
Let's go through that sequence of six sequences.
|
||
|
|
Control K which I've written down as capital C hyphen K in these notes but I've explained that last time.
|
||
|
|
What this does is it deletes text from the current cursor position to the end of the line.
|
||
|
|
So if you're somewhere you've typed some stuff then you've cursed back a bit and you realize you got it wrong and you want to delete from where the cursor is to the right hand end of the line.
|
||
|
|
Then control K will do that but it's a kill command so it's storing it away.
|
||
|
|
Meta D remember the Meta key is usually the alt key on your keyboard but if you haven't got one of those then you can use escape and then D.
|
||
|
|
Meta D is a shift type of mutti hold down the alt key and then press D but if you use the escape variant you type the escape and then you type the D.
|
||
|
|
Meta D deletes forward from the cursor to the end of the current word so that's assuming you're in a word that's why I said current word.
|
||
|
|
If you're between words then it deletes to the end of the next word going right that is.
|
||
|
|
Word boundaries are the ones that we talked about in the last episode when we looked at Meta F move forward a word.
|
||
|
|
Basically it's letters and numbers and things which are not letters and numbers are regarded as word delimitors word boundaries.
|
||
|
|
When you are in a word and you do a Meta D the space after the word isn't deleted.
|
||
|
|
The space before the word is only deleted if the cursor is already in that space so you can get slightly different effects so you can get rid of the double spacing you might get in a bit of text by doing that.
|
||
|
|
You can see how similar it is in many respects to doing these types of things in an editor if you're an editor user like Emax or Vim then there are similarities.
|
||
|
|
By the way these key sequences I'm talking about are effectively or at least they're related to Emax.
|
||
|
|
There is a series of control commands which you can do which will which are equivalent to Vim but I'm not really going to talk about them here.
|
||
|
|
I might add them in towards the end of this series but it's easier I believe anyway to use these.
|
||
|
|
So number three in my list is Meta Del which written as Meta Del meaning the delete key but Meta Backspace also works and I find that these don't let they aren't equivalent.
|
||
|
|
This is why I've had all this preamble about issues with different keyboards and stuff and also with desktops and whatever.
|
||
|
|
Anyway Meta Del or Meta Backspace will delete backwards from the cursor to the start of the current word so we've gone forward and deleted stuff now this is how you delete backwards.
|
||
|
|
If you're between words it starts it goes back to the start of the previous word and it's the same boundaries as we've talked about before.
|
||
|
|
Now when I do this on my keyboard I find this only works with Meta Backspace not as Meta Del or Delete.
|
||
|
|
It's really useful for if you're type in a command if you're type in a command with a filename in it it's really useful for deleting backwards through that filename in one chunk at a time.
|
||
|
|
The next one is Control-W what this does is it deletes backwards from the cursor to the previous white space.
|
||
|
|
So if you're in a great long filename with no spaces in it Control-W will delete the whole thing.
|
||
|
|
That was the one I learned first have to say but it's real pain if you've got something quite long and you just want to delete a few components of it so that's where Meta Backspace is an easier way of doing it.
|
||
|
|
But it's using white space not the boundaries that we've looked at so far word boundaries.
|
||
|
|
So there's bunch of things that delete stuff. Control-Y is the Y because it refers to a Yank but it's a paste sequence which pastes the most recently killed text back into the buffer out the cursor position.
|
||
|
|
So I talked about how as you delete stuff it's especially if you delete stuff and then do something else then delete some more stuff.
|
||
|
|
It's actually in what they're calling a kill ring which is to I think you could look at it as a stack but since you can't visualize it not as far as I know then it's a little bit difficult to appreciate.
|
||
|
|
But anyway the next command lets you do some stuff with this so-called kill ring and that's Meta Y and what that does is it rotates the kill ring.
|
||
|
|
It moves the topmost thing I guess to the bottom and then gives you the next one down in the list and then it will paste it.
|
||
|
|
But it only works if the command you just did was Control-Y or Meta Y.
|
||
|
|
So if you're in the domain of pasting stuff then Meta Y can be quite useful.
|
||
|
|
I've got an example in a minute or two appreciating that make doing examples in audio when you're talking about stuff on a command line is difficult.
|
||
|
|
The example to think hope anyway are explained in written form in the notes.
|
||
|
|
So let's get into the examples then.
|
||
|
|
So for example we have written the string we want to echo the string how many miles to Babylon.
|
||
|
|
The way I've written it is an echo and then these words how many miles to Babylon without enclosing quotes which is legal.
|
||
|
|
The echo will handle that not ideal but it makes it exactly as easy as I'm cheating slightly.
|
||
|
|
So if you did that you typed that and then you moved your cursor back to the M in miles.
|
||
|
|
And remember you can use the Meta B command that we looked at in the previous show to do this.
|
||
|
|
This is moved backwards by words.
|
||
|
|
And I've written it up here with the circumflex underneath the M to where the cursor key would be.
|
||
|
|
Then if you pressed Control-K the text from the cursor the cursor is on the M of miles to the end of the line is deleted.
|
||
|
|
So you're all left with this how many in a space.
|
||
|
|
If you then moved your cursor to the capital H of how many you could use Meta B again to do this.
|
||
|
|
Then you press Control-Y to paste it the text back.
|
||
|
|
You'd end up with the string echo space miles to Babylon how many.
|
||
|
|
There's no space between Babylon and how many because they were none when it got chopped.
|
||
|
|
You can see it's hoping to appreciate that you chopped the thing off the end and you stuck it on the front.
|
||
|
|
Not really well-chatteringly useful but you know if you just think to play around with just to get the hang off.
|
||
|
|
If this sounds interesting to you then I would suggest you do a bit of messing around with this sort of stuff.
|
||
|
|
The example 2 is a little bit longer and more complicated and it's using file names.
|
||
|
|
So I was thinking what I could use as a demonstration.
|
||
|
|
And I logged in as root and I started having a look at various files in the VAR log directory where all the log files are.
|
||
|
|
Now on my machine I've got Mosquito which is a tool to let you do communication between systems.
|
||
|
|
Small is a short message type thing.
|
||
|
|
I've talked about it in a previous episode and it's useful for IoT type applications.
|
||
|
|
But it writes a log and the log is in VAR slash log slash Mosquito with 2Ts slash Mosquito dot log.
|
||
|
|
So I'm just assuming that if you're using it you might want to do the thing that you tend to do with log files and tail them.
|
||
|
|
Sometimes I'm just going to see what just happened or what went wrong when you were developing something or whatever.
|
||
|
|
So normally you would have typed such a line by typing tail, the command let's look at the end of a file, space slash VAR slash LOG slash MOS.
|
||
|
|
And if you press tab at that point you will get that expanded to Mosquito.
|
||
|
|
To me you have Mosquito and Sonya and machine.
|
||
|
|
There'll be other instances of other files like Apache things that you can do similar sort of stuff.
|
||
|
|
Then in the case of Mosquito if you press tab again it fills in the rest because the only things in that directory will be files coiled Mosquito dot log and then a number and another suffix.
|
||
|
|
So Mosquito dot log is the first one that comes back when you press tab.
|
||
|
|
But I'm sure you know this, if you're a command line user you know tab will do all sorts of clever things with expansion of file names and that type of thing.
|
||
|
|
Now assuming you've hit the you've entered this command and you've looked at the output and you realize you want to look at something else then you might recall that line with an up arrow.
|
||
|
|
And it's in the VAR LOG directory you want to look at.
|
||
|
|
You decide you want to look at the system log. That's a file called syslog.
|
||
|
|
So you recall that tail command we just talked about.
|
||
|
|
If you press meta backspace three times, remember I talked about this meta backspace will delete words backwards.
|
||
|
|
So it will delete Mosquito dot log and then Mosquito.
|
||
|
|
So you end up with slash VAR slash log slash slash slash being a delimiter and it will stop the deletion.
|
||
|
|
Then if you type syslog you would get met with the command tails based VAR slash log slash syslog and there you'd be able to check the contents of that file.
|
||
|
|
Now maybe other files you want to look at and you can use a similar sort of technique of meta backspace to delete the last component and type something else in its place.
|
||
|
|
Now the last demonstration relating to this was if you were to delete that syslog element of the file name using meta backspace.
|
||
|
|
You could restore it with control Y as we've already spoken about. Control Y will just put back what was last deleted.
|
||
|
|
But if you then type meta Y which is the thing that rotates this kill ring or this stack of deleted stuff, meta Y you'd see the syslog component be replaced by Mosquito slash Mosquito dot log.
|
||
|
|
So in other words the Mosquito slash Mosquito dot log string got deleted even though you hit meta backspace three times.
|
||
|
|
It got deleted and assembled together and it's in this stack or ring and if you do meta Y you will pull it up from the second position in this stack.
|
||
|
|
So if you've been doing this or looking in this directory by typing stuff with tab of course to help you because you don't like typing like me probably.
|
||
|
|
And you then deleted bits which you can do with meta backspace and then looked at other things.
|
||
|
|
You can go back through this stuff and get back to a previous condition with this meta Y.
|
||
|
|
Because you'd be saying well why not just do a barrow to go back a command and you're quite right that would also do the job.
|
||
|
|
But I can't think of a more sophisticated example so I have to stick with that one.
|
||
|
|
But I do find myself using meta backspace a lot since I learned it existed a while ago now.
|
||
|
|
But I'm not used this control Y meta Y much but that's partly because I've only really absorbed its usefulness in sort of last few weeks.
|
||
|
|
So gradually as you appreciate the flexibility that these things offer you then hopefully you will find that
|
||
|
|
that you can incorporate them into your daily usage into your muscle memory as you use the command line.
|
||
|
|
Anyway I hope that makes the point that the new readlines actually got some quite powerful stuff in there.
|
||
|
|
And there's some worthwhile things to learn about there.
|
||
|
|
So that's all. Probably going to do a couple more of these before I stop.
|
||
|
|
And just to wet your appetite I was looking through this recently and discovered that there's a whole macro and type feature which you can set up using a config file.
|
||
|
|
So you can create your own key sequences which will give you shortcuts for example, point it a word, hit this key sequence and it will put quotes around it.
|
||
|
|
So I'm going to cover those in the next couple of episodes.
|
||
|
|
Okay, bye-bye.
|
||
|
|
You've been listening to Hacker Public Radio at Hacker Public Radio dot org.
|
||
|
|
We are a community podcast network that releases shows every weekday, Monday through Friday.
|
||
|
|
Today's show, like all our shows, was contributed by an HPR listener like yourself.
|
||
|
|
If you ever thought of recording a podcast then click on our contributing to find out how easy it really is.
|
||
|
|
Hacker Public Radio was founded by the digital dog pound and the Infonomicon Computer Club and is part of the binary revolution at binrev.com.
|
||
|
|
If you have comments on today's show, please email the host directly, leave a comment on the website or record a follow-up episode yourself.
|
||
|
|
Unless otherwise status, today's show is released on the creative comments, attribution, share a like, 3.0 license.
|