12

I've kind of become vim crazy these last few months, using plugins like Vimium to control chrome. I currently use iTerm2, but I was wondering if there were any terminal emulators or shells that use vim commands or could be configured to have vim like commands.

I'm mostly interested in selecting text (visual mode) from the buffer, yanking it, and pasting it somewhere on the command line below. It would be nice to be able to scroll up and down the buffer with j and k, and to edit your command with insert mode (currently have to use arrow keys)

Does such a beautiful thing exist?

nolanpro
  • 2,125
  • 3
  • 21
  • 22
  • This isn't usually a function of the terminal emulator, but the application (e.g. shell) you're running within it. – Barmar Feb 27 '13 at 19:58
  • 3
    For instance, in bash, `set +o vi` to get vi-style editing instead of emacs-style. Other applications that use readline should be similarly configurable. – Barmar Feb 27 '13 at 20:00
  • Thanks Barmar, i'll look into that – nolanpro Feb 27 '13 at 20:01
  • Wow `set +o vi` is pretty cool! I still need to figure out selecting text from the buffer – nolanpro Feb 27 '13 at 20:07
  • 2
    Selecting text from the buffer, unless you mean typed-but-not-yet sent text, *does* fall under the purview of the terminal emulator, as the shell has no notion of what's currently on your terminal emulators screen. You may want to look into the `tmux` or `screen` programs as well. – chepner Feb 27 '13 at 20:16
  • Thanks chepner, looks like both screen and tmux can select/copy/paste with (somewhat configurable) keyboard commands. Not exactly like vim, but might be the best I can hope for. – nolanpro Feb 27 '13 at 20:23

2 Answers2

10

Editing and executing commands through vim

  1. open vim, :r !history (or whatever), copying things, and executing it via :!<type your shell command here>. Use the last command :silent !<blah blah> if vim should stop bugging you with messages afterwards.

  2. If you stick to the regular emacs settings (if you don't change modes in either shell like i described, you actually can use ctrl+a, ctrl+e and all the like!), you might try writing your command and edit it in vim through CTRLXCTRLE.
    However this depends on the editor set via the EDITOR / VISUAL environment variable, which tells linux which editor the system should use usually!

    Here you can paste content if you have a vim that is compiled with the +clipboard or +xterm_clipboard flags!
    :wq and you are back at the prompt with the edits you have made.



Native bindings in the shell

bash: set -o vi

zsh: bindkey -v

Switching to normal mode with ESC or CTRL[, you can use movement like in vim (e w b h j k l ...) or open a vim with the current content using v.

The shells try their best to emulate the behaviour. Sadly you have no visual indicator wether you are inserting or not, besides the input changes when you type.

To have native bindings permanently, add the commands to either ~/.bashrc or ~/.zshrc, depending on the shell you use. If you just want to test things out, enter this on the shell directly, just don't forget the new setting will not stick this way.
This will also work on windows if you have a shell installed (be it through git's bash or via cygwin.).

isvforall
  • 7,860
  • 6
  • 27
  • 46
sjas
  • 15,508
  • 11
  • 75
  • 80
  • 1
    changed to "set -o vi", I am confused as a lot of people report "set +o vi", that does not work for me (linux) but that may be the correct syntax on BSD/OSX?... feel free to edit back to "set +o vi" if that is correct on "more systems" – jcr Mar 24 '16 at 12:04
4

sjas is close but missing the critical component:

set -o vi gives you vim bindings

then press v to edit your current line in vim

the mouse is almost always more efficient for selecting text, vim or no vim, so us the mouse for that.

Andy Ray
  • 26,451
  • 11
  • 86
  • 123
  • 6
    I very strongly disagree with the idea that the mouse is more efficient for selecting text. Visual mode is fantastic. And I can think of PLENTY of examples of it being much faster than highlight. One would be selecting an entire file: `ggvG` (from anywhere in the file). Or selecting an entire C-style function: `^v$%` (from any point on the line the function was declared). And those are just 2 really quick examples off the top of my head. I would say that in general vim is BETTER at selecting text. The mouse is not fast or precise when it comes to text, not compared to vim. – semicolon Jul 28 '16 at 18:43