Commands from insert mode

69 views
Skip to first unread message

Ed Kostas

unread,
Nov 7, 2012, 9:11:48 PM11/7/12
to vim...@googlegroups.com
I want to issue commands from the insert mode. I am aware that a lot of people tried it before me, but I could not find a satisfactory answer in discussion lists.

I don't like to use the arrows to move the cursor, since they are somewhat out of reach. My idea is to maintain the control key down, and press the HJKL keys. Therefore, I prepared the following imap calls:

:imap <C-h> <Left>
:imap <C-j> <Down>
:imap <C-k> <Up>
:imap <C-l> <Right>

These mappings worked as expected. Then I decided to add a few erase operations. I thought it would be simple. The first two trials worked perfectly well:

:imap <C-x> <Delete>
:imap <C-z> <BS>

However, I would like to have the command mode X, that erases the character before the cursor. I tried:

:imap <C-X> <BS>

It seems that vim considers <C-x> and <C-X> to be the same key. Of course, <C-X> is pretty tough to press, but I would like to keep it for symmetry.

My question is: How to make insertion mode <C-X> work like normal mode X?

Suresh Govindachar

unread,
Nov 7, 2012, 10:18:28 PM11/7/12
to vim...@googlegroups.com, edu5...@gmail.com

On 11/7/2012 6:11 PM, Ed Kostas wrote:
>
> I want to issue commands from the insert mode.
>
[ how to distinguish between lower and upper case X in
the following imaps ]

> :imap <C-x> <Delete>
> :imap <C-X> <BS>

Use <C-S- (S for shift), for example:

:imap <C-S-X> <BS>
:imap <C-S-X> <esc>Xi

--Suresh



John Little

unread,
Nov 8, 2012, 6:31:45 AM11/8/12
to vim...@googlegroups.com
On Thursday, November 8, 2012 3:11:48 PM UTC+13, Ed Kostas wrote:

> My question is: How to make insertion mode <C-X> work like normal mode X?

You seem not to know about insert mode control O. See

:h i_ctrl-O

So,
:inoremap <c-x> <c-o>X

Regards, John

Tim Chase

unread,
Nov 8, 2012, 8:56:59 AM11/8/12
to vim...@googlegroups.com, Suresh Govindachar, edu5...@gmail.com
On 11/07/12 21:18, Suresh Govindachar wrote:
> [ how to distinguish between lower and upper case X in
> the following imaps ]
>
> > :imap <C-x> <Delete>
> > :imap <C-X> <BS>
>
> Use <C-S- (S for shift), for example:
>
> :imap <C-S-X> <BS>
> :imap <C-S-X> <esc>Xi

AFAIK, most versions of Vim don't distinguish between <C-{letter}>
and <C-S-{letter}> as they send the same ASCII code. You might be
able to distinguish between, say <C-F2> and <C-S-F2> if you wanted
to use the function keys. Likewise, I don't think control+number is
caught in most cases, let alone control+shift+number (the exception
being control+@ and control+^ both of which Vim uses, but can be
remapped).

-tim


Ed Kostas

unread,
Nov 8, 2012, 10:37:04 AM11/8/12
to vim...@googlegroups.com, Suresh Govindachar, edu5...@gmail.com
> AFAIK, most versions of Vim don't distinguish between <C-{letter}>
>
> and <C-S-{letter}> as they send the same ASCII code. You might be
>
> able to distinguish between, say <C-F2> and <C-S-F2> if you wanted
>
> to use the function keys. Likewise, I don't think control+number is
>
> caught in most cases, let alone control+shift+number (the exception
>
> being control+@ and control+^ both of which Vim uses, but can be
>
> remapped).
>
> -tim

Hi, Tim.

Your answer was very useful. Since I cannot use <C-S-X>, I mapped the backspace key to z. Although <C-z> is not symmetrical with the X command, it is more practical, since it is very difficult to press <C-S-X>. However, I am having another problem. I hope you can help me with it.

I am new to Vim, as you can see (five days of practice, to be exact). In any case, I wrote the following script, that worked perfectly well in the machine I am using to perform experiments:

