2

When I repeatedly inserting at the beginning of a line it is problematic because the cursor ends up in the middle of the line so I have to move back to the beginning of the line each time. So, for example, if I am commenting lines then might do something like i##_<ESC> to comment the line, then I have to do j^ to get back to the beginning of the next line before pressing dot to do a repeat.

Is there any way to simplify this? In particular I don't like having to type the hat to get to the beginnning of the line.

  • 2
    If you're just commenting out, what I usually do is: <c-v> at the start of the first line, select till the last line, then I## <esc>. – muru Mar 27 '16 at 20:41
  • If reaching for the ^ key is too far, 0 will also move to the beginning of the line. – alxndr Mar 27 '16 at 21:35
9

Just use uppercase I instead of lowercase i

The i and a Insert mode commands have uppercase versions I (insert at beginning of line) and A (append at end of line)

So, if you use I##<Esc>, you can use . to repeat on any other line, irrespective of where the cursor is

If you want to comment bunch of lines that are together, use visual mode as suggested by Amadan

  • I inserts before the first non-whitespace, not at the beginning of line. – Amadan Mar 28 '16 at 19:33
  • 1
    yup, that is true.. OP used ^, so I would be suitable.. – Sundeep Mar 29 '16 at 3:49
7

Just hit the enter key. It will take you to the start of the next line.

5
  • Use block visual mode: 0Ctrl+V, select the range, I (capital i), ##, Esc.

  • Shift+V, select extent, :s/^/## /g, CR

  • Use a commenting plugin, such as Commentary.

  • Make a macro: qq, insert one comment, CR to next line, q, then just @q to play it, and @@ to repeat it (or use count).

3

i inserts before the cursor. If the cursor is in column 1, then before the cursor also happens to be at beginning of line, but this is accidental. Repeating a change that begins with i will always insert before the cursor, but not always at the beginning of line. For repeated changes to insert at beginning of line you must make "beginning of line" part of the change that is repeated.

I inserts at the beginning of a line. The command I##_<Esc> inserts "##_" at beginning of line, not before the cursor. (If the cursor is in column 1, then beginning of line also happens to be before the cursor, but this is accidental.) This change can be repeated with . and will always insert at beginning of line.

  • I inserts before the first non-whitespace, not at the beginning of line. – Amadan Mar 28 '16 at 19:33

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.