Quantcast
Channel: Traversing text in Insert mode - Stack Overflow
Viewing all articles
Browse latest Browse all 14

Answer by user12711 for Traversing text in Insert mode

0
0

Yes, there is a way. You can use gj and gk inside Insert Mode to move up (backwards) and down (forwards) within a single logical-line that wraps around numerous visual-lines. That is handy if you have very long lines and/or a narrow width terminal and can be much faster than the arrow keys, or hitting ESC to use the other navigation shortcuts. Also you can use CTRL-O to issue one normal-mode command. Neither of these navigation movements will escape you out of insert mode.

It can be disruptive to productivity to hit ESC (which was besides home-row on terminal keyboards when Vi was invented). I work with text files that have extremely long logical-lines, whole paragraphs of sentences full of plain text. These two options are a real time saver for me. It's still handy to escape out to normal-mode if I need to hunt for a particular word within the paragraph, but many times I just need to move a visual-line up or down, and then continue inserting.

I actually edit my .vimrc to map out my nearly useless ALT key, so my left hand presses ALT while my right uses the arrow keys. (and it does not escape out of insert-mode: notice the "i" at the end of each entry).

imap <A-UP> <ESC>gkiimap <A-DOWN> <ESC>gji

I also add lines to .vimrc so that my arrow keys work in normal-mode to traverse up and down within many visual-lines that are contained by one logical-line (the code is identical to the above except you use map or nmap rather than imap) And I use keyboard's other navigation keys with the ALT-KEY, all within insert-mode.

I add additional navigation lines for my ALT key, to use with my keyboards preassigned navigation keys, to navigate more quickly within insert-mode. None of these will escape you out of insert-mode:

" <ALT> navigation within insert-mode imap <A-UP> <ESC>gki imap <A-DOWN> <ESC>gjiimap <A-LEFT> <ESC>biimap <A-RIGHT> <ESC>eaimap <A-END> <ESC>Aimap <A-PageUp> <ESC>(iimap <A-PageDown> <ESC>l)iimap <A-Home> <Esc>I" so that <ALT> behaves the same in normal modenmap <A-UP> gknmap <A-DOWN> gjnmap <A-LEFT> bnmap <A-RIGHT> le  nmap <A-END> $nmap <A-PageUp> (nmap <A-PageDown> )nmap <A-Home> ^

My "within" insert-mode navigation short cuts utilize the ALT key with PgUp, PgDn, Home & End keys too, to jump around far more quickly. I use alt-pgup and alt-pgdn presses to jump forward or backward by sentences. And, I use alt-home and alt-end to jump to the beginning and end of my paragraph long logical lines. None of this escapes me out of insert mode. (I figured that I might as well program all the preassigned navigation keys to work quickly within insert-mode, without leaving it). And if I want to move one character at a time, I just let up on the ALT key while still using the arrow keys.

All of those are really simple modifications to the .vimrc. You can get creative and do all sorts of things to improve navigation within insert-mode. Otherwise, if you use the plain ole ARROW keys within insert-mode on long lines, then you need a cup of coffee in your left hand to sip while you hold the arrow keys down with the right. That takes forever!

By the way, the key already works with all the other normal mode navigation commands, like h,j,k,l and w,W,b,B,e and search: /,% etc. EXCEPT that it automatically escapes you out of insert mode without having to press the ESC key. So using with the normal-mode navigation characters will leave you stranded in normal-mode, until you take the effort to press i,I,a,A,o,O again, which I find easy to forget to do.

Since the keyboards preassigned navigation commands: arrows: UP, DOWN, RIGHT, LEFT and PGUP, PGDN, HOME, END don't do anything special with the key, I found that I might as well map them out in .vimrc to help my navigation within insert-mode, while staying within insert-mode. And, to reiterate, its helpful to me because I work with very long text documents that have paragraph long logical-lines that span across a lot of visual-lines, and the arrow keys and h,j,k,l don't acknowledge the visual-lines. This fixes that particular problem, both within insert-mode and normal-mode.

I give credit to my knowledge above, which I found in the book HACKING VIM 7.2 by Kim Schulz


Viewing all articles
Browse latest Browse all 14

Latest Images

Trending Articles





Latest Images