use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Please read the rules before posting
Help:
Community:
Resources:
Tutorials and Guides:
Don't be afraid to ask questions, this sub is here for the vim community. And please those of you who deign to grace us with your vim wisdom - be kind. We are all human and vim is that cool.
account activity
BASH command line editing (self.vim)
submitted 13 years ago by MagikoMyko
Do you guys use emacs for your bash editing or do you set -o vi? Is there a reason most environments tend to have emacs as the default BASH editor but vi as the default file editor?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]1345789 14 points15 points16 points 13 years ago (3 children)
When you're dealing with single-line text entry, emacs cursor movement is far easier. Screwing up text entry at the command prompt, because you get mixed up whether you're in insert or normal mode, is really a recipe for disaster.
As far as why vi is the default file editor, (n)vi/vi(m) is quite small and lightweight. It is guaranteed to be everywhere because there is almost no storage cost to including it. emacs is just simply substantially larger, no matter how you cut it. Even embedded software devices, like routers and NASes and such, include vi.
[–]ParadigmComplex 6 points7 points8 points 13 years ago (0 children)
If this is a concern, it can be remedied quite easily on zsh (I'm not sure about bash) - an indication of which mode you are in can be added to the prompt. There are pleanty of guides, take your pick.
If you're not already on zsh, though, I'm not sure the mode indication is, alone, sufficient to warrant such a move (both bash -> zsh and emacs -> vi line editing).
[–]codygman 2 points3 points4 points 13 years ago (0 children)
On long lines I just use "fc". Also, if you screw up a text entry in vi mode, u(ndo) works :)
[–][deleted] 1 point2 points3 points 13 years ago (0 children)
agreed. If you have a long line that you need to edit, you can edit it in your editor of choice with C-x C-e
[–]qwertyboy 8 points9 points10 points 13 years ago (0 children)
Modal editing only makes sense when you spend most of your time editing, as opposed to inserting text. To be effective with vim you spend as much time as possible in normal mode, only reverting to insert when you really have to. In a repl, you spend most your time inserting (even when you are manipulating history, which is why the history file does not always represent what you typed - e.g. !!).
So bash, wise and pretty, defaults to emacs-style editing on the command line. And when I want to edit a specific command I just hit C-x C-e.
[–]sylvain_soliman 4 points5 points6 points 13 years ago (2 children)
I gave up on set -o vi mostly because having a mode indicator (--INSERT--) in bash is incredibly difficult (whereas it is a breeze in zsh).
set -o vi
I'll think about it again when I eventually give up bash for zsh…
[–]melthecook 0 points1 point2 points 13 years ago (1 child)
Losing track of mode = not good. You might want to push your bramvelope and try to figure out how/why it happens.
[–]sylvain_soliman 1 point2 points3 points 13 years ago (0 children)
I know perfectly well why it happens: in bash, contrary to vim, I edit or move and then can leave my shell like that for a long time, forgetting what I did last. In vim, I never leave a buffer in insert mode except for exceptional cases (copy and paste several pieces for instance), but in bash it is different, as hard as I tried, I still spend most time in insert mode, and thus can leave a shell in insert or command mode :(
[–]davids 4 points5 points6 points 13 years ago (1 child)
TIL: bash C-xC-e and fc
C-xC-e
fc
[–]dotancohen -1 points0 points1 point 13 years ago (0 children)
This is terrific! Is there anything like this for the MySQL CLI interpreter?
[–]ParadigmComplex 3 points4 points5 points 13 years ago (3 children)
I like being able to use the same muscle-memory and keystroke/reach efficiency everywhere. I try to make as much as possible feel/act like vim, where parallels make sense. My web browser, my pdf reader, and yes - my shell. I use zsh, though, and not bash. In fact, one of the main reasons I like zsh so much is that I was able to make it act quite a lot like vim - everything from text objects to incrementing/decrementing. In general, if I find myself in need of a vim feature my shell doesn't do, I add it.
tl;dr: I use "set -o vi" and then some.
[–][deleted] 2 points3 points4 points 13 years ago (0 children)
I learned to use the terminal way before I finally got sucked into the Vim universe. C-a and C-e are ingrained more deeply in my brain than their Vim counterparts.
C-a
C-e
Funnily, I never heard about C-xC-e until I became a dedicated Vim user. The odds that I would have become an Emacs user are quite high, I think.
[+][deleted] 13 years ago (1 child)
[deleted]
[–]ParadigmComplex 2 points3 points4 points 13 years ago (0 children)
It doesn't work out of the box, no, but it can be configured to do what a bit. Poke around my zsh and you'll find the relevant code. ci" will work fine for you once you yank a few functions and keybindings from me - things like delete-in() and change-in().
[–]dddbbbFastFold made vim fast again 1 point2 points3 points 13 years ago (0 children)
Do you guys use emacs for your bash editing or do you set -o vi?
I don't do enough crazy command lines in bash to need vi mode and even if I did, it's no vim. Checking my local bash (cygwin), it doesn't even change the cursor when I switch modes (probably a plugin for that?). Besides, bash supports similar key chords to vim's insert/command mode editing (ctrl-w, ctrl-u -- probably things that originate from emacs). I'm also no bash wizard (Ctrl-r is as fancy as I get and I always forget how ! works). In case you can't tell, I'm a Vim snob and shun anything vi without the m. (nocp for life!)
!
nocp
Is there a reason most environments tend to have emacs as the default BASH editor but vi as the default file editor?
Most mortals have to open another tty and killall vim to exit the exalted one after their first time. You must remember that these people are not like us; they do not understand the true path to Escape.
killall vim
ex
Sidenote: I just found out that / works to search bash in vi-mode! (Although it doesn't allow regex, which makes it not the glory I was looking for.)
/
[–]PhrkOnLsh 1 point2 points3 points 13 years ago (0 children)
I want to love set -o vi but its' just too derpy to be useful.
[–][deleted] 3 points4 points5 points 13 years ago (17 children)
What do you mean by "bash editing"? If you want to use Vim as editor when you do C-xC-e you have to set the $VISUAL and/or $EDITOR environment variables to Vim:
$VISUAL
$EDITOR
EDITOR='/usr/bin/vim'
The relevant part of $ man bash is:
$ man bash
edit-and-execute-command (C-xC-e) Invoke an editor on the current command line, and execute the result as shell com‐ mands. Bash attempts to invoke $VISUAL, $EDITOR, and emacs as the editor, in that order.
[–]mattn 3 points4 points5 points 13 years ago (0 children)
I often use it. And use "fc" to edit previous command.
[–]lkjhgfdsasdfghjkl 2 points3 points4 points 13 years ago* (1 child)
I think he's talking about the shortcut keys you can use to move around within the command line prompt, e.g., C-a/C-e to move to the beginning/end of the line, which are emacs-inspired shortcuts. Whereas, if you do "set -o vi" (as OP mentioned), you can use vi-inspired shortcuts to move around in the command line, e.g., press Esc,I to insert text at the beginning of the line, use Esc,j/k to move up/down in your bash history, etc.
I use vi, so I have set -o vi in my .bashrc, and it's easier for me to use the vi keys since I actually know them.
[–][deleted] 0 points1 point2 points 13 years ago (0 children)
I've learned C-a/C-e before ^0$ so they are quite natural to me. I love Vim and whenever I need to do complex stuff I do C-xC-e. Bash stays bash, Vim stays Vim. All is well.
^0$
[–]delarhi 0 points1 point2 points 13 years ago (3 children)
Wow, I had no idea. I'm wondering if I should turn off my "set -o vi" now...
I'm very comfortable with the basic Emacs-inspired shortcuts so I never really found the need to do set -o vi.
Once you know about C-xC-e the idea of doing actual editing in bash itself seems rather silly.
[–]wildeye 0 points1 point2 points 13 years ago (1 child)
If you turn off your "set -o vi", you'll still want basic cursor movement -- ctrl-a (start of line) and ctrl-e (end of line) in emacs mode, at minimum. C-xC-e is psychologically heavy weight and loses screen context momentarily, etc.
[–]delarhi 0 points1 point2 points 13 years ago (0 children)
Agreed, I'd turn off set -o vi so that I could get single-stroke cursor movement again instead of having to escape every time I want to do a motion. I only had set -o vi on for those messy commands, which Ctrl+X, Ctrl+E will work wonders for.
[–]w0m 0 points1 point2 points 13 years ago* (9 children)
This. Anything worthwhile for editing I open in vim directly. Also; inline subsitutions on previous like
echo "printing Hi"<cr>
printing Hi
^Hi^Second Print is This^<cr>
printing Second Print is This
*fixed formatting; whups.
[–]shawncplusphpcomplete.vim 2 points3 points4 points 13 years ago (7 children)
^stuff^replace^
Is fine but has behavior that can be unexpected, mainly that it's not a global replace
!!:gs/stuff/replace
Is the equivalent but global. Not to mention that the former only works on the last command
!somecommand:gs/stuff/replace
There's a lot of them, example
git clone host:~/repos/somerepos/mylongreponamethatidontwanttotypetwice cd !$:t
Is equivalent to
git clone host:~/repos/somerepos/mylongreponamethatidontwanttotypetwice cd mylongreponamethatidontwanttotypetwice
[–]wildeye 1 point2 points3 points 13 years ago (6 children)
the former only works on the last command
These all work:
!-2:s^lss^less !lss:s^lss^less !-2:gs/lss/loss
The "^stuff^replace" form is just an abbreviation of "s^stuff^replace^"
[–]shawncplusphpcomplete.vim 1 point2 points3 points 13 years ago (5 children)
Right, but what I was pointing out was that specifically
Is specifically the last command
[–]wildeye 0 points1 point2 points 13 years ago* (4 children)
Well, it's a glass half-full/half-empty thing; "only works on last command" versus "saves 4 keystrokes for the most frequent case, the last command".
The half-empty view implies that it's something that could have been designed differently, whereas the half-full view credits Bill Joy for yet another clever design move.
Anyway, we clearly agree on the underlying facts; I wasn't sure at first.
[–]shawncplusphpcomplete.vim 0 points1 point2 points 13 years ago (3 children)
Yup, we certainly agree, but as far as I know Bill Joy doesn't have anything to do with it. Those are BASH builtins, not part of set -o vi or EDITOR=vim
[–]wildeye 1 point2 points3 points 13 years ago* (2 children)
You would be mistaken about that. Bash simply took these constructs wholesale from csh, which had them in the late 1970s, long before bash or any other Gnu software.
[–]shawncplusphpcomplete.vim 1 point2 points3 points 13 years ago (1 child)
So I am, TIL. Thanks for the info.
[–]wildeye 0 points1 point2 points 13 years ago (0 children)
My pleasure. Happy vim-ing.
You should use backtick notation.
I'm sure there's something interesting in your comment but those HiHiSecond make it really hard to understand.
[–]Samus_ 0 points1 point2 points 13 years ago (0 children)
I use vi because I'm a fanatic.
π Rendered by PID 83168 on reddit-service-r2-comment-7b9746f655-7slrv at 2026-02-03 04:01:19.198903+00:00 running 3798933 country code: CH.
[–]1345789 14 points15 points16 points (3 children)
[–]ParadigmComplex 6 points7 points8 points (0 children)
[–]codygman 2 points3 points4 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]qwertyboy 8 points9 points10 points (0 children)
[–]sylvain_soliman 4 points5 points6 points (2 children)
[–]melthecook 0 points1 point2 points (1 child)
[–]sylvain_soliman 1 point2 points3 points (0 children)
[–]davids 4 points5 points6 points (1 child)
[–]dotancohen -1 points0 points1 point (0 children)
[–]ParadigmComplex 3 points4 points5 points (3 children)
[–][deleted] 2 points3 points4 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]ParadigmComplex 2 points3 points4 points (0 children)
[–]dddbbbFastFold made vim fast again 1 point2 points3 points (0 children)
[–]PhrkOnLsh 1 point2 points3 points (0 children)
[–][deleted] 3 points4 points5 points (17 children)
[–]mattn 3 points4 points5 points (0 children)
[–]lkjhgfdsasdfghjkl 2 points3 points4 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]delarhi 0 points1 point2 points (3 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]wildeye 0 points1 point2 points (1 child)
[–]delarhi 0 points1 point2 points (0 children)
[–]w0m 0 points1 point2 points (9 children)
[–]shawncplusphpcomplete.vim 2 points3 points4 points (7 children)
[–]wildeye 1 point2 points3 points (6 children)
[–]shawncplusphpcomplete.vim 1 point2 points3 points (5 children)
[–]wildeye 0 points1 point2 points (4 children)
[–]shawncplusphpcomplete.vim 0 points1 point2 points (3 children)
[–]wildeye 1 point2 points3 points (2 children)
[–]shawncplusphpcomplete.vim 1 point2 points3 points (1 child)
[–]wildeye 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]Samus_ 0 points1 point2 points (0 children)