Hacker News new | past | comments | ask | show | jobs | submit login
You can use vi mode editing in any program that uses readline
86 points by dorfsmay on Feb 16, 2015 | hide | past | favorite | 17 comments
With any program that uses readline, including psql, python and ipython, you can switch between vi and emacs editing by typing:

ctrl - alt - j

( C-M-j in emacs lingo)




To automatically enable this for all term applications, create a file .inputrc in $HOME and put this:

set editing-mode vi

$if mode=vi

set keymap vi-command

"gg": beginning-of-history

"G": end-of-history

set keymap vi-insert

"jj": vi-movement-mode

"\C-p": history-search-backward C-L: clear-screen


What happens if you don't set keymap "gg", "G", etc? Would they not work, or...? (Currently not on a *nix machine to be able to test it out, or else I wouldn't ask such a simple question :P)


"G" works with a number for recalling shell history. For example, "4G" recalls the fourth command in the shell history. However, "G" alone without a number does not work as it does in vi (jump to last line). I suppose that was the desired functionality.

It makes sense for "gg" to be absent, as it is not a vi command. It is a "vimism". The typical vi command for moving to the first line is "1G".


My guess is, that you redefine them to mean the semantically same in a new domain. What I mean is that gg and G lets you go to the top of the file and to the bottom of the file, right? But on the command-line, what is the top of your file? And what is the bottom?


I can only speak for the Emacs capabilities of readline: all commands which would usually change the line (previous/next-line, beginning/end-of-buffer) will use the history as the buffer. All of them preserve the line you are currently typing, which will be the end of the buffer.


As well as ~/.inputrc (which I have "set editing-mode vi" within), there is also ~/.editrc which has support for the editline library (which some repls use). I have my ~/.editrc set to the following to give me a vim-like editing environment

    bind -v
    bind "^R" em-inc-search-prev
    bind \\t rl_complete
(NOTE: I forget the source for this, it was something like oh-my-vim but I can't find exactly where it was from.)


Thanks!

You really only need the first line for the apps to start in vi mode automatically.

Knowing the key combination to toggle is still useful for when pairing etc...


This will also work for any interactive process you run as

rlwrap <command>

For example, when working with SML/NJ, which has an interactive REPL but no history support or command-line niceties, you can run

rlwrap sml

And get all the benefits of Vi, history files, etc.


Ha! Even better... I always use rlwrap when I have to use sqlplus.


hehe, I got to hear about rlwrap for SML/NJ too (Coursera proglang MOOC). Very small, very reliable, what's not to love.


As a vim user for everything, I once tried to use the vi mode in bash and zsh, but quickly got back to emacs style cause I couldn't do what I was used to (mostly Ctrl+P/N,A/E,R,W/U). Is there someone here who would tell me how he uses the vi mode in a shell/readline program so I could try again?


Hit "<esc> k", then it's like vi, k/j to go up down the history buffer, l/h to move right and left (and of course, w, b, fx etc...), cx/dx/ etc... to change/delete (x if a movement command) etc...

"<esc> /pattern" to search back in the history buffer

The only tricky one for bash is "<esc> =" for file completion.


I'm probably not telling a lot of vi fans here anything new but just in case; add vi behaviour to Firefox with this add-on.

https://addons.mozilla.org/en-US/firefox/addon/vimperator/


See also:

http://5digits.org/pentadactyl/

"Pentadactyl was once called Vimperator, initially written by Martin Stubenschrott, then developed and maintained by Doug Kearns, Kris Maglione, and several other invaluable contributors. Doug and Kris, the primary Vimperator developers for several years, have left the Vimperator project and now develop and maintain Pentadactyl in its stead."


Unfortunately, Pentadactyl is not very active at this point. There haven't been new nightly builds in months, and there hasn't been a proper release in nearly a year.


Thanks, I will give it a go!


This also works on Twitter (try j, k, r, t, u, etc.).




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: