Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

share|improve this question

migrated from stackoverflow.com Jul 14 '11 at 1:58

This question came from our site for professional and enthusiast programmers.

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

4 Answers 4

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.

share|improve this answer
    
+1 nice answer. –  matchew Jun 27 '11 at 4:37
    
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 '11 at 6:10

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

share|improve this answer
    
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 '11 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. –  Jonathan Leffler Jun 27 '11 at 4:37

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

share|improve this answer
    
This is actually one character shorter than the original; well done. –  Jonathan Leffler Jun 27 '11 at 4:38

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.

share|improve this answer
    
That doesn't look shorter than the original. –  Jonathan Leffler Jun 27 '11 at 4:36
    
No, it’s just the only approach that works in vimscript, if that’s what the OP is trying to do. –  Aristotle Pagaltzis Jun 27 '11 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. –  Jonathan Leffler Jun 27 '11 at 6:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.