7 ms·
Bash One-Liners Explained, Part V: Navigating around
- minimax 14y agoIf you find yourself using these keyboard shortcuts frequently, you can save yourself some pinky fatigue by remapping caps lock to ctrl.
- acabal 14y agoDoing this was a big part in saving my left hand from RSI. I can't emphasize enough how awesome it is to have ctrl bound to caps lock.
- lunchladydoris 14y agoYes! It's the first thing I do when I get a new system these days. In OSX you can set it up in keyboard preferences and in Windows I use Autohotkey. Working on a keyboard with a regular Caps Lock key feels very strange after all this time.
- gizmo686 14y agoBut then where is my ESC key?
- jlkinsel 14y agoMostly common (to me), but there's a few good ones in there! ^x^e could be my new favorite!
- JoelMcCracken 14y agoThe manual: http://cnswww.cns.cwru.edu/php/chet/readline/rluserman.html#SEC2 http://cnswww.cns.cwru.edu/php/chet/readline/rluserman.html#... edit: oh, this is the first time I've seen this: there is a vi mode for bash http://cnswww.cns.cwru.edu/php/chet/readline/rluserman.html#SEC22 http://cnswww.cns.cwru.edu/php/chet/readline/rluserman.html#...
- JakeSc 14y agoThis is a great guide. Especially awesome for me was: 26. Change input mode to vi $ set -o vi This command changes the key bindings to vi's. If vi's your favorite editor, you'll love this. I'll cover the vi mode in more details in the next part of the article. I'd love to find a cheat sheet for vi bash usage (and also an indicator for which editing mode I'm in).
- tnorthcutt 14y agoYou'll probably like this (includes cheat sheet): http://www.catonmat.net/blog/bash-vi-editing-mode-cheat-sheet/ http://www.catonmat.net/blog/bash-vi-editing-mode-cheat-shee...
- tsm 14y agoI'm a huge vi(m) fan, but still use Emacs-style readline. When editing code in vim, I tend to make several edits each time I'm in edit mode. When at the terminal, all I'll need is "go to front" or "search backwards". And which is easier: Esc+Shift-6+i or C-a? Esc+Shift-/ or C-r? I'm in no way an Emacs ninja, but it's not too hard to memorize the ten or so most common commands. Added bonus: they work in OS X and Qt textfields. Other added bonus: they work out of the box in terminals you haven't configured yourself.
- jlgreco 14y agoI like vi-mode in my shell primarily for j/k to navigate through history (arrow keys just always suck imho, and C-r to search backwards in history is just overkill most of the time) and 'c[motion]', which is what I almost exclusively find myself using to correct things I messed up ('d[motion]' pops up less frequently). 99% of the time those are 99% of the things I use; for that set of features I think the vi sequences are definitely have the advantage.
- pmr_ 14y agoThe Emacs next-line and previous-line bindings C-n and C-p also work well for history navigation.
- 14y ago
- andrewla 14y agoMy biggest complaint is that the semantics of CTRL-w and ALT-b are different. CTRL-w goes to the last space, and ALT-b goes to the nearest non-letter. Why did they define word in two different ways for the two different commands? I would kill for a non-deleting version of CTRL-w; if I have a long path in a command-line I want to be able to move to the previous parameter, not just the previous path element.
- michaelhoffman 14y agoAlt+Backspace deletes to the last non-letter. To go to the previous space, Ctrl+R SPACE works for me.
- andrewla 14y agoCtrl+R SPACE seems like it's a bit iffy for some cases, especially since I use Ctrl+R so frequently for history searches. Using it instinctively (like I use Ctrl+W) wreaks all kinds of havoc; if I do something like Ctrl+R python to get a previously run command, then hit Ctrl+R SPACE, then it moves to the previous commandline that contains python instead of staying where I am and moving back a word. Still, something to explore -- I can probably find a way to use this.
- skylan_q 14y ago"Bash uses the readline library for input editing. The readline library supports emacs style key bindings[...]" Been using Emacs for a year and I didn't know this. O_o
- bittersweet 14y agoThe author notes that there is no meta key anymore [1], however you can often setup your terminal emulator to use another key as the meta key. In iTerm [2] I have the left alt/option key setup as meta, so things like meta-b are much easier to type then Esc-b. [1] "You'll often see Meta+b but there is no such key on the keyboards anymore." [2] Profiles -> Keys "Left option acts as meta"
- tambourine_man 14y agoInstead of searching through history, you can add this to your .inputrc "\e[B": history-search-forward "\e[A": history-search-backward And this to your .bash_proflie export HISTCONTROL=erasedups export HISTSIZE=100000 shopt -s histappend Now the up/down arrow keys auto search and complete backwards/forwards based on what's written. If the line is empty, it behaves as normal. This is also pretty neat, in your .bash_proflie: bind '"\t":menu-complete' Enables cyclic tab completion These are the first things I do when I'm on another terminal.
- follower 14y agoRegarding HISTFILESIZE the man page for bash says: > When this variable is assigned a value, the history file is truncated Which I read as meaning if you export the following then you should get infinite entries: export HISTFILESIZE= I'm testing to see if it works for this also: export HISTSIZE=
- X6MW3aQrZU5VKkz 14y agoAbsolutely love both of these, thank you so much. My only nitpick would be that when you have text typed, say '$ vim' and go up, finding '$vim somefile', then going down again should bring you back to what you originally typed. This is also the behaviour in vim, I believe. Any other tips? These two were brilliant!
- guiambros 14y agoCool, thanks for the cyclic tab. That was a new one for me. I've been using the remap of the up/down arrow for a while too, and it is crazily useful indeed. It's the first thing I do on a new terminal. That, together with adjusting PS1 and setting up my common aliases.
- crazydiamond 14y agoSome awesome ones he did not mention: Control-q: push-line-or-edit -- this saves the current command without executing it, allows you to run another command, then puts back the saved command for you to edit/run. Esc-A: accept-and-hold -- execute command and maintain cursor position, so you can edit command and re-run. Esc-' : quote-line -- escapes the single quotes in a line. Old Vim user here, who has used emacs mode since moving from ksh to bash for about 8-10 years. Recently, switched back to vi mode, however, I have mapped Ctrl-A, Ctrl-E and a few more so i can use them in vi mode too. e.g. bindkey "^A" beginning-of-line
- deleted 14y ago[deleted]
- prakashk 14y agoI knew most of these, but I learned about Ctrl-# (for commenting a command), and Ctrl-x+Ctrl-u (or Ctrl-/) for undoing changes. One shortcut that used often is Ctrl-o. When you have a few commands in your history that you want to repeat in that seqeunce, search for the first command in the group, optionally edit it if you want, and press Ctrl-o instead of the ENTER key. After this command is executed, the next command in the history is presented to you. Press Ctrl-o again to continue the sequence, or press ENTER once you reached the last command in the sequence. If you want skip a command in the sequence, instead of Ctrl-o just press Ctrl-n (or down-arrow) to get the next one in sequence.
- olaf 14y agoThis seems to be written by someone who has no deep experience. For example, Ctrl-u does only delete the whole line, if the cursor stands at the end of that line, on my system (Ubuntu) this command generally deletes the characters left from the cursor. What's missing, is the complement to this command, which deletes the characters under and right from the cursor, namely Ctrl-k. Everybody who regularly uses Ctrl-u should know that.