2

Why I ask:

I use to enter code for example if(condition){}, in following step:

  1. if(){ }
  2. move cursor back into () to complete condition
  3. move cursor into {} to add task

I have read Traversing text in Insert mode, and I add follow code into my $HOME/.vimrc

" key mapping
inoremap <A-h> <C-o>h
inoremap <A-j> <C-o>j
inoremap <A-k> <C-o>k
inoremap <A-l> <C-o>l

now I can use Alt+h and Alt+l, but the rest of two new map had no effect, then I test: Ctrl+oj and Ctrl+ok, both of them work.

  1. Is there any mistake when I do the key mapping?
  2. How to check if my new mapping is conflicted with other or not?

UPDATE: 2nd/Nov/2016


  1. I buy a new keyboard with cursor key...
  2. Install auto pair

However, I found one interesting thing, when I in Linux, there is ok for all above mapping just except Alt+h, because it conflicted with the ubuntu current open window help menu. I only meet my problem when I use ssh via MobaXerm application.

Community
  • 1
  • 1
How Chen
  • 1,340
  • 2
  • 17
  • 37
  • Why in all hell don't you simply use the cursor keys? – romainl Nov 01 '16 at 10:22
  • Are you using GVim or running command-line Vim from a terminal or terminal emulator? – Anthony Geoghegan Nov 01 '16 at 10:41
  • @romainl becase my keyboard has no cursor keys.... – How Chen Nov 01 '16 at 12:06
  • @AnthonyGeoghegan I use gvim via smartTTY – How Chen Nov 01 '16 at 12:07
  • 1
    A keyboard without cursor keys? Are we in the 70's? – romainl Nov 01 '16 at 14:42
  • @romainl, I just bought a new keyboard with cursor key... – How Chen Nov 02 '16 at 07:45
  • @romainl are you a vim user? if so- why would you even need the cursor keys? I always thought that vim is being chosen by people that do not want to move their hands all over their desk.. – lewiatan Nov 02 '16 at 08:08
  • @lewiatan, how you move your cursor under insert mode? – How Chen Nov 02 '16 at 08:33
  • I'm a Vim user, yes. And FWIW the top `vim` answerer on SO. Vim is chosen by people who value efficiency. Pressing `` is the most efficient way to move the cursor one character to the right in insert mode. – romainl Nov 02 '16 at 08:47
  • @romainl ok. no offence. But on the other hand I do not agree with that, because in my opinion moving your hand is not efficient (or maybe I should say ergonomic). I prefer using Ctrl + [ to leave the insert mode and move where I need to. – lewiatan Nov 02 '16 at 09:20
  • @HowChen I would: 1. Insert a template/frame of your condition. 2. Leave the insert mode with "Ctrl + [" 3. Move up and right with "k" and "w" 4. Start inserting condition with "a". 4. Repeat step 2 5. Start inserting below with "o". For more see my answer below. – lewiatan Nov 02 '16 at 09:23
  • Any way you look at it, `` is a lot more efficient than `la` or whatever insert mode mapping you could come up with. Sure you need to move your hand a bit but it's actually a good thing for your fingers/hands/forearms. – romainl Nov 02 '16 at 09:36

2 Answers2

4

I have read Traversing text in Insert mode, and I add follow code into my $HOME/.vimrc

You should carefully read the accepted answer for that answer, specially this part:

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.

The answer where you borrowed the mappings also mentions this:

Notwithstanding what Pavel Shved said - that it is probably more advisable to get used to Escaping Insert mode - here is an example set of mappings for quick navigation within Insert mode: (...)

Anyway, if you want to understand the problem with the Alt+j and Alt+k, you should first ensure that the mapping is still defined in Vim (they could have been erased or overwritten). You can use :imap to list them; try these:

:imap <A-j>
:imap <A-k>

If your mappings are correctly defined each one will list its target (e.g.: * <C-O>j). In this case you should check if Vim is receiving these combinations correctly; try inserting then in the text (insert mode) by using Ctrl+V (or Ctrl+Q if you mapped that to paste from clipboard) and the Alt combinations. You can get more details at the Vim FAQ "I am not able to create a mapping for the key. What is wrong?".

Edit:


If your issue is mainly related with closing parenthesis, then there are several other options, which I believe that are more practical. I quick internet search returned the following:

Community
  • 1
  • 1
mMontu
  • 8,983
  • 4
  • 38
  • 53
  • But when I program, if I want to enter if(condition), I always like to enter if() first and then back to condition – How Chen Nov 01 '16 at 12:28
  • @HowChen great that you posted an actual use case in this comment! I would recommend writing a macro, or googling a bit for "vim if parens" or something like that (doing that, I found https://github.com/jiangmiao/auto-pairs which may be what you want, or at least hopefully inspirational). – chelmertz Nov 02 '16 at 07:48
2

I also think that you misuse Vim.

I know that the question was about something else but here is my idea of how you should move around in vim.

You have 3 steps:
1. Insert some empty loop / condition
2. Insert a condition
3. Insert a body of the loop / condition

This should represent 3 changes, each separated by leaving the insert mode.

To do it properly you can perform step 1 and then leave insert mode by using either Esc or Ctrl+[ (with the second one- which is also vim default- you do not have to reach for escape key).

Then you should navigate to the place where you want to insert your change using h,j,k or l and follow it by starting insert mode.

There are several ways to start insert mode:

I - start insert mode at the beginning of the line (omitting whitespaces at the beginning)
i - start insert mode before the cursor
a - start insert mode after the cursor
A - start insert mode at the end of the line
s - change the sign under the cursor (can be combined with visual mode)
c - change text from under the cursor until place you have specified with the movement (e.g. ce - change until the end of the word, cl - the same as "s")
C - change everything from cursor until the end of the line
S - replace the whole line
o - start insert mode in the new line below
O - start insert mode in the new line above

lewiatan
  • 1,126
  • 2
  • 21
  • 37