:imap <C-h> <Left>
:imap <C-j> <Down>
:imap <C-k> <Up>
:imap <C-l> <Right>

:imap <C-x> <Delete>
:imap <C-z> <BS>

:imap <C-w> <C-o>:w<CR>

However, the script is not working in about 40 machines. The machines are all of the same brand (HP), and roughly the same model. All machines are running Zorin OS, that is built on top of Ubuntu. As far as I am aware, all machines have the following programs: Ubuntu, Zorin, konsole, rlwrap (that was necessary to run a shell inside vim), sbcl Common Lisp, Emacs, Latex, Latex-extra, vim, cream, firefox, google.

In the machines where the script does not work, when one types <C-x> in INSERTION mode, the command erases the char below the cursor, and put the cursor one char ahead, leaving a blank behind. In fewer words, the <C-x> command is behaving almost like a replace space bar.

Ben Fritz

unread,
Nov 8, 2012, 11:02:48 AM11/8/12
to vim...@googlegroups.com, Suresh Govindachar, edu5...@gmail.com
On Thursday, November 8, 2012 7:56:27 AM UTC-6, Tim Chase wrote:
> On 11/07/12 21:18, Suresh Govindachar wrote:
> > [ how to distinguish between lower and upper case X in
> > the following imaps ]
> >
> > > :imap <C-x> <Delete>
> > > :imap <C-X> <BS>
>
> AFAIK, most versions of Vim don't distinguish between <C-{letter}>
> and <C-S-{letter}> as they send the same ASCII code. You might be
> able to distinguish between, say <C-F2> and <C-S-F2> if you wanted
> to use the function keys. Likewise, I don't think control+number is
> caught in most cases, let alone control+shift+number (the exception
> being control+@ and control+^ both of which Vim uses, but can be
> remapped).
>

However, Vim CAN distinguish between <A-x> and <A-X> (alt+x and alt+X). If you change your modifier key you should be set.

Ben Fritz

unread,
Nov 8, 2012, 11:04:10 AM11/8/12
to vim...@googlegroups.com, Suresh Govindachar, edu5...@gmail.com
On Thursday, November 8, 2012 9:37:04 AM UTC-6, Ed Kostas wrote:
> However, the script is not working in about 40 machines. The machines are all of the same brand (HP), and roughly the same model. All machines are running Zorin OS, that is built on top of Ubuntu. As far as I am aware, all machines have the following programs: Ubuntu, Zorin, konsole, rlwrap (that was necessary to run a shell inside vim), sbcl Common Lisp, Emacs, Latex, Latex-extra, vim, cream, firefox, google.
>
> In the machines where the script does not work, when one types <C-x> in INSERTION mode, the command erases the char below the cursor, and put the cursor one char ahead, leaving a blank behind. In fewer words, the <C-x> command is behaving almost like a replace space bar.

Check that the version of Vim is the same, and that you are running Vim in the same terminal application. Or start using gvim instead.

Alejandro Exojo

unread,
Nov 8, 2012, 4:41:15 PM11/8/12
to vim...@googlegroups.com
El Jueves, 8 de noviembre de 2012, Ed Kostas escribió:
> :imap <C-h> <Left>
> :imap <C-j> <Down>
> :imap <C-k> <Up>
> :imap <C-l> <Right>

I'm not going to answer what you asked, but since in other mail you said that
you are beginning, I'm going to try to explain you something way more
important:

You are trying to avoid normal by adding in insert mode several mappings.
DON'T DO THAT!

Is called normal mode for a reason. You normally want to be in that mode. As
Drew Neil explains in his Practical Vim book (that I totally recommend), a
painter doesn't necessarily spend most of the time with the brush leaning on
the canvas.

You are losing the main reason that makes Vim useful if you don't use modes.
Even worse: many important Vim features like the dot command are completely
based on the change of modes.

Also, if you really, really want to map something, make sure that you make a
good decision on what you use. The reason is that you may be hiding, and this
making more difficult to use the functionality that Vim provides. For example:

If you map <C-x> in insert mode, you will need to find another key to use the
completion that is provided when you press <C-x> followed by another key.

If you map <C-h> you may have problems with the backspace key (in some cases
Vim will see the backspace and <C-h> as the same key).

