8

Is there a shortcut in vim for :20 followed by i, i.e. one command for inserting at a line number?

2
  • This is about how to do programming with a particular tool; it is within scope for SO. Jun 27, 2011 at 4:37
  • why would you hard code a particular line number in a command?
    – user76528
    Jun 27, 2011 at 7:22

4 Answers 4

2

You can use nmap vim command like this:

:nmap ~! 20Gi

And then every time you will press keystrokes ~! (or you can choose any other key combination) it will move cursor on line # 20 in insert mode.

1
  • Note that if option startofline is unset, G preserves the cursor column, therefore, i could start Insert mode not necessarily on the first non-blank character of the line. In this particular case it's better to use I or gI command (depending on whether you want to insert a line number before of after possible leading whitespace).
    – ib
    Jun 27, 2011 at 6:10
1

You can type :20i<enter>Text you want to insert including newlines...<esc>

2
  • how is this different than what she is trying not to do? I thought he was looking for an easier way of inserting text at line N without :Ni, in this case line 20. Did I misinterpret the question?
    – matchew
    Jun 27, 2011 at 4:33
  • This is the same number of characters typed as if you invert the order of the 'enter' and 'i' as in the question. Jun 27, 2011 at 4:37
1

another way to do what you ask is 20Gi in normal mode. it may not be faster, but it may be easier to remember. if you want to do it without the shift try 20ggi

thinking about it more, and this wont work in obvious cases, but I like to do the following

:set mouse=a
:nmap <2-LeftMouse> i

which, in normal mode (note nmap), will bring me to that line and when I double click it will change to insert on the specified line. The usefulness could be trivial, but if you are used to using the mouse this map help.

http://www.viemu.com/vi-vim-cheat-sheet.gif

viemu.comhas a half decent cheatsheet, if you like

1
  • This is actually one character shorter than the original; well done. Jun 27, 2011 at 4:38
0

Normal mode i is available in command mode as startinsert, but that doesn’t accept a line number. So you need two commands, and a bar to separate them, which gives you :20 | startinsert.

3
  • That doesn't look shorter than the original. Jun 27, 2011 at 4:36
  • No, it’s just the only approach that works in vimscript, if that’s what the OP is trying to do. Jun 27, 2011 at 6:17
  • In that case, you need to expand your answer to explain what you mean, because it is not entirely clear to me what you do mean; I've not encountered vimscript before, but I only use vim as a souped-up vi, not making full use of the incredible complexities it has added. Jun 27, 2011 at 6:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .