Escolar Documentos
Profissional Documentos
Cultura Documentos
log in
help
tour
Sign up
While in Insert Mode in Vim, is there any way to traverse the text (like moving some characters forward and backward) other than using
the arrow keys ?
This is because obviously if you press
moving through the text.
ctrl
and
while in
Esc
Insert
) and traversing the text then; but obviously that is not productive.
vim
edited Jun 10 '13 at 11:31
timss
3,910
Andreas Grech
2
18
44
48.8k
76
243
319
12 Answers
You seem to misuse vim, but that's likely that you're not very familiar with it.
The right way is to press Esc , go where you want to do a small correction, fix it, go back and
keep editing. It is effective because Vim has much more movements than usual character
forward/backward/up/down. After you learn more of them, this will happen to be more
productive.
Here's a couple of use-cases:
you accidentally typed "accifentally". No problem, the sequence Esc F f r d A will
correct the mistake and bring you back where you've been editing it. F f movement will
bring you back to the first encountered "f" character. Compare with Ctrl + <- -> -> ->
->
del
d
End , that does virtually the same in a casual editor, but takes more
keystrokes, makes you move your hand out of alphanumeric space of the keyboard.
you accidentally typed "you accidentally typed", but want to correct it to "you intentionally
typed". Then Esc 2 b c w will erase the word you want to fix and bring you to
insert mode, so you can immediately retype it. To get back to editing, just press A
instead of End , to reach which you should move your hand
you accidentally typed "mouse" instead of "mice". No problem - the good old Ctrl + W
will delete the previous word without going out from insert mode. And it happens to be
much faster to erase small word than to fix errors in it. I'm so used to it that I had closed
the browser page when I was typing this message...
repetition count is largely underused. Before making a movement, you can type a number;
and the movement will be repeated this number of times. For example, 1 5 h will
bring you 15 characters back and 4 j will scroll you 4 lines down. Start using them and
http://stackoverflow.com/questions/1737163/traversing-text-in-insert-mode
09/04/2016, 4:21 PM
Page 1 of 8
you'll get used soon and find out that pressing 10 times <- key is less fast than iterative
approach to moving cursor, when you type 1 2 h , notice that you made a mistake
and immediately correct yourself with l l .
But, if you still want to do small text traversals without leaving insert mode, follow rson's advice
and use Ctrl + O . As an example, Ctrl + O F + f will move you to previous f character
and leave you in insert mode.
edited Apr 22 '13 at 18:03
Anh Do
5,255
Pavel Shved
5
34
55
48.2k
10
83
141
15
+1 Excellent answer and thanks for all the examples you gave! Andreas Grech Nov 15 '09 at 13:35
44
It's also worth noting that you can use Ctrl+o to issue a single command in normal mode. This can at
times get you where you want to be, especially when combined with the t/T and f/F movement commands.
Randy Morris Nov 16 '09 at 0:27
14
This is a great insight into how to use Vim properly. However, I find pressing "Esc" to be a pain, and so I
think it's worth pointing out that you can map a more accessible key to that function, e.g. "imap jj <esc>".
Symmetric Apr 15 '12 at 1:07
You seem to misuse the browser Mustafa Serdar anl Jul 12 '12 at 1:57
11
To fix a typo you should not leave insert mode. Vim is powerful because you create atomic, repeatable
actions. Inserting a typo, and fixing it creates two separate actions. The right way to fix a typo it to do it
without leaving insert mode. If that mean using arrow keys to navigate there that is fine. Vim does have
some keybindings to navigate in insert mode. Check :h ins-special-keys . ctrl-h : backspace,
ctrl-w : delete word, ctrl-u : delete to beginning of line, alt-b : go back a word. everett1992 Apr
21 '13 at 23:41
Insert mode
Movement
hjkl
Notwithstanding what Pavel Shved said - that it is probably more advisable to get used to
Esc aping Insert mode - here is an example set of mappings for quick navigation within Insert
mode:
" provide hjkl
inoremap <A-h>
inoremap <A-j>
inoremap <A-k>
inoremap <A-l>
h
k
Alt
You have to copy that code into your vimrc file to have it loaded every time you start vim (you
can open that by typing :new $myvimrc starting in Normal mode).
(Other uses of
Alt
in Insert mode)
http://stackoverflow.com/questions/1737163/traversing-text-in-insert-mode
09/04/2016, 4:21 PM
Page 2 of 8
It is worth mentioning that there may be better uses for the Alt key than replicating Normal
mode behaviour: e.g. here are mappings for copying from an adjacent line the portion from the
current column till the end of the line:
" Insert the rest of the line below the cursor.
" Mnemonic: Elevate characters from below line
inoremap <A-e>
\<Esc>
\jl
\y$
\hk
\p
\a
" Insert the rest of the line above the cursor.
" Mnemonic: Y depicts a funnel, through which the above line's characters pour
onto the current line.
inoremap <A-y>
\<Esc>
\kl
\y$
\hj
\p
\a
(I used \ line continuation and indentation to increase clarity - the commands are interpreted
as if written on a single line.)
Reference:
:help insert-index
Command-line mode
This set of mappings makes the upper
" provide hjkl
cnoremap <A-h>
cnoremap <A-j>
cnoremap <A-k>
cnoremap <A-l>
Alt
hjkl
Alternatively, these mappings add the movements both to Insert mode and Command-line
mode in one go:
" provide hjkl
modifier key
noremap! <A-h>
noremap! <A-j>
noremap! <A-k>
noremap! <A-l>
The mapping commands for pulling Normal mode commands to Command-line mode look a
bit different from the Insert mode mapping commands (because Command-line mode lacks
Insert mode's Ctrl + O ):
" Normal
cnoremap
cnoremap
cnoremap
cnoremap
http://stackoverflow.com/questions/1737163/traversing-text-in-insert-mode
09/04/2016, 4:21 PM
Page 3 of 8
CTRL-B
CTRL-E
CTRL-F
'cedit')
CTRL-H
CTRL-W
CTRL-U
CTRL-P
front of the
CTRL-N
front of the
<Up>
front of the
<Down>
front of the
<S-Up>
<S-Down>
<PageUp>
<PageDown>
recall previous
cursor)
recall next
cursor)
recall previous
cursor)
recall next
cursor)
recall previous
recall next
recall previous
recall next
<S-Left>
<C-Left>
<S-Right>
<C-Right>
cursor
cursor
cursor
cursor
left
left
right
right
<LeftMouse>
Reference:
one
one
one
one
word
word
word
word
from
from
from
from
history
history
history
history
:help ex-edit-index
accolade
2,103
13
19
Its great but doesn't work with command line vim. Yugal Jindle Nov 13 '13 at 4:22
@YugalJindle: Thanks! :) I have now added info on tweaking the Command-line mode. (The approach is a
bit different from Insert mode.) accolade Jan 18 '14 at 12:24
What if a man is using wmii and have no winkey Zen Aug 23 '14 at 4:30
@Zen: What do you mean by "winkey"? I have not referenced the Windows key in my answer. Do you mean
the Alt key? accolade Apr 8 '15 at 13:39
h
l
j
k
move
move
move
move
cursor
cursor
cursor
cursor
Ctrl
left
right
down
up
which is probably the simplest way to do what you want and is easy to remember.
Other very useful control keys in insert mode:
CTRL-W
CTRL-O D
CTRL-U
CTRL-H
CTRL-J
CTRL-T
CTRL-D
Atcold
221
felix
15
1,531
16
25
Thank you. This should be considered the correct answer. It is actually helpful. Atcold Mar 2 at 20:56
Many people in the vim community argue that you should not navigate in insert mode, that it is
http://stackoverflow.com/questions/1737163/traversing-text-in-insert-mode
09/04/2016, 4:21 PM
Page 4 of 8
not the vim way. I think this is an incorrect sentiment learned when transitioning from standard
editors to vim.
Vim is most powerful when you use its tools to create atomic, repeatable actions or finds.
It is ok to navigate while in insert mode if you are fixing a mistake you made in the same insert
session. You should not navigate outside of the range of text you modified.
If you make a mistake while entering text and escape out of insert mode to fix it you will not be
able to repeat the intended action, . will repeat the correction.
Vim does support many insert mode navigation keys. Obviously there are the arrow keys,
home, and end, but there are also many other shortcuts. see :h ins-special-keys
answered Apr 21 '13 at 23:34
everett1992
669
20
If you are a vim purist, skip reading this answer. OTOH, if you are new to vim and are looking
for a few helpful tips you wont find in the many hundred of vim tutorials and blogs, read on... :-)
vim
Turn on mouse mode, and use the mouse wheel and clicking
...to navigate large bodies of text.
Most (all) modern computers have a touchpad that is closely integrated with the keyboard
(e.g. MacBooks). Industrial designers have spent many man years optimizing these designs so
that the old problem of having to move the hand away from the keyboard is no longer a real
issue. Okay, it is if you are used to a mouse and don't like to switch, but for anyone new to vim
(like those that might find this post via a search), this should not be much of an issue.
And so...
These are what I'd call more modern (using mouse, scroll wheel, etc.) ways of navigating in
vim, equally effective depending on your preference of input.
HTH
edited Mar 18 at 6:46
Shyam Habarakada
5,738
20
34
@MilesRout yes, I had a disclaimer. But in all seriousness, I would think there is a good proportion of vim
users who's brains work the same as mine. That is ... my brain usually works like "I want the cursor there"
and not like "I want the cursor 5_words_to_the_left". And the conventional way of vim doesn't work that well
for those people. I love vim because a) it is available everywhere and b) has powerful features. I don't
however feel the need to conform to all it's default mechanisms. Shyam Habarakada Oct 14 '14 at 23:08
http://stackoverflow.com/questions/1737163/traversing-text-in-insert-mode
09/04/2016, 4:21 PM
Page 5 of 8
@MilesRout There is a lot of religion around this sort of thing in vim, so I won't debate with you on this. The
repetition count method doesn't help me, and the accepted answer also calls it out as something heavily
underused. I can only guess why that is the case. Using w, b etc. for navigating to nearby places are all
great, but for going further, the mouse wheel works a lot better for me. Cheers. Shyam Habarakada Oct
17 '14 at 0:58
+1. I don't understand it down vote. There is a disclaimer. Take it if you like it and skip it if you don't.
fangmobile.com Oct 12 '15 at 20:32
This is a very helpful answer Slava Nov 9 '15 at 8:57
As someone who has recently moved to VIM, I find the premise of this answer to be absolutely spot-on.The
ability to instantly tell the number of repetitions necessary to position the cursor in a given spot might or
might not be something that comes with experience. Right now, much as @ShyamHabarakada, my brain
just doesn't work like that. Regardless of how unorthodox this might be, I personally re-mapped fn+hjkl to
the arrow keys for very small movements. For big movements, I usually approximate a number of repetitions
and then fine-tune with those. For me, that's as quick as I can get. Jacoscaz Nov 12 '15 at 11:11
In GVim, you can use the mouse. But honestly, what's wrong with using the arrow keys?
There's a reason why they are on a keyboard.
answered Nov 15 '09 at 11:15
ammoQ
22.9k
46
76
but that reason is not catered to vim users. One of the central strengths of vim is that you can do all kinds of
things without time-costly taking your hands off the keyboard home row. In particular, the hjkl movements.
accolade Jun 13 '11 at 23:27
I agree with @ammoQ. Nothing wrong with using the arrow keys. If using arrow keys give you more
productivity, go with it. Eunwoo Song Feb 20 '13 at 18:36
accolade: I use hjkl more often than the arrow keys when using vi, but switching between modes has
become a subconscious action for me. ammoQ Feb 21 '13 at 20:20
To have a little better navigation in insert mode, why not map some keys?
imap <C-b>
imap <C-f>
imap <C-e>
imap <C-a>
" <C-a> is
<Left>
<Right>
<End>
<Home>
used to repeat last entered text. Override it, if its not needed
If you can work around making the Meta key work in your terminal, you can mock emacs mode
even better. The navigation in normal-mode is way better, but for shorter movements it helps
to stay in insert mode.
For longer jumps, I prefer the following default translation:
<Meta-b>
maps to
<Esc><C-left>
Alex
736
15
I believe Home and End (and PageUp / PageDn ) also work normally while in insert mode,
but aside from that, I don't believe there are any other standard keys defined for text traversal.
answered Nov 15 '09 at 10:23
Amber
225k
33
378
393
but possibly isn't there any way of traversing the text using only the letter keys ? Andreas Grech Nov 15
'09 at 10:25
5
Certainly there is, if you don't want to be able to type certain letters... ;) hobbs Nov 15 '09 at 10:33
http://stackoverflow.com/questions/1737163/traversing-text-in-insert-mode
09/04/2016, 4:21 PM
Page 6 of 8
You can create mappings that work in insert mode. The way to do that is via inoremap. Note
the 'i' at the beginning of the command (noremap is useful to avoid key map collisions). The
corollary is 'n' for 'normal' mode. You can surmise what vim thinks is 'normal' ;)
HOWEVER, you really want to navigate around in text using 'normal' mode. Vim is super at
this kind of thing and all that power is available from normal mode. Vim already provides easy
ways to get from normal mode to insert mode (e.g., i, I, a, A, o, O). The trick is to make it easy
to get into normal mode. The way to do that is to remap escape to a more convient key. But
you need one that won't conflict with your regular typing. I use:
inoremap jj <Esc>
Since jj (that's 2 j's typed one after the other quickly) doesn't seem to appear in my vocabulary.
Other's will remap to where it's comfortable.
The other essential change I make is to switch the CAPSLOCK and CONTROL keys on my
keyboard (using the host computer's keyboard configuration) since I almost never use
CAPSLOCK and it has that big, beautiful button right where I want it. (This is common for
Emacs users. The downside is when you find yourself on an 'unfixed' keyboard! Aaarggh!)
Once you remap CAPSLOCK, you can comfortably use the following insert mode remappings:
Keeping in mind that some keys are already mapped in insert mode (backwards-kill-word is Cw (Control-w) by default, you might already have the bindings you want. That said, I prefer C-h
so in my .vimrc I have:
inoremap <C-h> <C-w>
BUT, you probably want the same muscle memory spasm in normal mode, so I also map C-h
as:
nnoremap <C-h> db
(d)elete (b)ackwards accomplishes the same thing with the same key chord. This kind of quick
edit is one that I find useful in practice for typos. But stick to normal mode for moving around in
text and anything more than killing the previous word. Once you get into the habit of changing
modes (using a remap of course), it will be much more efficient than remapping insert mode.
answered Jan 23 '13 at 20:28
pfries
895
13
nice remap for escape - I reckon itll work for just about anything except transliterated arabic! jjt Dec 18 '15
at 22:23
Johan
7,697
18
69
95
vim lets you map any key to pretty much anything you want. Among the many capabilities is
also the ability to switch in and out of command mode, or to move the cursor in insert mode...
so if you're not restricted to a default-configured vim, anything can be done.
No, I'm not good enough to give an example. If I had to, I'd pick up the manual and figure it
out.
answered Nov 15 '09 at 10:29
Carl Smotricz
43.6k
11
80
135
http://stackoverflow.com/questions/1737163/traversing-text-in-insert-mode
09/04/2016, 4:21 PM
Page 7 of 8
imap
to map any key in insert mode to one of the cursor keys. Like so:
imap h <Left>
Now h works like in normal mode, moving the cursor. (Mapping h in this way is obviously a bad
choice)
Having said that I do not think the standard way of moving around in text using VIM is "not
productive". There are lots of very powerful ways of traversing the text in normal mode (like
using w and b, or / and ?, or f and F, etc.)
answered Nov 15 '09 at 10:40
heijp06
6,920
18
38
I didn't say the the standard way of moving through text in vim is not productive; what i said was that it was a
bit "annoying" having to go out of insert mode to go back a few characters and then having to back to insert
again; but then again, maybe this is because I haven't got quite used to vim yet :) Andreas Grech Nov 15
'09 at 10:47
http://stackoverflow.com/questions/1737163/traversing-text-in-insert-mode
09/04/2016, 4:21 PM
Page 8 of 8