0

I’m having trouble with hjkl movement in insert mode.

The issue is that I have changed hjkl movement to move by display line. I have 'wrap' on. The cursor should normally move line by line, not move display line by display line.

With my vimrc, when Ctrl is pressed along with up or down movement, it should move through display line by display line, but somehow it kept inserting a space and moving through display line (especially up movement).

Yesterday, I tried with only the Ctrl + movement key in insert mode, and it worked fine, but it failed to work properly when other scripts are included. I can't figure out how to solve it.

:set wrap linebreak nolist
:set showbreak=…

vmap j gj
vmap k gk
nmap j gj
nmap k gk

inoremap <C-h> <Left>
inoremap <C-j> <C-o>gj
inoremap <C-k> <C-o>gk 
inoremap <C-l> <Right>
cnoremap <C-h> <Left>
cnoremap <C-j> <Down>
cnoremap <C-k> <Up>
cnoremap <C-l> <Right>
7
  • Welcome to Vi and Vim! Sorry but it's not completely clear what the problem is. Is it when you press <C-j> in Insert mode, to trigger the <C-o>gj mapping? You said it's not working, but what is happening in that case? Is it just not moving at all, or moving to the next real line instead of the next virtual line? Can you reproduce the issue by typing <C-o>gj directly? Can you try to reproduce it with a minimal vimrc with only 4 or 5 lines needed to set that up? If you inspect the mappings with :imap, do you see that <C-j> mapping correctly displayed there? – filbranden Aug 9 '20 at 22:52
  • 2
    Please edit the question to clarify and post the answers to my follow up questions. – filbranden Aug 9 '20 at 22:53
  • "it kept inserting "SPACE" and move thru DISPLAY LINE" Is it possible that you have any trailing spaces in your mapping definitions? Take a look at your vimrc with :set list enabled to see if you have any extra unwanted spaces there? – filbranden Aug 12 '20 at 3:28
  • "or do you have any simple good script to enable vim movement in insert mode ?" Frankly, my advice is that you avoid using motion commands while in Insert mode. Insert mode is meant for inserting text. There's a reason why Vim is a modal editor. Get used to being in Normal mode all of the time, and only entering Insert mode when you're actually inserting text. – filbranden Aug 13 '20 at 13:50
  • 1
    "are you switching from insert mode to normal that often?" Actually, I'm normally in Normal mode and I switch to Insert mode when I need to Insert something. I rarely, almost never use motions in Insert mode, when I do, it's arrow keys or Home/End/PgUp/PgDn. (I really only do that when playing vimgolf, to shave off a few keystrokes in cases when it helps.) Normal mode is much more efficient for motion, that's the point of a modal interface. You might like to read this question and all answers in it! – filbranden Aug 15 '20 at 18:32
2

The problem is that you have a trailing space in your inoremap line in your vimrc.

That trailing space becomes part of the mapping replacement, so every time you type <C-h>, not only will Vim execute the <C-o>gk part, which will move up one line, it will also produce a <Space> in Insert mode.

I can actually even see it in your paste in your question...

Screenshot with selection.

If you enable :set list while looking at your vimrc, this is what you'll see:

inoremap <C-h> <Left>$
inoremap <C-j> <C-o>gj$
inoremap <C-k> <C-o>gk $
inoremap <C-l> <Right>$

See the trailing space there?

Also if you use :imap or :imap <C-k> to inspect the definition of the mapping, the trailing space will be displayed as <Space>, which will also make it obvious:

:imap <C-h>
i  <C-K>         <C-O>gk<Space>

Simply remove the trailing space from your mapping definition and it will work as expected.

2
  • 3
    to add to this answer, I would very much advise against mapping <C-h>, <C-J>, <C-K> and <C-L>. It might work for you in the graphical version (if you do not use e.g. digraphs) but e.g. <c-h> will overwrite your backspace key in the terminal, I am actually surprised, this works in gvim properly. Same for the other control keys. – Christian Brabandt Aug 20 '20 at 11:06
  • 2
    @ChristianBrabandt Also... If you want to write mappings to keep Vim in insert mode and use Ctrl+keys to move around, basically denying Vim's "modal editor" nature... Maybe what you wanted was Emacs all along, right? I tried to make this point in the (so very long!) comment thread on the question, but OP doesn't seem to believe Vimmers actually go back to normal mode to move around, that that's actually more efficient most of the time... Oh well. – filbranden Aug 20 '20 at 11:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.