And so on...

--
Alex (a.k.a. suy) | GPG ID 0x0B8B0BC2
http://barnacity.net/ | http://disperso.net

Ed Kostas

unread,
Nov 8, 2012, 9:48:01 PM11/8/12
to vim...@googlegroups.com
Hi, Alejandro.

Thank you for your letter. In fact, I have heard a lot of arguments from people that claim that I should get used to the dichotomy between normal mode, and insertion mode. I agree with these argument, and in my private installation of Vim, I don't mess with vim key mapping. However, I would appreciate if you could answer me the following argument of a writer whose name is Caroline Shore.

There are people who use the editor to browse text. The go around the text looking for a given phrase, error, sequence of chars, etc. For instance, an engineer who is trying to configure a monitor editing the /etc/profile is in this situation. In this case, using the normal mode to move the cursor, or erasing a few chars makes a lot of sense. When the need arise for typing, the engineer can escape to insertion mode.

In the world at large, besides the engineer, there are creative writers who seldom move the cursor, or even erase characters, or make corrections. These people just type forward. In the old days of typewriters, they would type their manuscripts, and almost never would make corrections, or erase text. In fact, with a typewriter, the result of correction is not very good. This kind of writers live in the insertion mode, with short incursions to normal mode. My question: In this case, do you think that it would make sense to provide a short escape into normal mode?

I prepared the map for lawyers, that prepare formal documents. The type very fast, and now and them a wrong char slip in the line; then they want to move the cursor one or two chars backward, erase the wrong insertion with x, and return to their typing forward. These lawyers claim that they prefer Emacs over Vim because Emacs is more productive for this kind of writing.

By the way, nowadays lawyers plead electronically. I mean, instead of typing the document, printing the document, and submitting a hard copy to the judge, they prepare the document in LaTeX with a text editor (say, Emacs), produce a pdf output (say, with pdflatex), and submit the result to the court. A text editor, like Vim, with a very fast start up is appreciated.

If you have good arguments for not using commands from insertion mode, even for people who type most of the time, and seldom need to move the cursor or erase a char, I would be happy to hear your arguments. In fact, I am eager to hear the other side of this story.

Below I put a list of reasons for my lawyer friends appreciating Vim, as compared with other editors, like Eclipse, gedit, and even Emacs.

1 -- Vim has a very fast start up. It is almost instantaneous. This is very important when you need to type a short document, LaTeX compile it, and submit the pdf file as soon as a submission window opens.

2 -- Vim is relatively small. The compiled application has less than 7 megabytes. You can copy it in a pen drive, take it to another town, copy it into the BAR association computer, do your working, and erase Vim. With Eclipse, you would need to ask permission to install Java, download 200 megabytes, install Eclipse, prepare your documents, uninstall Eclipse, uninstall Java, etc.

3 -- It has scripts. Therefore you can configure your keyboard to speed your work.

Suresh Govindachar

unread,
Nov 8, 2012, 10:20:32 PM11/8/12
to vim...@googlegroups.com
On 11/8/2012 6:48 PM, Ed Kostas wrote:> On Thursday, November 8, 2012
7:41:49 PM UTC-2, Alejandro Exojo wrote:
>> El Jueves, 8 de noviembre de 2012, Ed Kostas escribi�:
I use vim a lot -- most of my life is spent in a vim buffer -- and I
agree with the lawyers. I am an engineer and when I start a project
(e.g., a new .h or .cc file), most of my time is spent in insert mode
with brief excursions to normal mode.

Here's my "Mappings to facilitate the creation of text" from 2001:
http://vim.wikia.com/wiki/Mappings_to_facilitate_the_creation_of_text

Something else that you might find interesting "Enhanced Command Window"
from 2004: http://vim.wikia.com/wiki/Enhanced_command_window

--Suresh


Ed Kostas

unread,
Nov 9, 2012, 12:32:34 PM11/9/12
to vim...@googlegroups.com

