hjkl-ing while in insert mode

67 views
Skip to first unread message

soundphed

unread,
Feb 12, 2009, 3:26:14 AM2/12/09
to vim_use
Hi there, I am wondering if it might be neat to be able to move around
in insert mode via the h,j,k,l keys, as in normal mode.
Say like with a modifier key, so you could hold down <ctrl> and hjkl
would then move the cursor while keeping you in insert mode.

Does something of this nature already exist in vim?
If not, how does one move the cursor left,right,up,down while in
insert mode?

Albie

unread,
Feb 12, 2009, 3:49:27 AM2/12/09
to vim...@googlegroups.com

Not as far as I know (besides the obvious up, down, left right). I have
this in my vimrc:

" allow for emacs-like movement in insert mode
noremap! <C-p> <up>
noremap! <C-n> <down>
noremap! <C-f> <right>
noremap! <C-b> <left>
noremap! <M-f> <C-right>
noremap! <M-b> <C-left>

You would of course change the above by substituting hjkl instead of
bnpf. The abovementioned mappings would work in command-line mode as
well, which was the original reason for me adding it.

Regards
--

[ Albie Janse van Rensburg ~ http://morph.telspace.co.za ]

A foolish consistency is the hobgoblin of little minds.
-- Ralph Waldo Emerson

Tony Mechelynck

unread,
Feb 12, 2009, 4:07:28 AM2/12/09
to vim...@googlegroups.com

Well, of course h j k l in Insert modes insert the letters h j k l into
your text, and I ton't think you would want to type only text containing
none of these four letters.

With Ctrl, Ctrl-J is linefeed (which Vim handles as Enter IIUC) and
Ctrl-H is Backspace so I think that's excluded too. (And the presence or
absence of the Shift modifier is immaterial for the meaning of Ctrl +
letter).

Alt-h to Alt-l are synonyms to è é ê ë respectively. I don't know about
you, but I wouldn't want to remap these letters, all of which are used
with various frequencies in my native language (French). Alt-H to Alt-L
(or Alt-shift-h to Alt-shift-l) are the uppercase counterparts of these
accented letters. You might try Ctrl-Alt-h to Ctrl-Alt-l if your gvim
sees them. (In Latin1 or UTF-8 the equivalent characters, 0x88 to 0x8B,
are rarely-used control characters, but if your preferred encoding is
Windows-1252 they might collide with something you might want to use, I
don't know).

The standard way of moving the cursor in Insert mode, even if it makes
you puke, is by using the cursor-movement keys. You could also use the
Ctrl-O prefix, see |i_CTRL-O|, but it isn't very practical for such
constantly-used keys. Another possibility would be to move your hand
straight up and use F5 to F8 (after remapping them to <Left> <Down> <Up>
and <Right> respectively).

And in case you didn't know, the latter is achieved by

:map! <F5> <Left>
:map! <F6> <Down>
:map! <F7> <Up>
:map! <F8> <Right>


Best regards,
Tony.
--
BRIDGEKEEPER: What is the air-speed velocity of an unladen swallow?
ARTHUR: What do you mean? An African or European swallow?
BRIDGEKEEPER: Er ... I don't know that ... Aaaaarrrrrrggghhh!
BRIDGEKEEPER is cast into the gorge.
"Monty Python and the Holy Grail" PYTHON (MONTY)
PICTURES LTD

Albie Janse van Rensburg

unread,
Feb 12, 2009, 7:27:37 AM2/12/09
to soundphed, vim...@googlegroups.com
soundphed wrote:
> thank you for your answer, that was _exactly_ what i was looking
> for. :)

Just be aware that those mappings will override the default
functionality for those ctrl-<letter> keys. In insert mode, ctrl-p does
word completion, for instance. And as Tony Mechelynck mentioned, ctrl-k
starts digraph entry, which is very useful for typing in a non-English
language.

I have actually now restricted my mappings to only the command-line mode
(cnoremap), it generally being the most limited mode in Vim, IMO.

Regards

--

