5

I am relatively new to using vim. I have to admit it is a powerful tool, however it can be very frustrating if one doesn't learn how to use it.

Whatever, there is one or two things that annoy me terribly:

  1. How can I get to the end of the previous line/start of the next one?

    Assume I have this text file:

    abcdef
    | <- cursor, not character
    ghijkl
    

    In all the text editor I have seen, pressing left-arrow would bring me to the f letter, like this:

    abcdef| <- cursor, not character
    
    ghijkl
    

    Idem est, pressing right-arrow would bring me to the beginning of the next line (before the g character).

    However, vim doesn't do that, the cursor is stuck if I don't press up/down-arrow. Can I change that? It is very annoying.

  2. How can I go directly to the end of the last line by pressing down-arrow?

    Assume I have this text file

     a
     b
    |c   <- '|' is the cursor
     def
    

    By pressing down-arrow, I will go before the letter d, okay, but if I click again, nothing will happen. However, I would like the cursor to go to the end of that line (since it is the last one of the text file). How? The only solution I found was to put several new empty line...

  3. Why does the cursor can't go to the last character in command mode? It keeps being stuck like this: abcde|f, without being able to go after the f.

  • There is a vim option that allows the h and l keys to wrap to the previous/next line when at the beginning/end of a line. I'll post it as an answer when I have time. – EvergreenTree Oct 20 '15 at 12:19
8

You can use the whichwrap setting to change this behaviour. Try :set whichwrap+=<,>,h,l to allow the left and right arrow keys and h and l to wrap. See :help whichwrap for more options.

5

All of these are normal mode commands and leave you in normal mode:

  • $ will move the cursor to the end of the line.
  • 0 will move the cursor to the beginning of the line.
  • ^ will move the cursor to the first non-blank character in the line.

  1. Why does the cursor can't go to the last character in command mode? It keeps being stuck like this: abcde|f, without being able to go after the f.

Vim is on the last character in the line (newlines don't count), so it makes sense that the cursor can't move after it, because there's nothing there. To help visualize it, you may want to set the cursor to a block cursor or horizontal line, rather than the misleading vertical line.

vertical cursor
block cursor
horizontal cursor

In all three of those images, the cursor was in the same location, simply the shape was changed.

In insert mode, the cursor can move beyond the last character in a line, because it indicates where the new character will be placed.

See :he 'guicursor' for information on how to change the cursor shape. Here's a simple setup that sets the cursor to a block in all modes except insert mode, in which it's a 10%-wide vertical line:

:set guicursor=a:block-Cursor,i:ver10-Cursor

This is tested in GVim, console based version may need additional configuration.

3

For 1: Use Space and Backspace to do what you are used to arrow keys doing. h and l will never move you off the current line, but Space and Backspace will. (This is default in vim but not in vi; it's adjustable with the whichwrap option as others have mentioned.)

Also, + (or Return) and - will take you to the non-whitespace start of the next line or previous line, respectively. Can be very handy in tab-structured documents.

For 2: Use G$ to go to the end of the last line. It's two keystrokes, yes, but does exactly what you want.

Also, you may want to learn about gi, which begins inserting text at the same place you were inserting text last time you left insert mode. This would probably handle some use cases for going to the end of the document. (Not all of them. If you've done some other insertion edits such that gi wouldn't go to the end of the document, and now want to start adding text at the end of the last line, just use GA.)

For 3: The only reason I can think of why you would want this behavior, is if you always use i to insert. Use a and A to append text. A will enter insert mode and append text to the end of the current line. a will append text after the current character position. There, now you don't need to move your cursor off of the text of the line. :)

1

Simple solution (by simple I mean no fancy mappings or similar).

For 1 are the combinations:

kA: k go one line up, A append text to the end of the line (your "left-arrow")

and

jI: j go one line down, I insert text before the first non-blank in the line.

These commands are even faster typed then moving to the arrow keys. Generally speaking I think Vim is designed to never have to use arrow keys.

For 2 jI does also do what you want.

For 3 this is the expected behaviour for the cursor. It would be very irritating if j would jump to the end of the line. Or am I understanding you wrong?

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.