Friday, March 23, 2012

Re: Bash's vi command line editing mode

On 03/23/2012 11:26 AM, Gary Johnson wrote:
> On 2012-03-23, Ven Tadipatri wrote:
>> Hi,
>> This may sound like a real newbie question, but when I do the "set -o vi"
>> in the bash command line shell, if I hit<Esc> and v on the command line,
>> it goes into vi editing mode. This is kind of cool, as I can exercise
>> the full editing power of vi, and when I exit the editor it runs the command.
>> Unfortunately, sometimes I may have a really powerful/dangerous/unnecessary
>> command that I've typed, and all I want to do is just cancel, not execute the
>> command. How do I do this?
>> :q! doesn't seem to work, as the command still runs. :wq , well, I don't want
>> to save anything, I just want to get out of the editor and return to the plain
>> old bash prompt.
>> Is this possible? Of course I can always kill the terminal that I'm running
>> in to avoid running the command as soon as vi exits. Or I can try to press
>> ctrl+C as fast as possible.
>> I was hoping for a better alternative.
>
> One way is to exit vim with the :cq command. See
>
> :help :cq
>
> That will quit vim with an error code telling bash that the editing
> was unsuccessful.
>
> Another is to delete the contents of the vim buffer before saving
> and quitting as normal, e.g., dd if there is only one line in the
> buffer or ggdG to if there is more than one line, followed by ZZ or
> :wq to save and quit.
>
> In this case, you _do_ want to save something because that something
> is what bash will execute.
>
> I usually use dd:wq because the :w makes me feel more sure that
> the empty buffer has actually been written.
>
> Regards,
> Gary
>

I always ddZZ and I think it's the best approach because this is
not something you might use often and remembering a special :cq
for it makes no sense because everybody knows dd and it works
perfectly well for this use case.

I like it better than :q! because it's easier to type, because
I hardly ever use :q! and dd is more visually explicit, i.e.
you can see "this is what will get executed - nothing", with
:q! I think if I take a longer time to edit the command, I
might save it accidentally as I'm working on it, and then
exiting will run the saved version.

I also use the same dd command when I edit the regular command
line and then decide I don't want to run it.

-ak

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: