TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Things you didn't know about GNU readline (2019)

168 点作者 meribold11 个月前

29 条评论

tpoacher11 个月前
Along the same lines, I find this little utility in standard linux repos to be amazing. Try it on ed: `rlwrap ed`.<p><pre><code> Package: rlwrap [...] Homepage: https:&#x2F;&#x2F;github.com&#x2F;hanslub42&#x2F;rlwrap Description-en: readline feature command line wrapper This package provides a small utility that uses the GNU readline library to allow the editing of keyboard input for any other command. Input history is remembered across invocations, separately for each command; history completion and search work as in bash and completion word lists can be specified on the command line.</code></pre>
评论 #40806202 未加载
评论 #40802890 未加载
评论 #40804868 未加载
olau11 个月前
Some years ago I helped diagnose an O(n²) bug in readline.<p>For years, I had problems pasting many lines of SQL in a psql session. It would slow down as it worked through the characters. One day the same happened in another shell, and it dawned on my that this was probably a problem with readline.<p>So I fired up a profiler and sure enough, something inside readline was recalculating something upon each character input with the work done proportional to all the characters already input.<p>I sent my findings to the mailing list, and Chet Ramey fixed it.<p>In the process, I discovered that wonky editing of long lines when resizing a terminal window was also fixable. When you resize, you change the line width, so readline needs to take that into account when it wraps the long lines. I think it did have code to do that at the time, but it just didn&#x27;t kick in for some reason. Can&#x27;t remember the details, but it does seem to work fine in psql today. That was another multi-year annoyance.
kstrauser11 个月前
It&#x27;s a relief seeing readline pulled in as a dependency for some command line tool: ahhh, there&#x27;s going to be at least a reasonable amount of command history and editing features available.<p>To remind yourself how much we take for granted, play with `psql --no-readline` some time and see how awful it is to lose the ability to up-arrow get the last query back, edit it, and send it again.
评论 #40802345 未加载
评论 #40805026 未加载
评论 #40805228 未加载
评论 #40801738 未加载
gumby11 个月前
^U in readline isn&#x27;t an emacs command (that keystroke does something else in emacs); it&#x27;s the old teletype protocol for line input.<p>So if you aren&#x27;t using readline, ^U deletes everything back to the beginning of the line. Readline preserves that because us pre-readline old farts have it wired into our fingers.<p>Before display terminals there were just printing terminals: IIRC # deleted a character and @ everything to the beginning of the line. This goes back to Multics and was carried forward in early releases of Unix at Bell Labs.<p>This is obviously pretty inconvenient when you have a display terminal but I am pretty sure that code remains in the bowels of the BSD TTY IO system at least and can probably be enabled. Maybe it even survived into linux for some compatibility reasons.<p>Also I WUZ THERE and I think Chet has it backwards on the POSIX controls issue: bfox talked about the TWENEX line input JSYS that had that stuff built in (I believe it was also, perhaps even earlier, in VMS, and maybe TENEX, and surely he had talked to some of those folks too)
chasil11 个月前
Debian dash is a minimal POSIX shell that also is able to implement &quot;set -o vi&quot; (as suggested by POSIX.2).<p><a href="http:&#x2F;&#x2F;gondor.apana.org.au&#x2F;~herbert&#x2F;dash&#x2F;" rel="nofollow">http:&#x2F;&#x2F;gondor.apana.org.au&#x2F;~herbert&#x2F;dash&#x2F;</a><p>The dash shell doesn&#x27;t use GNU Readline to implement &quot;set -o vi&quot; but instead uses &quot;libedit&quot;:<p><a href="https:&#x2F;&#x2F;thrysoee.dk&#x2F;editline&#x2F;" rel="nofollow">https:&#x2F;&#x2F;thrysoee.dk&#x2F;editline&#x2F;</a><p>Debian does not appear to link dash with libedit, so command line editing is disabled.<p>They should consider doing so, and offering dash as an interactive shell. This would give people fewer bashisms to unlearn when they need to write portable scripts for the POSIX shell.<p>The dash shell is actually reasonably pleasant for interactive use when compiled with libedit.
评论 #40804458 未加载
评论 #40806837 未加载
sodapopcan11 个月前
This is cool, I didn&#x27;t actually know you could configure Readline so the title is apt for me :D<p>Much like in the article I&#x27;m also a Vim user who excited to try Vim mode at the command line and I almost instantly hated it. I suppose it&#x27;s almost required to mention tpope&#x27;s rsi.vim[0] which gives you Readline bindings in insert and command modes. I&#x27;ve adopted Readline <i>everywhere</i> in my OS except in Vim normal mode which I believe is not too uncommon for many folks.<p>[0] <a href="https:&#x2F;&#x2F;github.com&#x2F;tpope&#x2F;vim-rsi">https:&#x2F;&#x2F;github.com&#x2F;tpope&#x2F;vim-rsi</a>
评论 #40805724 未加载
评论 #40801810 未加载
gray_-_wolf11 个月前
&gt; Also, without some sort of indicator, Vim’s modal design is awkward here—it’s very easy to forget which mode you’re in.<p>That is why I have this in inputrc:<p>set editing-mode vi<p>set show-mode-in-prompt on<p>set vi-ins-mode-string +<p>set vi-cmd-mode-string :
评论 #40802257 未加载
jlv211 个月前
&quot;Tens of thousands of people probably use it every day without thinking about it.&quot;<p>Probably tens of millions at this point.
klibertp11 个月前
One important characteristic of this `readline(const char*)` call is that it blocks. So, if you&#x27;re trying to write a REPL for a language that uses green threads for concurrency - I&#x27;m looking at you, GNU Smalltalk - then DON&#x27;T. At least not from the main thread.
mklein99411 个月前
&gt; Also, without some sort of indicator, Vim’s modal design is awkward here—it’s very easy to forget which mode you’re in.<p>With `bash`, you can show which mode you&#x27;re in by putting this in your `~&#x2F;.inputrc`:<p><pre><code> show-mode-in-prompt on </code></pre> It makes your prompt look like this:<p><pre><code> [foo@bar ~]$ # before turning it on @[foo@bar ~]$ # after @[foo@bar ~]$ set -o vi (ins)[foo@bar ~]$ # and after I press esc: (cmd)[foo@bar ~]$ </code></pre> Customize these with `emacs-mode-string`, `vi-ins-mode-string`, and `vi-cmd-mode-string`.
bbminner11 个月前
Bash uses readline, right? So<p>&gt; Tens of thousands of people probably use it every day without thinking about it.<p>seems like an understatement.
globular-toast11 个月前
&gt; One thing I’ve done is reconfigured Ctrl-K. Normally it deletes from the cursor to the end of the line, but I rarely do that.<p>I would recommend against this. I delete from the cursor to the end of the line <i>all the time</i>. Maybe more in Emacs than readline, but still<p>You can already delete the whole line by issuing C-a then C-k. I also do this all the time and that key combo is as easy for me to type as any two letter word.<p>Remapping C-k means you now have two ways to kill the whole line and no way to kill from the cursor.<p>But worse: you&#x27;re teaching yourself a keyboard setup that only you use. You&#x27;ll be lost on somebody else&#x27;s computer, or any time you don&#x27;t have your config installed. The standard readline bindings turn up in sometimes surprising places and it&#x27;s a joy when you realise you&#x27;re already trained to use some system or software you&#x27;ve never used before.<p>I&#x27;m surprised the article doesn&#x27;t also mention C-y. After you&#x27;ve C-k killed something it goes on to the &quot;kill ring&quot;. Press C-y to bring it back! (You can bring more than the most recent thing back, but you now have the key words to look this up yourself).
oska11 个月前
Lovely opening paragraph :<p>&gt; I sometimes think of my computer as a very large house. I visit this house every day and know most of the rooms on the ground floor, but there are bedrooms I’ve never been in, closets I haven’t opened, nooks and crannies that I’ve never explored. I feel compelled to learn more about my computer the same way anyone would feel compelled to see a room they had never visited in their own home.
strbean11 个月前
This is nostalgic. For a Systems Programming course, I had the following assignment:<p>- Implement readline(3)<p>- If you have any questions, see the man page.<p>- Extra credit: implement all GNU extensions.
评论 #40802042 未加载
meribold11 个月前
To make Less (the pager program) behave a bit more as if it used Readline (it doesn&#x27;t), one can create a `~&#x2F;.config&#x2F;lesskey` file with these contents:<p><pre><code> #line-edit ^A home ^E end ^F right ^B left ^P up ^N down ^D delete ^W word-backspace \ed word-delete \ef word-right </code></pre> I think a relatively recent version of Less is required.
评论 #40802080 未加载
jeffrallen11 个月前
Ctrl-k kills the rest of the line. Ctrl-u kills the entire line.<p>Ctrl-y yanks stuff back in from your kill ring. Esc-y will then replace the stuff with the previous item in the kill ring.<p>Ctrl-arrows moves a word at a time.<p>Ctrl-r starts a reverse interactive search through the history. Use any editing key, like ctrl-e to start working on the current search result.
评论 #40805106 未加载
uncletaco11 个月前
One thing trying to work with Guix over the last five years taught me is how much I take for granted all the little things that come packaged extra. I was using guix for about a month before I learned that readline was the package that allowed me to do a lot of the extra commands like ctrl-c etc.
meribold11 个月前
Some more good ideas for Readline configuration: <a href="https:&#x2F;&#x2F;www.topbug.net&#x2F;blog&#x2F;2017&#x2F;07&#x2F;31&#x2F;inputrc-for-humans&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.topbug.net&#x2F;blog&#x2F;2017&#x2F;07&#x2F;31&#x2F;inputrc-for-humans&#x2F;</a>
usr110611 个月前
The article feels incomplete for not not mentioning the bash built-in called bind. It allows you to work with the reeadline bindings.<p>I use Ctrl-R a lot to find previous commands from my shell history. Often I have to press Ctrl-R several or even many times to find the right one. And then it happens that I overshoot and would like to change the search direction. Unfortunately forward-search-history is on Ctrl-S, which is overloaded by flow control. Occasionally I use flow control, so I cannot remove that. What solution are others using to get forward-search-history?
评论 #40804011 未加载
评论 #40803141 未加载
galenelias11 个月前
Shout-out to the clink project, which brings a modern shell experience (including Readline) to Windows&#x27; cmd.exe. This is the most advanced shell I have across Mac&#x2F;Linux&#x2F;Windows, which is always a bit surprising. It&#x27;s extremely extensible and has some pretty incredible functionality written for it (also by the maintainer).<p><a href="https:&#x2F;&#x2F;github.com&#x2F;chrisant996&#x2F;clink">https:&#x2F;&#x2F;github.com&#x2F;chrisant996&#x2F;clink</a>
评论 #40802507 未加载
ramses011 个月前
Remiss for not mentioning &quot;rlwrap&quot;: <a href="https:&#x2F;&#x2F;github.com&#x2F;hanslub42&#x2F;rlwrap">https:&#x2F;&#x2F;github.com&#x2F;hanslub42&#x2F;rlwrap</a><p>...basically &quot;readline in a can&quot; that you can use to improve text input of other programs.<p>I forget if it was RMS or ESR that really wanted to hold &quot;readline&quot; away from corporations, so it&#x27;s licensed as GPLv3 (not v2), and he explicitly wanted it to _not_ be LGPL (ie: embeddable).<p>My crazy readline&#x2F;bash tip is `ESC, .` (or maybe `alt+.`, I&#x27;m not 100% sure, random search result here: <a href="https:&#x2F;&#x2F;gist.github.com&#x2F;tuxfight3r&#x2F;60051ac67c5f0445efee" rel="nofollow">https:&#x2F;&#x2F;gist.github.com&#x2F;tuxfight3r&#x2F;60051ac67c5f0445efee</a> ). Basically it up-arrows through the last &quot;word&quot; of previous commands, adding it to the current command.<p>Example:<p><pre><code> mkdir foo cd `esc, .` =&gt; cd foo vim bar.txt cp `esc, .` baz.txt =&gt; cp bar.txt baz.txt </code></pre> It&#x27;s crucial in that it reduces the number of keystrokes, and avoids typing errors completely, as you&#x27;re &quot;recalling&quot; something rather than re-typing.<p>I&#x27;m also in the same boat as the author in that `set -o vi` doesn&#x27;t quite do it for me (although I consider myself an expert in vi). What I tend to do instead is:<p><pre><code> C-a # &lt;cr&gt; =&gt; go to the beginning of the line and insert a comment char fc =&gt; &quot;fix command&quot;, see &quot;help fc&quot; (it&#x27;s a built-in) </code></pre> ...basically it invokes: `$EDITOR &quot;$( history | tail -1 )&quot;` and whatever you save&#x2F;quit will get executed as a command.<p>This lets me intentionally &quot;damage&quot; a command, but then get into `vim` to &quot;fix it up&quot; (eg: `dw, cw, df&#x2F;, etc...`). Remove the comment from the beginning of the line to execute it, or keep it there to take the command back &quot;out&quot; of `vim` so you can keep building it up.
评论 #40802985 未加载
评论 #40805809 未加载
评论 #40803033 未加载
TZubiri11 个月前
Some years ago I sent a bug report to the email listed at the end of a man page.<p>I don&#x27;t recall what the bug was, I also don&#x27;t recall why the video I sent showcasing the bug had my national anthem in the background. But it sparked a nice bug report exchange.<p>That he is doing this for no pay and for the passion of providing software to users all around the world fits.
geophile11 个月前
I was writing a command-line tool in Python, and wanted bash-like line editing capabilities. And then I realized that there was a Python wrapper for readline. Problem solved!
BeetleB11 个月前
Readline is good, but once I started using the xonsh shell, and prompt_toolkit, going back to Readline always feels a bit lacking.
anovikov11 个月前
Great thing, but it is GPL... there is libedit that&#x27;s LGPL and while isn&#x27;t a complete replacement, is good enough.
godelski11 个月前
<p><pre><code> &gt; Ramey has now worked on Bash and Readline for well over a decade. He has never once been compensated for his work </code></pre> I just want to remind everyone of the xz hack. The lesson here is that a few people maintain projects that are critical to our infrastructure. You may be familiar with the XKCD comic[0], but this makes it look like the unpaid single&#x2F;few maintainer critical projects are rare.<p>These people are generally unpaid and that has big ramifications. Sure, it makes them susceptible to bribes and more prone to hostile takeovers (however you view xz), but there&#x27;s more. It means these works are simply a work of passion, and they are working on them in their spare time. We all know here that that time is less and has lower priority than our work time. So you&#x27;re more likely to be rushed and make mistakes, or not be able to fix things fast enough, and so on. Is this really the way we want this infrastructure to be maintained? Startups have more stability than this and are more likely to pass the &quot;CEO hit by a bus&quot; rule[1]. Not to mention the difficulties of transitioning when the dev wants to retire or move onto something else. This does lead to issues, and while we&#x27;ve solved them in the past, that doesn&#x27;t mean we can&#x27;t do better.<p>Surely they deserve some compensation. We have 8 companies which are worth over a trillion dollars and certainly all 8 use readline. Are you telling me that these companies can&#x27;t collectively kick back $200k&#x2F;yr. That&#x27;s literally what Elon makes in 10 minutes if he gets 5% on his wealth. I find it hard to believe we don&#x27;t have enough money to fund possibly thousands of these projects (if there are that many, but there are likely that many people). It only takes Ballmer 16 minutes and there are 20 people who can do it in 30 minutes or less (~50 for 1hr). Collectively the top 10 make 230 million per day for doing nothing. I think we have the money. (remember, we&#x27;re talking about products that these companies are using. This isn&#x27;t just a &quot;pure donation,&quot; as these things have clear market value and the loss of the maintenance will result in these companies also losing money)<p>At the least, I&#x27;d like to remind everyone that the company you work for likely offers some matching donation. If you&#x27;re at one of the big tech, they do.<p>[0] <a href="https:&#x2F;&#x2F;xkcd.com&#x2F;2347&#x2F;" rel="nofollow">https:&#x2F;&#x2F;xkcd.com&#x2F;2347&#x2F;</a><p>[1] If your CEO, or any single specific employee, is hit by a bus and can no longer work, does the entire company also collapse?
1vuio0pswjnm711 个月前
IME<p><pre><code> socat readline prog </code></pre> always feels faster than<p><pre><code> rlwrap prog</code></pre>
apitman11 个月前
This is way off topic but 2 hours ago I was trying to figure out how to read a single line from stdio using Rust&#x2F;tokio and failed to figure it out in a reasonable time. It was infuriating.
评论 #40805424 未加载
评论 #40803229 未加载
hollerith11 个月前
That&#x27;s nice, but if GNU readline disappeared from my Linux install I probably wouldn&#x27;t notice.