12 ms·
Keyboard shortcuts for GNU Readline
- djaouen 4y agoI didn’t know readline had macros! Useful!!!
- kitclen 4y agoJust enable vim mode (`set -o vi`) and call it a day.
- djaouen 4y agoCan I still use macros with 'q<register>'?
- tsm 4y agoBased on 30 seconds of experimentation, no.
- bippingchip 4y agoYou can edit the current prompt in a full vim by pressing ‘v’. On :wq the content of the file will be run as the command. In that case you can use the macros with q (and any fancy vim plugin you might like)
- bippingchip 4y agoYes! I never got the hang of the read line short cuts. Only one I remember and regularly use is reverse history search: ctrl-R. The downside is that while vim mode works nicely on bash, other commands like gdb etc that also use read line aren’t as easy to get into vim mode, if they support it at all…
- gen220 4y agoime, most programs using readline parse ~/.inputrc. set editing-mode vi set keymap vi-command That does the trick for me in python shell, psql, etc. First thing I do on any fresh unix box is setting above, and echo "set -o vi" >> ~/.bash_profile.
- tristan957 4y agoFor programs which use libedit, you can also setup vi mode. bind -v
- gen220 4y agoGood one! That covers sqlite3, it was my last straggler. :)
- cassepipe 4y agoCan't remember why but I am pretty the second line is not needed
- cassepipe 4y agogdb also uses readline... And readline is configured with ~/.inputrc Here is my config from a happy gdb vim mode user : set editing-mode vi # If set to ‘on’, Readline performs filename matching and completion in a case-insensitive fashion. The default value is ‘off’. set completion-ignore-case on # If set to ‘on’, Readline displays possible completions using different colors to indicate their file type. The color definitions are taken from the set colored-stats on # Perform partial (common) completion on the first Tab press, only start # cycling full results on the second Tab press (from bash version 5) set menu-complete-display-prefix on ### MAPPINGS ### # Allow to refresh the screen "\C-l":clear-screen # If there are multiple matches for completion, Tab should cycle through them TAB:menu-complete
- BaculumMeumEst 4y agovi bindings aren't worth running into weird problems imo. it took me less than a minute to hit an incompatibility with fzf. https://github.com/junegunn/fzf/issues/1238 https://github.com/junegunn/fzf/issues/1238
- Sakos 4y agoThis is it for me. I'd use vi bindings everywhere if I could and it didn't end up breaking other things.
- distantsounds 4y agoshhh, don't tell them there's a superior text editor out there, it's fun watching emacs users consistently do things the hard way
- soheilpro 4y agoAnd add search support, if you're using zsh: https://github.com/soheilpro/zsh-vi-search https://github.com/soheilpro/zsh-vi-search
- chasil 4y agoEvery vi mode that I have ever used in any shell allows search with ESC-/ as the vi editor itself allows.
- chasil 4y agoThis 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/V3_chap02.html https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V...
- deleted 4y ago[deleted]
- ataylor284_ 4y agoNot surprising. Back in the heyday of POSIX standardization, the Emacs camp and the POSIX camp were both very opinionated and not entirely aligned. One example is the standard disk block size used in command line utilities such as du and df. RMS made GNU use 1kb instead of the POSIX standard 512 byte size, but this could be overriden by setting the environment variable POSIX_ME_HARDER.
- Xophmeister 4y agoOne 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 exact strings is hard.
- loloquwowndueo 4y agoFzf can probably help here : https://github.com/junegunn/fzf https://github.com/junegunn/fzf
- deleted 4y ago[deleted]
- jlokier 4y agoI'd use "history | grep ..." at that point, if I can't remember an old command. Possibly "history | grep ... | less" and finish searching inside less.
- d4rkp4ttern 4y agoI was doing history grep too until someone on HN told me about hstr: https://github.com/dvorka/hstr https://github.com/dvorka/hstr
- uneekname 4y agoAnother similar tool that may be of interest is mcfly https://github.com/cantino/mcfly https://github.com/cantino/mcfly
- BeetleB 4y agoAs another commenter said, fzf is the way to go.
- senko 4y agoI'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 (for example tailing a logfile).
- asicsp 4y ago>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
- kleiba 4y agoThat'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.
- heleninboodler 4y agoa 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
- riwsky 4y agoKnowledge like this used to spread naturally—physically sitting with a junior engineer, muscle memory would make any shortcut-fu gaps painfully clear. That’s not as visceral in remote pairing, and doesn’t show up at all in async processes like CR. Still, I try to at least share the “greatest hits”: ctrl-r: search backwards through command history ctrl-a: go to beginning of line ctrl-e: go to end of line If nothing else, it’s a topic to keep us occupied while they hit the left arrow 47 times instead of ctrl-a.
- wdfx 4y agoIn ~20 years of professional programming, I have never explored much beyond these shortcuts, except for also using: * ctrl-left/ctrl-right to go back/forward word by word instead of char by char * ^C to cancel the current line
- sillysaurusx 4y agoOh my gosh thank you. I once went back by a word and swore I hallucinated, then realized the shortcut probably exists but never bothered to look into it.
- fiddlerwoaroof 4y agoThe emacs shortcut works here too: Alt-b for back a word and Alt-f for forward a word (at least if your terminal maps alt correctly)
- chaosite 4y agoIf we're counting ^C, then I'd also add: * ^D - Send EOF, which exits most shell-like programs (if you're on an empty line, so try ^C^D) And the job control bindings: * ^Z - Suspend current foreground process and return to shell, use `fg` to resume it in foreground and `bg` to resume it in background (see also `disown`) * ^S - "Stop" current process, useful if it's printing a lot of stuff and you want to pause it for a bit, otherwise useless and usually done by mistake * ^Q - resume a stopped process, probably after you ^S'ed by mistake
- kqr 4y agoHuh, by this account I'm apparently a fairly advanced readline user, but I still have some way to go! Thanks for sharing.
- stuaxo 4y ago"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.
- motoboi 4y agoNot at all. bash readline is probably the productivity enhancer skill a developer (or system admin) can develop. Simple things like copy and paste without hitting a mouse. This will make you _prefer_ the cli as it is not slowing you down anymore.
- Too 4y agoYes please. Teach me how to copy output of previous command into clipboard using keyboard only and I will love you forever. Bonus if I can have a cursor to only select partial lines of the output.
- manx 4y agoMany terminals have a different mode, where you can navigate and copy everything displayed in the terminal. Alacritty can do this, for example.
- znpy 4y agoUnrelated, but you could easily do that on screen with C-a ESC (default key binding)
- jbaber 4y agoYou can pipe to xclip to get it into the copy buffer. Then "shift-insert" is often the right paste when "ctrl-v" doesn't work.
- Too 4y agoThanks for tip, but quite far from ideal. Piping into something requires 1. knowing upfront I want to copy the output. 2. Doesn’t allow partial selection of the output. 3. Extra typing | xclip after every command. 4. Xclip is not installed by default.
- informalo 4y agoSimilar resource for zle which is used by zsh instead of readline [1]. [1] https://thevaluable.dev/zsh-line-editor-configuration-mouseless/ https://thevaluable.dev/zsh-line-editor-configuration-mousel...
- esrh 4y agoAfter getting very used to C-w (delete back work) and C-h (delete character backward) on readline, it really bugged me that these aren't default on emacs, even though readline is often advertised as giving emacs muscle memory. I ended up globally remapping C-w and moving the help prefix, C-h to C-x h and making C-h backspace, because i was pressing it incorrectly that often.
- saurik 4y agoSo, to be fair, C-h isn't some crazy invention of readline (and thereby a failure of what it was "advertising") that you got used to: it is the standard key code for backspace. Just like a horizontal tab is ASCII 9 and a carriage return is ASCII 13, ASCII 8--and I realize this might make less sense as this in some sense "undoes" a thing and maybe you would never expect it to be in a file--is backspace. Meanwhile, all Control does is convert a normal (capital) character into a control character, which is the term for the ASCII characters below 32 (aka, the space character) by essentially subtracting 64 (which is almost but not quite equivalent to masking out a bit). Notably, @ is 64, so C-@ generates ASCII 0, which is why you see ^@ printed for NUL bytes in many editors (including vim). After @, you get A-Z and then [, \, ], ^, and _. This is why, if you want to get Escape, you can type C-[, as ESC is 27 and [ is 91. And this is also how C-G becomes that annoying bell, because G is the 7th letter of the alphabet and BEL is 7, And so, C-M is equivalent to pressing Enter, C-J is equivalent to pressing Tab... and C-H is equivalent to pressing Backspace!! Now, there is also a DEL, for the Delete key, which is which is 127. As ASCII is designed for 7-bit, that is the same as -1, so you can type it using C-?, as ? is 63 ;P. OK, so, one might should then wonder how the hell Emacs even works, if they remapped the character code for Backspace to do something silly like Help... right?! And the answer is kind of "it doesn't", which I find amazing! There is actually a FAQ entry for Emacs "Why does the Backspace key invoke help?" and a related section from its manual on "If DEL fails to delete". (It might even be the solution you came up with isn't the ideal one, so you might want to read these.) https://www.gnu.org/software/emacs/manual/html_node/efaq/Backspace-invokes-help.html https://www.gnu.org/software/emacs/manual/html_node/efaq/Bac... https://www.gnu.org/software/emacs/manual/html_node/emacs/DEL-Does-Not-Delete.html https://www.gnu.org/software/emacs/manual/html_node/emacs/DE...
- CodeIsTheEnd 4y agoFor anyone using a Mac, the basic cursor movement commands available in readline and emacs—C-f, C-b, C-a, C-e, C-n, C-p—are also available in virtually any text input in any program, namely web browser inputs, url bars, and textareas, as well as all system UIs. Once you get used to using these shortcuts that means you can compose and edit text anywhere without ever moving your hands. I recently switched to using Linux full time for my job and it KILLS me that these shortcuts aren’t available outside of the terminal. I try to navigate to a search suggestion in Chrome using C-n and accidentally open three new windows. I try to add a sentence at the start of Slack message using C-a and accidentally highlight everything and delete it. I try to move my cursor up to edit a previous line using C-p and end up brining up a print dialogue. It’s breaking the muscle memory of my most basic interactions with the computer, and it sucks.
- sillysaurusx 4y agoCommand-A isn’t available on Linux in slack? How do people even function without basic function keys?
- noisem4ker 4y agoLet me introduce you to the Home key: https://en.wikipedia.org/wiki/Home_key https://en.wikipedia.org/wiki/Home_key
- aliqot 4y agoI've always pried this key and other useless keys off my keyboards. TIL.
- dan-robertson 4y agoCommand-A does select all. On Linux if you use defaults, ctrl+A will do that. If you change your gtk keyboard shortcut theme to ‘emacs’ it will do the equivalent of ‘home’ instead (which is what ctrl-A does on a Mac). To delete a message under these conditions, you can usually do e.g. C-A C-K. But that’s not so suitable for eg a high-risk text box on a buggy site where you want to regularly select all then copy in case the site crashes or loses your data.
- ordu 4y ago> C-e, C-f, M-b, M-f Why these strange shortcuts when any keyboard has arrow keys and they can be used with a control modifier to move by words? I can understand why to use C-a, C-e, because laptop keyboards tend to have "Home" and "End" keys in strange places, but those... And Emacs documentation for some reason insists on using C-e, C-f instead of arrow keys.
- Guillaume86 4y agoOn laptop one requirement I have is having these: fn+left/right -> home/end fn+up/down -> page up/down It's probably been 20 years I have laptops that support it and I'm perfectly fine/fast to manipulate text with the keyboard.
- gabrielgio 4y agoNot a eMacs person, but as a vim user arrow keys are annoying to reach. Using control or vim bindings it is far easier for me
- nanna 4y agoIt's Emacs not eMacs! ;) https://irreal.org/blog/?p=10973 https://irreal.org/blog/?p=10973)
- gabrielgio 4y agoTIL people write eMacs on purpose. Mine was just iPhone's autocorrector as someone mentioned in the comments.
- skydhash 4y agoCtrl used to be where Caps Lock is on some keyboards and today, you can map Caps Lock to Ctrl in Mac OS snd Linux. It will make more sense when you do that as pressing the shortcut will be much easier (home row).
- bogantech 4y ago> Why these strange shortcuts when any keyboard has arrow keys and they can be used with a control modifier to move by words? There was a time before PC's when keyboards didn't necessarily have Arrow keys
- samueloph 4y agoTwo settings I always set in my .inputrc which are not listed in the page: # Make autocompletion case insensitive and display suggestions after single tab. # https://bugs.debian.org/990353 set completion-ignore-case On set show-all-if-ambiguous On
- ollybee 4y agoReadline also has the kinkiest sounding line out of any man page: (M-y) Rotate the kill ring, and yank the new top. Always makes me giggle.
- leetrout 4y agoFriends if you dont know: you can add readline support to LOTS of things, especially custom scripts and tools with a prompt by just calling the program with rlwrap. > rlwrap is a 'readline wrapper', a small utility that uses the GNU Readline library to allow the editing of keyboard input for any command. https://github.com/hanslub42/rlwrap https://github.com/hanslub42/rlwrap
- sylware 4y agoI would double check on GNU realine code sanity, including its SDK before learning its shortcuts. I use busybox shells anyway.
- 6502nerdface 4y agoIt'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/ https://troglobit.com/projects/editline/ See also the note at the bottom of that page about other versions of editline/libedit.
- k3vinw 4y agoAhh yes. Freezing your terminal via control flow with Ctrl-s. It’s almost as bad as finding yourself trapped in vi with no obvious way to exit. But if I recall correctly, you can resume flow with Ctrl-q.
- TacticalCoder 4y agoIndeed. And you can desactivate it too: I'm just running: stty -ixon in interactive shells and Ctrl-s ain't an issue anymore. If I do want to "pause" the output, teletype style, as if the seventies called or something, I pipe my command into less instead. I've been disabling Ctrl+s since literally more than a decade and don't know what I'm missing. Probably not much.
- nicoburns 4y agoI really wish we could get a GNU Readline replacement with much stronger editing capabilities: better multiline support, mouse support, syntax highlighting, etc.
- Steltek 4y agoCtrl-R! Ctrl-R! Ctrl-R! How many people have been on a screen sharing call and watched the other person tediously up-arrow through 5-10 commands to try to rerun what they just did? The single greatest productivity boost you can gift a CLI newbie is searching their command history. Everyone jumps on the Emacs nav keys (C-f, C-b, etc) or other editing trivia. No one has trouble editing a command in place but it certainly takes some people way too long to run a command from one minute ago. Also, purely selfishly, it's goddamned painful to watch the up-arrow dance and it wastes so much time. I want to scream.
- anamexis 4y agoFor extra boost, add fzf and its shell integration. https://github.com/junegunn/fzf https://github.com/junegunn/fzf
- petepete 4y agoI missed the shell integration when switching to Fish a couple of years ago. fzf.fish saved the day, I wouldn't be without the keybindings now (Ctrl+Alt+F is roughly the equivalent of **Tab) https://github.com/PatrickF1/fzf.fish https://github.com/PatrickF1/fzf.fish
- grimgrin 4y agoctrl+r is great so is modifying your inputrc so you can still use up arrows but filter the options by first typing a bit of text i.e. $ find <up arrow to just show lines starting with find> tada: $if mode=emacs "\e[A": history-search-backward "\e[B": history-search-forward $endif I do a bit more in that if block, but trimmed it down to make a point
- yamtaddle 4y agoI'm really bad about picking up new keyboard shortcuts (I end up using them little enough that unless I take out time for focused practice, I'll forget them—I'm pretty sure I'm just never gonna get decent at vim or emacs, for this reason, aside from basic navigation keys) but took to this one instantly because it's so damn useful that I used it several times the very first day I learned it, kept using it the rest of the week, and have used it almost daily ever since. Doesn't hurt that it's reasonably mnemonic ("R" for "reverse-search") Reverse-history-search is amazing and I don't know how I went as long as I did without knowing about it.
- marcodiego 4y agoTIL readline has macros.
- quercusa 4y agoTIL Although GNU readline maintains a kill ring (clipboard), which works much like Emacs’s kill ring, it’s not shared with your system’s clipboard, nor any other instances of GNU readline. It is, in effect, local to the process you’re running. so it's not just that I didn't copy something...
- petepete 4y agoLess Useful Key Bindings: C-l Clear screen This is the one I use most frequently. All day every day. I hate clutter and unless I need to refer to the output from the previous commands, it's gone.
- deleted 4y ago[deleted]
- deleted 4y ago[deleted]
- javier_e06 4y agoThe time has come for gnu console and linux filesystems to support emoji characters for command names and filenames. In Windows 10 you can name your files and folders with emojis.
- dflock 4y agoThis has been supported on Linux systems for many, many years, decades in the case of filenames, afaik.
- Igor_Wiwi 4y agoHow to remember "Kill word backward/forward". Is there any mnemonic for C-w, M-d?
- jordigh 4y agoC-w is "wipe", and M-d is "delete". C-d is also "delete" but one character. There are several Emacs combos where C- is for single characters and M- is for whole word, like C-f/M-f, C-b/M-b etc. M-w also exists in Emacs proper but it's "wipe without delete" or in common parlance, "copy".
- thomastjeffery 4y agoI have two frustrations with keyboard shortcuts that I think most people generally share: 1. They are usually inconsistent, so muscle memory conflicts. 2. You usually can't edit them. If only we could just edit them. The possibilities....
- saurik 4y agoWhy can't you edit them? Have you looked at .inputrc? If not, read the article ;P. You can also certainly edit anything in vim/Emacs. On macOS, you can even remap the keyboard shortcuts in almost every GUI app--and even sometimes universally across apps due to conventions--because of how they try to model shortcuts as convenience for the menu bar.
- thomastjeffery 4y agoYes, there are quite a few places where you can, but not enough. Most software can't be changed. And while you can edit (remap) the keybinds in [neo]Vim, there are limitations. You can't edit all of them at the same time without running into race conditions. I've tried and failed. And even Emacs - a nearly best-case scenario - is a struggle. First, you have to remap all the Emacs functions you want to change. Then you have to remap all the plugins you use. It's a lot of unnecessary work, and it's tricky to find your way around.
- yrro 4y agoSee the GNU Readline User Manual - https://tiswww.cwru.edu/php/chet/readline/rluserman.html https://tiswww.cwru.edu/php/chet/readline/rluserman.html (or 'info rluserman')
- droopyEyelids 4y agoI guess I'm the only one who tried to do this? There is no ~/Library/KeyBindings/ directory in macOS 12.6.1, and a find . -iname DefaultKeyBinding.dict doesn't return anything either
- jhallenworld 4y agoTwo 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 https://github.com/nklabs/libnklabs ("nkreadline" has editing, tab-completion (for embedded "nkcli" commands) and history)
- trelane 4y agoFor me as a customer, the GPL is a feature, not an "issue." Of course, if you ship software for your hardware and want to lock your customers out of their own hardware, then I can see why you'd not choose it.
- jhallenworld 4y agoI don't disagree with you (I've written plenty of GPL-licensed software), but as someone who gets paid to develop products for other companies, it's an issue. Few of my customers are interested in making the embedded software for their product open source. Actually the MIT open source license (and other similar ones) solves a completely different problem for me. It allows me to reuse this software for many different customers. It's slightly easier to do this when I tell them that I'm going to use "this open source library" vs. "my personal library that I'm granting you a license for this product, but I retain the right to use it for other customers".
- jacksgt 4y agoHas anyone managed to get an RSS Feed from masteringemacs.org ?
- mickeyp 4y agoI run one on /feed. I merely haven't linked to it, which I ought to fix!
- kmtrowbr 4y agoLots of comments about small improvements to typing efficiency. It is an important topic. One thing I do, is turn the key repeat speed up as fast as I can stand, and turn the delay to repeat down as low as possible. I have always done this and over the years I've experimented to find the fastest possible settings. On the Mac, use: defaults write NSGlobalDomain KeyRepeat -int 1 defaults write -g InitialKeyRepeat -int 7 See: http://hints.macworld.com/article.php?story=20090823193018149 http://hints.macworld.com/article.php?story=2009082319301814... After getting so used to these fast speeds, when I see folks attempting to type with the slow default speeds ... it is quite hilariously slow and I must confess I feel a little pity for them.
- lvass 4y agoOr just M-42 f to press f 42 times. In vi, 42 i f esc, works for sequences too.
- nine_k 4y agoIDK if it's obvious, but GNU readline has a full-blown vi mode, with ESC, insert / command modes, all the ergonomic motions, etc. The default is Emacs mode, and the author of the post is a Emacs user, but that mode is not the only one supported.
- shadytrees 4y agoFun fact: GNU Readline and Bash are maintained by a single person. https://tiswww.case.edu/php/chet/readline/rltop.html#Maintainer https://tiswww.case.edu/php/chet/readline/rltop.html#Maintai... In case you were looking for more examples of "open source being held together by one tireless person somewhere" (obligatory xkcd comic link omitted).