Quantcast

Using Bash in Vi mode. How to map keys?

classic Classic list List threaded Threaded
18 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Using Bash in Vi mode. How to map keys?

aleCodd
is there any way to use the familiar :map and :ab commands when running bash with "set -o vi"?

for example, if i want to map the following:
        :inoremap ( ()<left>
how is it done?

can bash "understand" vi-like commands?

in other words, are the readline commands the ONLY solution, or is there another way to map keys in bash?

thanks
alex
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Using Bash in Vi mode. How to map keys?

Tony Mechelynck
On 01/11/10 16:23, aleCodd wrote:

>
> is there any way to use the familiar :map and :ab commands when running bash
> with "set -o vi"?
>
> for example, if i want to map the following:
>          :inoremap ( ()<left>
> how is it done?
>
> can bash "understand" vi-like commands?
>
> in other words, are the readline commands the ONLY solution, or is there
> another way to map keys in bash?
>
> thanks
> alex
>

"Bash in vi mode" is not Vim, it's bash using some Vi-like key bindings
(instead of its default Emacs-like key bindings) for command-line
editing. IIUC, bash has no ex-commands, and therefore no ":map" or
":abbrev" command. It has _bash_ commands though (external, aliases,
builtins, functions, there are several kinds). So, I think the solution
is to bone up on the bash manpage, and see if it can help you.

Best regards,
Tony.
--
BLACK KNIGHT: None shall pass.
ARTHUR:       I have no quarrel with you, brave Sir knight, but I must cross
               this bridge.
BLACK KNIGHT: Then you shall die.
                  "Monty Python and the Holy Grail" PYTHON (MONTY)
PICTURES LTD

--
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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Using Bash in Vi mode. How to map keys?

aleCodd
thanks for the reply.

so what is the equivalent for the following:
        ino ( ()<left>
using the readline module?

and if you could please be more precise, is the readline library the only utility for mapping keys in bush?

if yes, is there any interpreter i can use to convert Vim's mapping commands to readline commands?

One other thing, is there a way to change the cursor shape and color - when running bash in vi mode - so that the shape and size of the cursor would be different for different modes (insert/normal), like the 't_SI' or 'guicursor' in vim/gvim..?

cheers
alex
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Using Bash in Vi mode. How to map keys?

Tony Mechelynck
On 01/11/10 19:23, aleCodd wrote:
>
> thanks for the reply.
>
> so what is the equivalent for the following:
>          ino ( ()<left>
> using the readline module?

I don't know. These are all bash questions, not Vim questions. Maybe a
bash list or forum would be better than the Vim list. Have you read the
bash manpage?

>
> and if you could please be more precise, is the readline library the only
> utility for mapping keys in bush?
>
> if yes, is there any interpreter i can use to convert Vim's mapping commands
> to readline commands?
>
> One other thing, is there a way to change the cursor shape and color - when
> running bash in vi mode - so that the shape and size of the cursor would be
> different for different modes (insert/normal), like the 't_SI' or
> 'guicursor' in vim/gvim..?

no AFAIK, because bash is not a modal editor

>
> cheers
> alex

Regards,
Tony.
--
The misnaming of fields of study is so common as to lead to what might
be general systems laws.  For example, Frank Harary once suggested the
law that any field that had the word "science" in its name was
guaranteed thereby not to be a science.  He would cite as examples
Military Science, Library Science, Political Science, Homemaking
Science, Social Science, and Computer Science.  Discuss the generality
of this law, and possible reasons for its predictive
power.
                -- Gerald Weinberg, "An Introduction to General Systems
                   Thinking."

--
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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Using Bash in Vi mode. How to map keys?

Eran Borovik
In bash "vi" mode, one can press v and then get a full vim editor.
Then you will have everything you need.

Hope it helps,
Eran.

On 11/1/10, Tony Mechelynck <[hidden email]> wrote:

> On 01/11/10 19:23, aleCodd wrote:
>>
>> thanks for the reply.
>>
>> so what is the equivalent for the following:
>>          ino ( ()<left>
>> using the readline module?
>
> I don't know. These are all bash questions, not Vim questions. Maybe a
> bash list or forum would be better than the Vim list. Have you read the
> bash manpage?
>
>>
>> and if you could please be more precise, is the readline library the only
>> utility for mapping keys in bush?
>>
>> if yes, is there any interpreter i can use to convert Vim's mapping
>> commands
>> to readline commands?
>>
>> One other thing, is there a way to change the cursor shape and color -
>> when
>> running bash in vi mode - so that the shape and size of the cursor would
>> be
>> different for different modes (insert/normal), like the 't_SI' or
>> 'guicursor' in vim/gvim..?
>
> no AFAIK, because bash is not a modal editor
>
>>
>> cheers
>> alex
>
> Regards,
> Tony.
> --
> The misnaming of fields of study is so common as to lead to what might
> be general systems laws.  For example, Frank Harary once suggested the
> law that any field that had the word "science" in its name was
> guaranteed thereby not to be a science.  He would cite as examples
> Military Science, Library Science, Political Science, Homemaking
> Science, Social Science, and Computer Science.  Discuss the generality
> of this law, and possible reasons for its predictive
> power.
> -- Gerald Weinberg, "An Introduction to General Systems
>   Thinking."
>
> --
> 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
>

--
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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Using Bash in Vi mode. How to map keys?

aleCodd
Eran Borovik wrote
In bash "vi" mode, one can press v and then get a full vim editor.
Then you will have everything you need.

Hope it helps,
Eran.
I still have 2 problems with that, first i don't know how to "submit" the command back to bash after editing the command on vim, and second its really over the top, to switch back and forth between bash and vim for every single command.

another problem im struggling now, is to find out what is the equivalent to ":noremap" in when binding keys in .inputrc for bash.

I mean, sure you can bind the following:
        "a":"b"
but how do you bind:
        "a":"aa"
because this will not work because it will enter into an ENDLESS LOOP by remapping the 'a' character to itself...

cheers
alex
ZyX
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Using Bash in Vi mode. How to map keys?

ZyX
Reply to message «Re: Using Bash in Vi mode. How to map keys?»,
sent 15:40:29 02 November 2010, Tuesday
by aleCodd:

> I still have 2 problems with that, first i don't know how to "submit" the
> command back to bash after editing the command on vim, and second its
> really over the top, to switch back and forth between bash and vim for
> every single command.
>
> another problem im struggling now, is to find out what is the equivalent to
> ":noremap" in when binding keys in .inputrc for bash.
>
> I mean, sure you can bind the following:
>         "a":"b"
> but how do you bind:
>         "a":"aa"
> because this will not work because it will enter into an ENDLESS LOOP by
> remapping the 'a' character to itself...
Maybe you should try switching to zsh: its zle (z line editor) is highly
configurable. Though it does not have an equivalent to `noremap', you could bind
keys to functions: your example could be written like that:

    function _-ins-aa() { LBUFFER+=aa }
    zle -N ins-aa _-ins-aa
    bindkey a ins-aa

Not as obvious as `noremap a aa' (or `bindkey -s a aa' that results in ``zsh:
string inserting another one too many times'' error), but it works.

It also has a vi mode and I saw an answer on stackoverflow that explains how to
change prompt when switching from/to `normal' mode:
http://stackoverflow.com/questions/3622943/zsh-vi-mode-status-line (links in
this question are also useful). It looks like bash does not have such option.

signature.asc (205 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Using Bash in Vi mode. How to map keys?

Christian Brabandt
Hi ZyX!

On Di, 02 Nov 2010, ZyX wrote:
> It also has a vi mode and I saw an answer on stackoverflow that
> explains how to change prompt when switching from/to `normal' mode:
> http://stackoverflow.com/questions/3622943/zsh-vi-mode-status-line 

Here is another example:
http://www.bewatermyfriend.org/posts/2010/08-08.21-16-02-computer.html


regards,
Christian

--
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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Using Bash in Vi mode. How to map keys?

aleCodd
In reply to this post by ZyX
ZyX wrote
Maybe you should try switching to zsh: its zle (z line editor) is highly
configurable. Though it does not have an equivalent to `noremap', you could bind
keys to functions: your example could be written like that:

    function _-ins-aa() { LBUFFER+=aa }
    zle -N ins-aa _-ins-aa
    bindkey a ins-aa

Not as obvious as `noremap a aa' (or `bindkey -s a aa' that results in ``zsh:
string inserting another one too many times'' error), but it works.
Can I be more specific?
I want to bind in bash something equivalent to:
        :inoremap " ""<left>

Now, this mapping is so common that i'm sure that millions of people have tried it before me (for Bash), so would you say that still there is NO WAY that i can bind the above in .inputrc for bash?
 
ZyX
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Using Bash in Vi mode. How to map keys?

ZyX
Reply to message «Re: Using Bash in Vi mode. How to map keys?»,
sent 17:47:30 02 November 2010, Tuesday
by aleCodd:

> Can I be more specific?
>
> I want to bind in bash something equivalent to:
>         :inoremap " ""<left>
>
> Now, this mapping is so common that i'm sure that millions of people have
> tried it before me (for Bash), so would you say that still there is NO WAY
> that i can bind the above in .inputrc for bash?
I use «,s», «,f», «,u», «,h», «,"» «,'» for «()», «{}», «<>», «[]», «""», «''»
respectively. Having «"» mapped to «""» is very annoying when I do not need to
have two double strokes what is common. And having to type symbols from the top
row even without shift (programmer dvorak) is inconvinient.

signature.asc (205 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Using Bash in Vi mode. How to map keys?

aleCodd
ZyX wrote
I use «,s», «,f», «,u», «,h», «,"» «,'» for «()», «{}», «<>», «[]», «""», «''»
respectively. Having «"» mapped to «""» is very annoying when I do not need to
have two double strokes what is common. And having to type symbols from the top
row even without shift (programmer dvorak) is inconvinient.
 
hmm...what do you mean by 's>>' ?
can you please explain 1)what key do you bind 2)and how is it done in .inputrc

cheers
ZyX
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Using Bash in Vi mode. How to map keys?

ZyX
Reply to message «Re: Using Bash in Vi mode. How to map keys?»,
sent 19:15:44 02 November 2010, Tuesday
by aleCodd:

> hmm...what do you mean by 's>>' ?
Do not use google groups, it fucks up unicode symbols. It is was a ``,s''
enclosed in "\u00ab" and "\u00bb" (left and right double angle quotation marks),
googlegroups turn them into << and >>. In zshrc I have the following code:

    bindkey  -M evi    ",h"             "ins-brackets"
    <...>
    bindkey  -M evi    ",S"             "ins-parentheses-escaped"

where all ins-* are custom zle widgets. It is not the right place to discuss
their implementation, but here is similar code from my vimrc:
    inoremap ,ef           <C-o>I{<C-m><C-o>o}<C-o>O
    inoremap ,eF            <C-m>{<C-m><C-o>o}<C-o>O
    inoremap ,F              {<C-o>o}<C-o>O
    inoremap ,f               {}<C-\><C-o>h
    inoremap ,h               []<C-\><C-o>h
    inoremap ,s               ()<C-\><C-o>h
    inoremap ,u            <LT>><C-\><C-o>h
    inoremap ,es               (<C-\><C-o>E<C-o>a)<C-\><C-o>h
    inoremap ,H           [[::]]<C-o>F:
    inoremap ,eh            [::]<C-o>F:
    inoremap ,q           «»<C-\><C-o>h
    inoremap ,Q           „“<C-\><C-o>h
    inoremap ,eq          “”<C-\><C-o>h
    inoremap ,eQ          ‘’<C-\><C-o>h
    inoremap ,"           ""<C-\><C-o>h
    inoremap ,'           ''<C-\><C-o>h

Note that `,q', `,Q', `,eq' and `,eQ' lines will probably be also fucked up by
googlegroups, so it is better to view this message somewhere else, perhaps in a
mail client.

Here is from ipythonrc. Ipython uses readline, so it should be easy to port it
to .inputrc:
    readline_parse_and_bind ",s": "()\C-b"
other lines are similar.

> can you please explain 1)what key do you bind 2)and how is it done in
> .inputrc
I am using zsh, so nothing is done in inputrc.

signature.asc (205 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Using Bash in Vi mode. How to map keys?

aleCodd
thanks so much
just for curiosity, why do you have in the last 5 or so commands the <c-/>, why is not the <c-o> enough to enter a normal mode command before returning to insert mode..

and just to make sure, bindkey is a zsh command, and if so how do you rate zsh vs. bash when it comes to command line editing, which is more vi-like configurable?
ZyX
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Using Bash in Vi mode. How to map keys?

ZyX
Reply to message «Re: Using Bash in Vi mode. How to map keys?»,
sent 19:57:06 02 November 2010, Tuesday
by aleCodd:

> just for curiosity, why do you have in the last 5 or so commands the <c-/>,
> why is not the <c-o> enough to enter a normal mode command before returning
> to insert mode..
Because it moves one character left at the end of line and does not move
anything in the middle.

> and just to make sure, bindkey is a zsh command, and if so how do you rate
> zsh vs. bash when it comes to command line editing, which is more vi-like
> configurable?
While you are using zsh, you can configure almost anything. I do not know how to
bind a key to a custom function that does something with the command line, but I
do not say that this is impossible: I switched to zsh when I had three aliases,
two binds and no functions in my bashrc, so I do not have enough experience in
configuring bash. In zsh I do not use normal/insert modes, but have some vim-
like keybindings: <C-o> that can execute some commands in «normal» mode, <C-r>*,
a bunch of «,*» mappings. If you want to compare, try asking on stackoverflow
how to imitate vim's <C-o> in bash while having a emacs keys, based on those
answers I could yield more useful comparison.

I also know, that in zsh you may setup as many modes as you want: that indicator
was based onto the fact that zsh was changing its keymap when moving between
vicmd/viins, so it is not something hard-coded like in bash.

In some recent topics discussed on zsh mailing list there was an experimental
implementation of dynamic higlighting (though it was capable almost only for
higlighting command as alias/command/builtin/function). Can you imagine
something like that implemented without patching in bash?

signature.asc (205 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Using Bash in Vi mode. How to map keys?

aleCodd
ZyX wrote
Reply to message «Re: Using Bash in Vi mode. How to map keys?»,
sent 19:57:06 02 November 2010, Tuesday
by aleCodd:

> just for curiosity, why do you have in the last 5 or so commands the <c-/>,
> why is not the <c-o> enough to enter a normal mode command before returning
> to insert mode..
Because it moves one character left at the end of line and does not move
anything in the middle.

what do you mean by that? <c-o> waits for a command and reenters insert mode, but where does it MOVE?

> and just to make sure, bindkey is a zsh command, and if so how do you rate
> zsh vs. bash when it comes to command line editing, which is more vi-like
> configurable?
While you are using zsh, you can configure almost anything. I do not know how to
bind a key to a custom function that does something with the command line, but I
do not say that this is impossible: I switched to zsh when I had three aliases,
two binds and no functions in my bashrc, so I do not have enough experience in
configuring bash. In zsh I do not use normal/insert modes, but have some vim-
like keybindings: <C-o> that can execute some commands in «normal» mode, <C-r>*,
a bunch of «,*» mappings. If you want to compare, try asking on stackoverflow
how to imitate vim's <C-o> in bash while having a emacs keys, based on those
answers I could yield more useful comparison.

I also know, that in zsh you may setup as many modes as you want: that indicator
was based onto the fact that zsh was changing its keymap when moving between
vicmd/viins, so it is not something hard-coded like in bash.

In some recent topics discussed on zsh mailing list there was an experimental
implementation of dynamic higlighting (though it was capable almost only for
higlighting command as alias/command/builtin/function). Can you imagine
something like that implemented without patching in bash?

 really appreciate your detailed explanation, so i guess immigrating to zsh land will make things easier in the end, too bad that it will take another month or so to get used to it with all its commands etc.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Using Bash in Vi mode. How to map keys?

aleCodd
In reply to this post by ZyX
<quote author="ZyX">
Reply to message «Re: Using Bash in Vi mode. How to map keys?»,
sent 19:57:06 02 November 2010, Tuesday
by aleCodd:

> just for curiosity, why do you have in the last 5 or so commands the <c-/>,
> why is not the <c-o> enough to enter a normal mode command before returning
> to insert mode..
Because it moves one character left at the end of line and does not move
anything in the middle.
<unquote>

what do you mean by that? <c-o> waits for a command and reenters insert mode, but where does it MOVE?
> and just to make sure, bindkey is a zsh command, and if so how do you rate
> zsh vs. bash when it comes to command line editing, which is more vi-like
> configurable?
While you are using zsh, you can configure almost anything. I do not know how to
bind a key to a custom function that does something with the command line, but I
do not say that this is impossible: I switched to zsh when I had three aliases,
two binds and no functions in my bashrc, so I do not have enough experience in
configuring bash. In zsh I do not use normal/insert modes, but have some vim-
like keybindings: <C-o> that can execute some commands in «normal» mode, <C-r>*,
a bunch of «,*» mappings. If you want to compare, try asking on stackoverflow
how to imitate vim's <C-o> in bash while having a emacs keys, based on those
answers I could yield more useful comparison.

I also know, that in zsh you may setup as many modes as you want: that indicator
was based onto the fact that zsh was changing its keymap when moving between
vicmd/viins, so it is not something hard-coded like in bash.

In some recent topics discussed on zsh mailing list there was an experimental
implementation of dynamic higlighting (though it was capable almost only for
higlighting command as alias/command/builtin/function). Can you imagine
something like that implemented without patching in bash?
<unquote>
 really appreciate your detailed explanation, so i guess immigrating to zsh land will make things easier in the end, too bad that it will take another month or so to get used to it with all its commands etc.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Using Bash in Vi mode. How to map keys?

Benjamin R. Haskell-8
On Tue, 2 Nov 2010, aleCodd wrote:

> Reply to message «Re: Using Bash in Vi mode. How to map keys?»,
> sent 19:57:06 02 November 2010, Tuesday
> by aleCodd:
>
>> just for curiosity, why do you have in the last 5 or so commands the
>> <c-/>, why is not the <c-o> enough to enter a normal mode command
>> before returning to insert mode..
> Because it moves one character left at the end of line and does not
> move anything in the middle.
> <unquote>
>
> what do you mean by that? <c-o> waits for a command and reenters
> insert mode, but where does it MOVE?

From :help i_CTRL-\_CTRL-O

"""
The CTRL-O command sometimes has a side effect: If the cursor was beyond
the end of the line, it will be put on the last character in the line.
In mappings it's often better to use <Esc> (first put an "x" in the
text, <Esc> will then always put the cursor on it).  Or use CTRL-\
CTRL-O, but then beware of the cursor possibly being beyond the end of
the line.
"""

--
Best,
Ben

--
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
ZyX
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Using Bash in Vi mode. How to map keys?

ZyX
In reply to this post by aleCodd
Reply to message «Re: Using Bash in Vi mode. How to map keys?»,
sent 21:15:14 02 November 2010, Tuesday
by aleCodd:

> > what do you mean by that? <c-o> waits for a command and reenters insert
> > mode, but where does it MOVE?
Just try it while at the end of non-empty line and see where cursor moves. Don't
you think that there is some purpose of existance of <C-\><C-o> alongside with
<C-o>?

> >  really appreciate your detailed explanation, so i guess immigrating to
> >
> > zsh land will make things easier in the end, too bad that it will take
> > another month or so to get used to it with all its commands etc.
Zsh has reasonable wizard that creates initial zshrc and does not require much
time to get used to it if you are already familiar with bash. Most of bash
functions will work in zsh as well, but you will need to rewrite prompt and
bindings (it looks like they can not invoke any functions so it is easy with
``binkey -s'').

It also has some very useful modules: zpty (an analog of expect), zmv
(rename/copy/link files); ttyctl (restores tty settings after application exited
or crashed), PCRE regex support, hooks for everything, globbing flags (capable
of replacing `find' and any `find | ... | xargs ...' pipe, though with some
perfomance penalty), parameter expansion flags (for example, escaping a string
stored in a variable no more requires sed, just ${(qqq)VAR}), highly
configurable completion... Just start using and you'll never want to switch
back.

PS: It was hard to find where is the text you wrote.

PPS: Most of the message is an offtopic here.

signature.asc (205 bytes) Download Attachment
Loading...