[ Albie Janse van Rensburg ~ http://morph.telspace.co.za ]

"Life is too important to take seriously."
-- Corky Siegel

Bertrand Janin

unread,
Feb 12, 2009, 8:48:54 AM2/12/09
to vim...@googlegroups.com
Tony Mechelynck wrote :
> Alt-h to Alt-l are synonyms to è é ê ë respectively. I don't know about
> you, but I wouldn't want to remap these letters, all of which are used
> with various frequencies in my native language (French).

I don't know about windows and mac, but you can map a Compose key on X
that will work also in other apps. I was looking for a nice way to move
around in insert mode, I think Alt-h to Alt-l is the holy grail =)

Bertrand

Ben Fritz

unread,
Feb 12, 2009, 10:24:39 AM2/12/09
to vim_use
I personally avoid moving around in insert mode as much as possible.
If I need to move any more than a single word away, or move to a
different line, I ALWAYS leave insert mode first. To me, "insert mode"
is less of a mode of operation than it is an incomplete command. I
view "itypesometext<esc>" as a single command, not entering a mode,
typing text, then leaving the mode. This actually agrees with the way
Vim treats it, in a way...if you use 'u' to undo after an insert, it
will remove all text inserted since you pressed the 'i' key in the
first place. Basically the entire insertion is treated as a single
operation.

Roberto Miura Honji

unread,
Feb 12, 2009, 10:32:34 AM2/12/09
to vim...@googlegroups.com
I agree with Ben...
I use vim like him.

2009/2/12 Ben Fritz <fritzo...@gmail.com>



--
----------------------------------------------
Roberto Miura Honji
LAS - Laboratório de Administração e Segurança de Sistemas
Engenharia de Computação - 2006
Instituto de Computação - UNICAMP

email: miura...@gmail.com (principal)
email: ra06...@students.ic.unicamp.br
msn:   miura...@msn.com
-------------------------------------------

Bertrand Janin

unread,
Feb 12, 2009, 10:46:59 AM2/12/09
to vim...@googlegroups.com
Ben Fritz wrote :

> I personally avoid moving around in insert mode as much as possible.
> If I need to move any more than a single word away, or move to a
> different line, I ALWAYS leave insert mode first. To me, "insert mode"
> is less of a mode of operation than it is an incomplete command. I
> view "itypesometext<esc>" as a single command, not entering a mode,
> typing text, then leaving the mode. This actually agrees with the way
> Vim treats it, in a way...if you use 'u' to undo after an insert, it
> will remove all text inserted since you pressed the 'i' key in the
> first place. Basically the entire insertion is treated as a single
> operation.

I'm doing less and less operations in insert mode, and I agree that it
is a better way to edit. I think the only situation where I actually
need to move around is when I type something like that (JS):

myFunc({ Obj: "true"})

I tend to write myFunc(), then move one char left, then type {}, etc..
The only better ways I found were:

1. create an imap {} {}<Left>
2. use <C-O>h

The first one is annoying when I want to just type {} alone and I'm
having hard time getting used to the second.. But I'll get there.

Bertrand

Tony Mechelynck

unread,
Feb 13, 2009, 2:53:58 AM2/13/09
to vim...@googlegroups.com
On 12/02/09 13:27, Albie Janse van Rensburg wrote:
> soundphed wrote:
>> thank you for your answer, that was _exactly_ what i was looking
>> for. :)
>
> Just be aware that those mappings will override the default
> functionality for those ctrl-<letter> keys. In insert mode, ctrl-p does
> word completion, for instance. And as Tony Mechelynck mentioned, ctrl-k
> starts digraph entry, which is very useful for typing in a non-English
> language.
>
> I have actually now restricted my mappings to only the command-line mode
> (cnoremap), it generally being the most limited mode in Vim, IMO.
>
> Regards
>