>
> I use vim a lot -- most of my life is spent in a vim buffer -- and I
>
> agree with the lawyers. I am an engineer and when I start a project
>
> (e.g., a new .h or .cc file), most of my time is spent in insert mode
>
> with brief excursions to normal mode.
>
>
>
> Here's my "Mappings to facilitate the creation of text" from 2001:
>
> http://vim.wikia.com/wiki/Mappings_to_facilitate_the_creation_of_text
>
>
>
> Something else that you might find interesting "Enhanced Command Window"
>
> from 2004: http://vim.wikia.com/wiki/Enhanced_command_window
>
>
>
> --Suresh

Hi, Suresh.

Your script is exactly what I wanted all the time. Therefore, I don't have any work left. All I have to do is downloading your scripts, and start typing. However, I have a question. In the distribution of your mapping, there is a warning: "The contents may not be useful for Vim 7." What does it mean? The mapping may not work for Vim 7.3? I run a few test, and everything seems ok.

Alejandro Exojo

unread,
Nov 9, 2012, 5:01:54 PM11/9/12
to vim...@googlegroups.com
El Viernes, 9 de noviembre de 2012, Ed Kostas escribió:
> In the world at large, besides the engineer, there are creative writers who
> seldom move the cursor, or even erase characters, or make corrections.
> These people just type forward. In the old days of typewriters, they would
> type their manuscripts, and almost never would make corrections, or erase
> text. In fact, with a typewriter, the result of correction is not very
> good. This kind of writers live in the insertion mode, with short
> incursions to normal mode. My question: In this case, do you think that it
> would make sense to provide a short escape into normal mode?

Besides being an engineer, I also wrote prose in LaTeX and DocBook (well,
mostly technical documents, but with some long paragraphs without equations or
figures in a row). But I'm the kind of person that tries to read, correct and
polish what I wrote two or three times. So, in my case, even if I might write
a long burst of text in insert mode without escaping, I still go back and make
changes later on.

So I'm maybe not the kind of user that you are trying to set up Vim for. Even
then, I would still try to find a way to go quickly to normal mode, and then do
movements there. There are many ways to make returning to normal mode faster.
I have mapped "jj" and "kk" to behave like <Esc> in insert mode, and some
people like to use "jk" or some key pressed with CTRL.

Then, in normal mode, you can move back very easily with "b" or "h". I like
this more than a press and hold key with CTRL.

One last detail that I forgot in the other email: by default, Vim will not
undo every keystroke that you added in insert mode. That's another good reason
to go to normal mode once in a while.

A nice suggestion for your use case (that is useful both if you want to avoid
normal mode or you use it often) is mapping <Space> to <C-g>u<Space>.
Something like:

inoremap <Space> <C-g>u<Space>

That has no visible effect at all on each white space, but adds an "undo break"
each time you press that key. That will make possible that Vim undoes words
separated by spaces.

Good luck!

Suresh Govindachar

unread,
Nov 9, 2012, 8:33:31 PM11/9/12
to vim...@googlegroups.com
That warning was made by some Wiki Administrator who did not
understand the tip. I'll start a new thread to contact him.

--Suresh

Anthony Campbell

unread,
Nov 10, 2012, 10:37:11 AM11/10/12
to vim...@googlegroups.com
On 08 Nov 2012, Ed Kostas wrote:

[snip]

> In the world at large, besides the engineer, there are creative
> writers who seldom move the cursor, or even erase characters, or make
> corrections. These people just type forward. In the old days of
> typewriters, they would type their manuscripts, and almost never would
> make corrections, or erase text. In fact, with a typewriter, the
> result of correction is not very good. This kind of writers live in
> the insertion mode, with short incursions to normal mode. My question:
> In this case, do you think that it would make sense to provide a short
> escape into normal mode?

I find the thought of creative [sic] writers who "almost never make
corrections" deeply depressing although perfectly believable in the
light of the sloppy verbose writing I encounter these days, even in
books from well-known publishers.

I do all my initial drafts of books and other material in (g)vim and
rewrite endlessly, with lots of cutting and pasting. In the old days I
used to write everything on a typewriter and was continually striking
stuff out and rewriting it, which resulted in the frequent need to
retype whole pages to make them presentable. The thought of going back
to that fills me with horror.

