You are not logged in.

#1 2006-04-20 05:23:10

elasticdog
Member
From: Washington, USA
Registered: 2005-05-02
Posts: 995
Website

[TIP] Use vi keys for CLI history and editing

I randomly discovered this when reading documentation about GNU Screen, and thought it was awesome!  By default bash (and I believe other shells too) use some emacs like commands for movement: <code>CONTROL-A</code> to move the cursor to the begining of the line, and <code>CONTROL-E</code> to move the cursor to the end of the line, etc.

You can change that behavior to more closely mimic the movements and commands in Vi by issuing the command:

$ set -o vi

If you just want to see what your current settings are, leave off the <code>vi</code> part.

$ set -o
allexport       off
braceexpand     on
emacs           off
errexit         off
hashall         on
histexpand      on
history         on
ignoreeof       off
interactive-comments    on
keyword         off
monitor         on
noclobber       off
noexec          off
noglob          off
nolog           off
notify          off
nounset         off
onecmd          off
physical        off
posix           off
privileged      off
verbose         off
vi              on
xtrace          off

Then you can hit the <code><ESCAPE></code> key to enter the command-editing mode.  Then try out using the "<code>h j k l</code>" movement keys to navigation through the command history.  You can also use other Vi movement commands to edit the line.  If you want to go back to typing, use the typical "<code>i I a A</code>" keys to either Insert or Append as expected.  To make this change permanent, just add the "<code>set -o vi</code>" command in your <code>~/.bashrc</code> file.

Anyway, after messing around with it for a bit, I think it will make my command-line usage much more efficient.  Hope this appeals to somebody else out there...

Offline

#2 2006-04-20 14:14:39

Bison
Member
From: Jacksonville, FL
Registered: 2006-04-12
Posts: 158
Website

Re: [TIP] Use vi keys for CLI history and editing

That is awesome! I will try when i get home

Offline

#3 2006-04-20 14:47:24

elasticdog
Member
From: Washington, USA
Registered: 2005-05-02
Posts: 995
Website

Re: [TIP] Use vi keys for CLI history and editing

I also forgot, you can use Vi commands to skip to items from your history as well.  If you view your history:

$ history
  ...
  377  ps aux | grep firefox
  378  sudo pacman -Syu
  379  exit
  380  startx
  381  startx
  382  sudo pacman -Syu
  383  less /etc/ntp.conf
  384  sudo vim /etc/pacman.conf
  385  sudo pacman -Syu

...and say you want to run "<code>ps aux | grep firefox</code>", you can just type <code><ESCAPE></code> to enter command-editing mode, put in the line number you want: <code>377</code>, and then hit '<code>G</code>' to go there.  There are all sorts of cool things I'm figuring out...

Offline

#4 2006-04-20 16:31:56

alterkacker
Member
From: Peoples Republic of Boulder
Registered: 2005-01-08
Posts: 52

Re: [TIP] Use vi keys for CLI history and editing

Other handy things you can do:
- Esc '/' text <Enter> to search back in your command history for text.
- 'n' to continue searching backward
- 'N' to search forward

And my favorite: once you have the command line you want through the above or with the "Esc linenumber 'G'" technique you can then either edit the line in-place or enter 'v' to invoke a full 'vi' session on that line - great for long multipart commands!

Offline

#5 2006-04-20 19:08:16

codemac
Member
From: Cliche Tech Place
Registered: 2005-05-13
Posts: 794
Website

Re: [TIP] Use vi keys for CLI history and editing

For zsh, the `vicmd` option does the same thing.

Offline

#6 2006-04-21 00:53:26

PeteMo
Member
From: H'Burg, VA
Registered: 2006-01-26
Posts: 191
Website

Re: [TIP] Use vi keys for CLI history and editing

One question.  Although I'm enjoying the vi style on the bash shell, I have become quite used to using "ctrl-L" to clear the screen.  That only works in the vi shell if you are in command mode.  If you are in insert mode, it simply prints "^L" to the screen.  Is it possible to make ctrl-L clear the screen in insert mode?

I tried the following in my ~/.bashrc with no luck:

alias ^L='/usr/bin/clear'

Offline

Board footer

Powered by FluxBB