I haven't; but I use only Fnn keys (or shift-Fnn keys), and, in Normal
mode only, non-ASCII keys (my Belgian keyboard has a number of these: ²
³ é § è ç à ° µ £ ù -- not counting the characters obtained with AltGr
or with dead-circumflex and dead-diaeresis) as the {lhs} of my mappings,
unless I explicitly want to override a preexisting key binding. Also, my
mapping for <F1> intentionally overrides the default functionality
(which is "help" as in many programs), as I have: ":map <F1> :bot
help<CR>" to have it open the help window (if not already open) at the
very bottom rather than next to the current window.


Best regards,
Tony.
--
I can read your mind, and you should be ashamed of yourself.

Tony Mechelynck

unread,
Feb 13, 2009, 3:01:11 AM2/13/09
to vim...@googlegroups.com

Well, I wouldn't want to move down one line whenever I try to insert an
e-acute, which is the most frequent accented letter in French. I see YM
does V.

Best regards,
Tony.
--
Democracy means simply the bludgeoning of the people by the people for
the people.
-- Oscar Wilde

Tony Mechelynck

unread,
Feb 13, 2009, 3:38:50 AM2/13/09
to vim...@googlegroups.com

Well, I feel quite comfortable using <Left> <Right> (moving by single
characters), <C-Left> <C-Right> (by words), <Home> <End> (to either end
of the current line), <Up> <Down> (by lines), <PageUp> <PageDown> (by
pages i.e. screenfulls) when in Insert mode -- and each of these
commands stops undo, it's only when you enter characters one after
another without cursor moves that they constitute a single "undo block".
In fact, the following

:inoremap <Space> <Space><Left><Right>
:inoremap <Tab> <Tab><Left><Right>
:inoremap <CR> <CR><Up><Down>

create an undo point after each WORD.

Best regards,
Tony.
--
It is illegal for a driver to be blindfolded while operating a vehicle.
[real standing law in Alabama, United States of America]

Andreas Bernauer

unread,
Feb 13, 2009, 7:55:32 AM2/13/09
to vim...@googlegroups.com
Tony Mechelynck wrote:
> In fact, the following
>
> :inoremap <Space> <Space><Left><Right>
> :inoremap <Tab> <Tab><Left><Right>
> :inoremap <CR> <CR><Up><Down>
>
> create an undo point after each WORD.

Alternatively to <Left><Right>, you could use <C-G>u, which breaks an undo
sequence and does not move the pointer.

--
Andreas.

Ben Fritz

unread,
Feb 13, 2009, 10:02:45 AM2/13/09
to vim_use


On Feb 12, 9:46 am, Bertrand Janin <tamen...@neopulsar.org> wrote:

> I tend to write myFunc(), then move one char left, then type {}, etc..
> The only better ways I found were:
>
>  1. create an imap {} {}<Left>
>  2. use <C-O>h
>
> The first one is annoying when I want to just type {} alone and I'm
> having hard time getting used to the second.. But I'll get there.
>

Maybe you don't need to:

http://vim.wikia.com/wiki/Automatically_append_closing_characters

Tony Mechelynck

unread,
Feb 14, 2009, 5:21:18 AM2/14/09
to vim...@googlegroups.com

True; but (a) these <Left><Right> and <Up><Down> mappings were invented
some time ago, when the latest Vim version (which was some Vim 6.x)
didn't yet know about Ctrl-G u, and (b) they illustrate the fact that
"everything you do from i or a to Esc" can be more than one undo block
if you move the cursor in the meantime, which is what I mentioned just
above those mappings in the same post.

Best regards,
Tony.
--
"But what we need to know is, do people want nasally-insertable
computers?"

David Liang

unread,
Feb 15, 2009, 2:00:38 AM2/15/09
to vim_use
I have found this works well for me: (holding down the ALT or META
key)
inoremap <M-j> <C-O>gj
inoremap <M-k> <C-O>gk
noremap! <M-h> <Left>
noremap! <M-l> <Right>

This works perfectly for GVIM; terminal VIM took a little while
setting up -- see
http://vim.wikia.com/wiki/Mapping_fast_keycodes_in_terminal_Vim
Reply all
Reply to author
Forward
0 new messages