I have no difficulty with Insert/Normal modes though I have made one or
two mappings for things I do frequently, such as formatting a paragraph.
I find it helpful to have the useless and annoying CapsLock key mapped
to Esc.

[snip]


> By the way, nowadays lawyers plead electronically. I mean, instead of
> typing the document, printing the document, and submitting a hard copy
> to the judge, they prepare the document in LaTeX with a text editor
> (say, Emacs), produce a pdf output (say, with pdflatex), and submit
> the result to the court. A text editor, like Vim, with a very fast
> start up is appreciated.


I should have thought that Lyx would be better for this than either vim
or emacs - it would save a lot of time. My own practice for "creative"
writing is to work in vim with lots of cutting and pasting until I've
got things more or less as I want them and then to paste the result into
Lyx if I want to print it as a book or something similar.

Anthony

--
Anthony Campbell - a...@acampbell.org.uk
http://www.reviewbooks.org.uk
http://www.skepticviews.org.uk
http://www.acupuncturecourse.org.uk
http://www.smashwords.com/profile.view/acampbell




Chris Lott

unread,
Nov 10, 2012, 10:52:03 AM11/10/12
to vim...@googlegroups.com
On Sat, Nov 10, 2012 at 6:37 AM, Anthony Campbell <a...@acampbell.org.uk> wrote:
> On 08 Nov 2012, Ed Kostas wrote:
>
> [snip]
>
>> In the world at large, besides the engineer, there are creative
>> writers who seldom move the cursor, or even erase characters, or make
>> corrections. These people just type forward.
>
>
> I find the thought of creative [sic] writers who "almost never make
> corrections" deeply depressing although perfectly believable in the
> light of the sloppy verbose writing I encounter these days, even in
> books from well-known publishers.

That might be the least charitable way of interpreting the original
comments. I took it to mean that writers of prose tend to "write
forward" a lot more often and for longer durations than, perhaps,
programmers do. But not necessarily that this is the *only* way in
which they work.

I write almost nothing but prose (using Markdown). Vim works quite
well for that. But I do understand, particularly when drafting, that
the bouncing between insert and normal modes adds a bit of friction.
It took me a long time to switch from Emacs because at the drafting
stage it remained more efficient than Vim. In terms of pure speed
while drafting, a non-modal editor is probably technically more
efficient in some ways, but overall it's a win.

I'm still working on methods to make that stage of my writing process
more efficient. I absolutely understand the desire to make normal mode
more suited to that particular way of working (recognizing that there
are, hopefully, further editing stages involved :) It has helped me to
consider Vim a text processing engine rather than an editor.

c
--
Chris Lott <ch...@chrislott.org>

John Little

unread,
Nov 10, 2012, 8:29:28 PM11/10/12
to vim...@googlegroups.com
In the previous interesting discussion about how writers use vim, no one has mentioned vim with cream

http://cream.sourceforge.net/

which is "a modern configuration" of vim, with "A single editing mode".

Now, having started with vi in 1983, cream is not for me, but appears to have all the mappings that the OP wanted, in a consistent, considered, and supported fashion.

Regards, John Little

Anthony Campbell

unread,
Nov 11, 2012, 4:52:15 AM11/11/12
to vim...@googlegroups.com
On 10 Nov 2012, Chris Lott wrote:

[snip]

> > I find the thought of creative [sic] writers who "almost never make
> > corrections" deeply depressing although perfectly believable in the
> > light of the sloppy verbose writing I encounter these days, even in
> > books from well-known publishers.
>
> That might be the least charitable way of interpreting the original
> comments. I took it to mean that writers of prose tend to "write
> forward" a lot more often and for longer durations than, perhaps,
> programmers do. But not necessarily that this is the *only* way in
> which they work.

Sorry if it is uncharitable, but I still find it difficult to understand
how it is possible to write extended prose from scratch without making
numerous corrections, unless you are Bertrand Russell, who apparently
wrote like that - with superb results. Of course, the writers referred
to might simply be copying out material they had previously written in
longhand, as many good authors still do. I used to work in that way
years ago, though I still found myself changing things a lot when I
came to the typing stage.

