Live data from Hacker News

Bash One-Liners Explained, Part V: Navigating around

catonmat.net

21–30 of 35 posts

Re: Bash One-Liners Explained, Part V: Navigating around

#21
Instead 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.

Re: Bash One-Liners Explained, Part V: Navigating around

#22
post #7
post #5

This 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).

I'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 te…

You can enjoy the best of both worlds with something like zsh's edit-command-line. I'm very happy with Emacs keys in the shell, unless I need to make a non-trivial edit or type in a multi-line command. In those cases, C-x C-e drops me right into Vim with the current command-line ready for editing.

Re: Bash One-Liners Explained, Part V: Navigating around

#23
post #5

This 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).

Many thanks for pointing this out! I hadn't read far enough to get to it myself. I kind of wish it was closer to the top because it rendered most of the other tips kind of pointless. Well, not C-r, but I already knew that one. This has made me even happier than learning I could get vim key bindings in vim's own command-line mode[1]. Thanks again! [1](C-f from command-line mode, or q: from normal)

[deleted]

Re: Bash One-Liners Explained, Part V: Navigating around

#24
post #9

My 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.

Alt+Backspace deletes to the last non-letter. To go to the previous space, Ctrl+R SPACE works for me.

Ctrl+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.

Re: Bash One-Liners Explained, Part V: Navigating around

#25

Instead 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_pr…

Absolutely 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!

Re: Bash One-Liners Explained, Part V: Navigating around

#26

Instead 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_pr…

Cool, 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.

Re: Bash One-Liners Explained, Part V: Navigating around

#28
Some 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

Re: Bash One-Liners Explained, Part V: Navigating around

#29
post #5

This 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).

| I'd love to find a cheat sheet for vi bash usage (and also an indicator for which editing mode I'm in).

In zsh, you can type: "bindkey" to get a listing. IIRC, in bash it is "bind -p".

Re: Bash One-Liners Explained, Part V: Navigating around

#30
post #22
post #7

Earlier quoted context omitted.

I'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 te…

You can enjoy the best of both worlds with something like zsh's edit-command-line . I'm very happy with Emacs keys in the shell, unless I need to make a non-trivial edit or type in a multi-line command. In those cases, C-x C-e drops me right into Vim with the current command-line ready for editing.

Anybody know how to do the equivalent of C-x C-e in vim mode?
Post reply on HN