12 ms·
Things to Know About GNU Readline
- CJefferson 7y agoI have a love-hate relationship with Readline. While it is good once learnt, it's really hard to learn. For beginners it's not obvious there is anything to learn, and even if they do want to know, you have to basically be told to Google for 'Readline'. It would be great if it could somehow be made more discoverable.
- gumby 7y agoBack when it was written Emacs was pretty popular (well, it had been the default around the lab for about a decade). So users would use it without noticing. The same reason the standard text widgets of Mac OS support Emacs commands.
- TheGrassyKnoll 7y agoFor starters, how about: $ man readline
- colejohnson66 7y agoMan pages are sometimes cryptic. When someone is first learning something, having all the details thrown in their face can be quite off-putting, at least for me
- zeckalpha 7y agoinfo readline instead perhaps
- kwk1 7y agoThat can't really be "for starters", because it requires one to already know of readline in order to pull up the manpage.
- cat199 7y agoagree. it's pretty well referenced in most shell documentation (checked bash and pdksh).. but then, well, you have to be someone who reads shell documentation.. of course, if you're not someone who reads shell documentation, then, well, how would you find a better reference anyway..
- zeke 7y agoWe need a program that logs sessions and tells us where we could have saved keystrokes. I think something like the script command to record my session along with a list of rules to look for, so when I backspace 8 times to delete a word it suggests C-w. Or when I arrow to the end of line it suggests C-e. I've been thinking about this for the past six months. If anyone has suggestions, let me know.
- gumby 7y agoYou need to run deep q learning on your bash history
- choeger 7y agoMaintained by a single person. Let that sink in. How many developers and admins rely on readline for their daily work? How many critical installations world wide depend on it? As a contrast: How much revenue is generated with, and how many people work on, something as mundane as a chat application for mobile phones? Sometimes it is astonishing that our software eco system actually works.
- nicwolff 7y agoHe maintains bash too!
- DoofusOfDeath 7y agoMaintained by a single person, but I doubt that individual is irreplaceable as maintainer. One reason we get so much bang-for-the-buck with software is that the maintenance costs for a given software package aren't necessarily proportional to the number of places/times it gets used.
- chetramey 7y agoNobody is irreplaceable.
- heyoni 7y agoHmm...maybe it should be repackaged into a Saas product with a monthly, per user license!
- rhizome 7y agoAnd a cookie notification.
- ori_b 7y agoHow many people does it need maintaining it? It's not that big, and there isn't that much that needs change.
- 7y ago
- westurner 7y agoI map <up> to history-search-backward in my .inputrc; so I can type 'sudo ' and press <up> to cycle through everything starting with sudo: # <up> -- history search backward (match current input) "\e[A": history-search-backward # <down> -- history search forward (match current input) "\e[B": history-search-forward https://github.com/westurner/dotfiles/blob/develop/etc/.inputrc https://github.com/westurner/dotfiles/blob/develop/etc/.inpu...
- deeg 7y agoI do the exact same thing. This will probably be considered sacrilege by some but I also remap tab completion to cycle through all matches instead of stopping at the first ambiguous character: # Set up tab and Shift-tab to cycle through completion options "\e[Z": "\e-1\t" TAB: menu-complete # Alt-S is normal style TAB complete "\es": complete
- colejohnson66 7y ago> I also remap tab completion to cycle through all matches instead of stopping at the first ambiguous character Windows PowerShell does this and I love it. I don’t understand why people wouldn’t want this
- wruza 7y agoBecause instead of helping me enter a long filename by its distinctive parts, it forces flat mental read-decide-skip loop based on first few letters, which is slow and cannot be narrowed down without typing out additional letters by hand. It is like selecting from a popup menu through a 1-line window. If it presented a real popup for file selection, combining both worlds (tab to longest match, untab to undo, up-down to navigate), that would be great. But it doesn’t.
- nathanwh 7y agoFor anyone who isn’t already aware, fzf[0] does this exactly (and more!) https://github.com/junegunn/fzf https://github.com/junegunn/fzf
- jolmg 7y ago> The Readline API is much more extensive than that single function of course, and anyone using it can tweak all sorts of things about the library’s behavior. Library users can even add new functions that end users can configure via ~/.inputrc, meaning that Readline is very easy to extend. But, as far as I can tell, even Bash ultimately calls the simple readline() function to get input just as in the example above, though there is a lot of configuration beforehand. I feel that was designed backwards. Instead of having facilities so applications can extend readline by adding their own line editing functions specific to them, it should have facilities so users can add their own extensions and make them applicable to all applications that use readline. That would be cool. In particular, I would like to be able to extend it to add better multi-line editing support like what zsh's zle has. In zle, I can use `o` and `O` to create lines below or above the cursor, `j` and `k` to move to the next or previous line, `>>` to further indent the current line, etc. It's basically a mini-version of a vi buffer, right in the command line. In readline, you can insert a newline in the current prompt with Ctrl-V Ctrl-J, but to move between the lines, I have to move left or right beyond the beginning or end of the current line. Even worse, readline's history format doesn't support having a single entry for multi-line commands, like zsh does. :(
- aidenn0 7y agoAlso, according to RMS, CLISP is a derivative work of readline because of the optional dependency on it. (CLISP is now GNU CLISP, but was not originally part of the gnu project).
- mikekchar 7y agoImagine you are a photographer. You take a photo of something. The photo is covered by copyright. I can't use it without your permission. Now imagine that I write a book and I include your photo in it. This is an infringement of copyright. I can't distribute my book, containing your photo, without infringing copyright. It doesn't matter how much your photo contributes to my book -- including it in my book and distributing it is infringement. You might even be able to get an injunction to stop me from distributing my book without removing your photo. Now imagine that you put your photo under a CC "share alike" license. What it says is that, while I don't generally have the right to use your photo in my book, if I decide to license my book using a CC "share alike" license, then you will give me permission to use your photo. To make things simple, you give blanket permission to anyone to use the photo as long as whatever is including the photo is licensed as CC "share alike" -- in that case it is not necessary to get specific permission to use it. Finally, let's say that you make the offer that if I infringe your copyright by including the photo in my project that you will forgive that infringement if I agree to re-license the project under CC "share alike". Exactly same thing.
- sokoloff 7y agoImagine instead that you sold a book of pictures with a few blank spaces/pages where a book owner could optionally put other pictures in that space. That book, standing alone, does not infringe on others' copyright, which (IMO) is closer to "exactly the same thing".
- mikekchar 7y agoYeah... I realised that later. I've added my opinion on that situation in a sibling post.
- 7y ago
- westurner 7y agoIs this macro from the article dangerous because it doesn't quote the argument? Control-j: "\C-a$(\C-e)" I can never remember how expansion and variable substitution work in shells.
- dan-robertson 7y agoThe macro means: \C-a: beginning of line $: self-insert (: self-insert \C-e: end of line ): self-insert So if your prompt (with | for cursor) looks like this: grep 'blah blah' |some_file And you execute that macro, you get: $(grep 'blah blah' some_file)| Which is correctly quoted and I think always correctly quotes whatever is at the prompt (unless you e.g. get half way through a string and press enter so the beginning of the prompt is halfway through a string, or maybe if you have multiple lines)
- westurner 7y agoYeah, but this and this do different things: # prints a newline echo $(echo "-e a\nb") # prints "-e a\nb" echo "$(echo "-e a\nb")"
- gumby 7y ago> Ramey told me via email that Readline, far from being an original idea, was created to implement functionality prescribed by the POSIX specification, which in the late 1980s had just been created. This actually isn't true. The POSIX shell spec (1003 IIRC) was published in 1992. The headline library was in the first bash release; Brian might have written it before starting on bash. The inspiration was the TOPS-20 input JSYS (system call) mixed in with Emacs. How do I know? Well you can ask Brian; as he reminded me it came from design discussions for bash I had with him on this very topic at the time, specifically that interactive text input should be a library.
- inflatableDodo 7y agoShould we all ask him individually? Is there some kind of batch process.
- brianjhanfox 7y agoI knew I was going to write readline, and I knew I was going to write it as a library. I wrote the vast majority of it on a weekend after I had a working shell. POSIX was mostly me telling David Korn that just because he had created a shitty user interface feature, we shouldn't be required to make that part of the specification.
- brianjhanfox 7y agoAnd I trust Gumby's memory almost to a fault :-) I had already written a version of Emacs for the Apple ][ and Apple //e called Amacs in 1983 or so. I loved Emacs and Zmacs, and wanted to build software that had that clarity of architecture. So I knew I wanted readline to have as much of the feature set of a genuine editor as made sense. I think Gumby was instrumental in talking to me about making a reasonable API for the completion part of readline. But I wrote a lot of code in the late 80s. And I wanted to use command line editing and completion in GDB, because we didn't have it. And I thought that was idiotic.
- inflatableDodo 7y agoHi, any thoughts on what would be a good place to start if someone was looking to write a terminal shell variant these days? Also, many thanks for all the useful things. :)
- stock_toaster 7y agoAnother thing you may not have known, libedit[1] is mostly readline compatible, and BSD licensed. [1]: https://thrysoee.dk/editline/ https://thrysoee.dk/editline/
- aasasd 7y agoWonders of Readline become painfully apparent when you have to use a program that doesn't employ it despite having a line-by-line command input interface. But! By some stroke of luck and computer magic, Readline can be injected right into the program, at least in Unixes: there's an util called `with-readline` for that. Some programs have the editing features but lack command history for some reason, or don't store it between sessions―this is also solved by the addition of Readline.
- unmole 7y ago> Readline can be injected right into the program, at least in Unixes: there's an util called `with-readline` for that. Damn! I knew this 6 months ago. I ended up writing a Python wrapper to add readline capabilities for a tool @work.
- msarnoff 7y agoAnd if you ever find yourself using a command-line utility that doesn't have readline support (`tclsh` and `smlnj` come to mind), you can use `rlwrap`, available via your package manager of choice: rlwrap tclsh and voila, no more `^[[C^[[D` spew.
- davidw 7y agoSigh... it always frustrated me that the Tcl folks never got something like readline integrated. IDK about the legal details, but Ruby and Python somehow managed. tkcon is an alternative, graphical shell for Tcl that's a good alternative to using tclsh on the command line, BTW.
- kevin_thibedeau 7y agoTclsh is only supposed to be a demo for embedding the interpreter. It consists of only 150 SLOC. Anyone could add readline if they really needed it. https://github.com/tcltk/tcl/blob/master/unix/tclAppInit.c https://github.com/tcltk/tcl/blob/master/unix/tclAppInit.c
- davidw 7y agoThis is like software marketing 101: You're going to have much happier users if you do that thing that "anyone could do" rather than ask that each person who might want to use readline read up on Tcl's C API and hack together something that links to readline.
- segmondy 7y agoI came to say this. rlwrap is so good. I honestly have never bothered using readline since I found rlwrap. I just wrap my command with it and move on.
- hyperpallium 7y agoThe first thing to know about rlwrap is that rlwrap is awesome.
- 7y ago
- unmole 7y agoSee also, linenoise[0], a small self-contained alternative to readline and libedit written by antirez who also wrote something called Redis. 0: https://github.com/antirez/linenoise https://github.com/antirez/linenoise
- derekp7 7y agoGot a question, what is the standard workaround to the fact that Readline's forward search (Crtl-S) is also XOFF (effectively pauses the terminal)? Other than turning off software flow control via stty -- I'd like to keep that on.
- yjftsjthsd-h 7y agoRemap it?
- Asooka 7y agoCtrl-S is XON. Ctrl-Q is XOFF.
- nineears 7y agoCtrl-U doesn't delete everything before the cursor. It moves everything before the cursor into the cut buffer.
- brianjhanfox 7y ago"cut buffer" is actually "kill ring" C-u only does that by default so that readline wouldn't confuse *nix users.
- gray_-_wolf 7y ago> Also, without some sort of indicator, Vim’s modal design is awkward here—it’s very easy to forget which mode you’re in. Luckily for us, there is option for the indicator :) set editing-mode vi set show-mode-in-prompt on set vi-ins-mode-string + set vi-cmd-mode-string :
- laktak 7y agoI prefer to set the cursor shape ;) https://stackoverflow.com/a/42107711/52817 https://stackoverflow.com/a/42107711/52817
- soyiuz 7y ago~/.inputrc set show-mode-in-prompt on set vi-cmd-mode-string "cmd:" set vi-ins-mode-string "ins:"
- Koshkin 7y agoWe used to have a simple line editor so we could edit a text file. Then GNU came along and gave us a wondrously complex editor so we could edit a single line... From the man page: Bugs It's too big and too slow. GNU software at its finest.
- xvilka 7y agoWe (radare2 project) recently added more readline-like features in our own implementation called dietline. We cannot use the readline itself due to portability issues but tried to implement the most useful features of it. See the basic documentation in radare2 book[1]. And of course, any contribution[2] to improve our implementation is welcome. [1] https://radare.gitbooks.io/radare2book/content/basic_commands/dietline.html https://radare.gitbooks.io/radare2book/content/basic_command... [2] https://github.com/radare/radare2/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+dietline https://github.com/radare/radare2/issues?utf8=%E2%9C%93&q=is...
- egwynn 7y agoOh man, it kinda hurts to see someone remapping `C-o` like this… That’s one of my favorite readline bindings! EDIT: From the bash man page: > operate-and-get-next (C-o) > Accept the current line for execution and fetch the next line relative to the current line from the history for editing. Any argument is ignored.
- saagarjha 7y ago> He said that millions of people probably use Bash without realizing it (because every Apple device runs Bash) iOS devices do not ship with bash.
- chetramey 7y agoAt least the last time I looked at a jailbroken iOS device, it opened a terminal running bash-3.2 (the same version as on macOS). It's there, you just can't get to it.
- saagarjha 7y agoMost "packaged" jailbreaks drop a shell binary into /bin, or outright install packages for things such as Bash and the GNU Core Utilities; iOS does not actually ship with these. (If it did, I would assume that the GPL would require that Apple provided source code for it here: https://opensource.apple.com/release/ios-1141.html https://opensource.apple.com/release/ios-1141.html .)
- chetramey 7y agoI don't buy it. First, why would a Linux-based jailbreak install a binary for bash-3.2, when that version is going on 13 years old? Second, there's nothing that requires Apple to make the GPL sources available in both MacOS and iOS. Doing it once is sufficient. I'm sticking with what I've seen and been told: the guts of MacOS are there, just hidden.
- saagarjha 7y ago> First, why would a Linux-based jailbreak install a binary for bash-3.2, when that version is going on 13 years old? Because nobody updated coreutils on iOS for years, AFAIK. FWIW, the unc0ver jailbreak installs bash 5.0.3. > Second, there's nothing that requires Apple to make the GPL sources available in both MacOS and iOS. Doing it once is sufficient. Why does the GPL not require that they release their modifications if they distribute them? > I'm sticking with what I've seen and been told: the guts of MacOS are there, just hidden. $ find -E /Volumes/PeaceG16G102.D321OS -regex "(ba)?sh" $ ls /Volumes/PeaceG16G102.D321OS/bin df ps (That's the iOS 12.4.1 restore image for iPhone XS, if you're wondering.)