In books from reputable publishers I've recently found paragraph breaks
in the middle of sentences, words needlessly split by hyphens which are
not at line breaks, and, of course, innumerable spelling mistakes that
sometimes reverse the intended meaning. It becomes obvious that the
original text was drafted at speed on a word processor and no proofs
were read either by the author or by a copy editor. And then
conventional publishers have the cheek to reprove self-published authors
for sloppy productions! [OK, rant over]

>
> I write almost nothing but prose (using Markdown). Vim works quite
> well for that. But I do understand, particularly when drafting, that
> the bouncing between insert and normal modes adds a bit of friction.
> It took me a long time to switch from Emacs because at the drafting
> stage it remained more efficient than Vim. In terms of pure speed
> while drafting, a non-modal editor is probably technically more
> efficient in some ways, but overall it's a win.
>
> I'm still working on methods to make that stage of my writing process
> more efficient. I absolutely understand the desire to make normal mode
> more suited to that particular way of working (recognizing that there
> are, hopefully, further editing stages involved :) It has helped me to
> consider Vim a text processing engine rather than an editor.
>

Thanks for the tip about Markdown - I'll look into it. I must also think
further about ways of making vim more efficient in Insert mode. I've
never really tried Emacs very much, but when I migrate my drafts to Lyx,
which I do for books, I am working modelessly. I know that many writers
use Lyx from scratch, but I find it easier to make big changges in Vim.

In my early forays into computing, years ago, I was using DOS, with a
word processor called Protext which was modal. When I made the switch to
Linux I found that Vim and Protext were quite similar in principle,
which is probably why I stayed with Vim.

Chris Lott

unread,
Nov 11, 2012, 12:02:45 PM11/11/12
to vim...@googlegroups.com
On Sun, Nov 11, 2012 at 12:52 AM, Anthony Campbell <a...@acampbell.org.uk> wrote:
>
>
> Sorry if it is uncharitable, but I still find it difficult to understand
> how it is possible to write extended prose from scratch without making
> numerous corrections,

The question is *when* (or *if*) those corrections and changes are
being made. Whether writing by hand or with the keyboard, the drafting
stage (or the "at speed" stage, as you put it), doesn't necessarily
involve much in the way of correction or the need to go out of insert
mode in significant ways. If the whole process *stops* at the drafting
stage, then there's a real problem.

I know I'm not the only writer who finds it makes my work weaker if I
don't allow myself this free drafting stage. If I let myself get
caught up in editing at that most creative and fertile point, my
writing is weaker.

The odd thing about Vim in this respect is that editors featuring a
"typewriter mode" for drafting have become commonplace...but this is
essentially what Vim is for beginners...once they get into Insert mode
and aren't sure how to get out.

Anthony Campbell

unread,
Nov 12, 2012, 4:31:26 AM11/12/12
to vim...@googlegroups.com
In spite of what I said previously, I agree with much of this, so I'm
prepared to contradict myself to some extent. It is generally best to
write first drafts at speed and revise later. The late F.L. Lucas said
this in his book _Style_ (still, in my opinion, one of the best
discussions of how to write, even though it apppeared 60 years ago), and
I'm sure he was right. (But he also cautioned against writing _too_
fast.)

I find that I can do this efficiently in Vim in spite of its being
modal, and I prefer this to trying to remember complicated key presses
as in Emacs. I usually write my first drafts _each day_ with
few changes apart from correcting typos, which I'm constitutionally
unable to leave untouched. Next day I read what I wrote previously and
sometimes make quite large changes. Sometimes I make notes for possible
alternatives, distinguishing them from the main text by enclosing them
in asterisks. After this process has gone on for some days or weeks I
often save this version as a reserve in case I want to go back to it
later and then continue adding text and making changes to the current
version.

What all this amounts to is that Vim is quite usable for quick first
drafts, at least for me, without the need to make it fully non-modal.
(In fact, I think that might actually slow things down.) But it is
useful to have short cuts for actions one does quickly, such as
formatting paragraphs and saving files, and this discussion has prompted
me to think of further ways of achieving this.


--
Anthony Campbell - a...@acampbell.org.uk
http://www.acampbell.org.uk
i


Reply all
Reply to author
Forward
0 new messages