Live data from Hacker News

Keyboard shortcuts for GNU Readline

masteringemacs.org

111–120 of 174 posts

Re: Keyboard shortcuts for GNU Readline

#111

One thing I wish I knew how to do -- presuming it's possible -- is constraining the reverse/forward history search. Say I know I'm looking for a grep command, I'll do `C-r grep`, but then I want to look through that subset of results for another string (e.g., the pattern, or maybe another part of a pipeline that I half-remember). AFAIK, reverse/forward history search is just an exact string match...and remembering ex…

As another commenter said, fzf is the way to go.

Re: Keyboard shortcuts for GNU Readline

#112
post #12
post #10

Earlier quoted context omitted.

>by far the most useful shortcut for me is Ctrl-r I rarely ever use Ctrl-r since I usually know the starting part of the command I want. So, I type those starting characters and use up/down arrow keys, courtesy these lines in `~/.inputrc` "\e[A": history-search-backward "\e[B": history-search-forward

That's useful, but C-r has its merits too, as you can search for any string in the history. So, even if you run, say, python programs all day, here's a way to find that one time you used a different command line parameter... etc.

You can set bindings for substring search too:

    "\e[A":history-substring-search-backward
    "\e[B":history-substring-search-forward

Re: Keyboard shortcuts for GNU Readline

#113
post #15

"Shame, then, that even serious command line hackers never bother learning about its capabilities, as they can supercharge your command line productivity." Because it is really confusing if you are not an emacs user.

I'm a vim guy, and I don't find the readline commands confusing. I just think of it as "Insert" mode in Vim (many of the readline commands work there too!!) and it feels pretty natural.

Re: Keyboard shortcuts for GNU Readline

#114
post #10
post #6

I'm using bash (and thus GNU readline) all day every day at work, and by far the most useful shortcut for me is Ctrl-r (reverse history search). I also sometimes use Ctrl-a (start of line, if the keyboard doesn't have a convenient Home key) and Ctrl-e (end of line, in lieu of End key). Btw the post disses Ctrl-s / Ctrl-q for pausing/resuming terminal output, but it can be useful when you're outputting a lot of text (…

>by far the most useful shortcut for me is Ctrl-r I rarely ever use Ctrl-r since I usually know the starting part of the command I want. So, I type those starting characters and use up/down arrow keys, courtesy these lines in `~/.inputrc` "\e[A": history-search-backward "\e[B": history-search-forward

a really cool hack I found in an HN thread long ago was to stick "hashtags" (aka bash end-of-line comments) at the ends of commands you want to find in your history quickly:

  sudo systemctl restart myservice #bounce
and then ctrl-r and type your hashtag to get right to it

Re: Keyboard shortcuts for GNU Readline

#115
post #15

"Shame, then, that even serious command line hackers never bother learning about its capabilities, as they can supercharge your command line productivity." Because it is really confusing if you are not an emacs user.

I'm a vim guy, and I don't find the readline commands confusing. I just think of it as "Insert" mode in Vim (many of the readline commands work there too!!) and it feels pretty natural.

[deleted]

Re: Keyboard shortcuts for GNU Readline

#117
post #93
post #3

Just enable vim mode (`set -o vi`) and call it a day.

This is actually encouraged by POSIX. set -o vi: "Allow shell command line editing using the built-in vi editor. Enabling vi mode shall disable any other command line editing mode provided as an implementation extension." No other mode, specifically emacs mode, is included in the POSIX standard. Take from this what you will. https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V...

[deleted]

Re: Keyboard shortcuts for GNU Readline

#118
Two issues with readline: it's GPL and it's big. For embedded systems intended to become products you really need something else. My tiny variant is this, but for sure there are many others:

https://github.com/nklabs/libnklabs

("nkreadline" has editing, tab-completion (for embedded "nkcli" commands) and history)

Re: Keyboard shortcuts for GNU Readline

#119
post #10
post #6

I'm using bash (and thus GNU readline) all day every day at work, and by far the most useful shortcut for me is Ctrl-r (reverse history search). I also sometimes use Ctrl-a (start of line, if the keyboard doesn't have a convenient Home key) and Ctrl-e (end of line, in lieu of End key). Btw the post disses Ctrl-s / Ctrl-q for pausing/resuming terminal output, but it can be useful when you're outputting a lot of text (…

>by far the most useful shortcut for me is Ctrl-r I rarely ever use Ctrl-r since I usually know the starting part of the command I want. So, I type those starting characters and use up/down arrow keys, courtesy these lines in `~/.inputrc` "\e[A": history-search-backward "\e[B": history-search-forward

I don't want to discount how valuable this may be to you. However, for the average sysadmin, or developer who spends time in shells on many different machines, I would gently suggest that this is not a good idea. It's not a significant enough improvement over the way it works out-of-the-box to merit both (1) not getting used to Ctrl-r and (2) getting used to a different behavior for the arrow keys. Just my opinion, of course.

Re: Keyboard shortcuts for GNU Readline

#120

It's perhaps worth mentioning the smaller and more liberally licensed editline library if you want to embed the same line-editing functionality in your own CLI. It's been used in non-GNU Unixes for decades. https://troglobit.com/projects/editline/ See also the note at the bottom of that page about other versions of editline/libedit.

In a similar vein there's also Linenoise form the creator of Redis.

https://github.com/antirez/linenoise

Post reply on HN