Move to end of the current word in Vim

Vim

Vim Problem Overview


Usually I use ea to append something to a word; however, this doesn't work if the cursor is already in the last position of the word.

ea will cause the cursor to move to the end of the next word.

I'm interested to know whether there is any hotkey for moving to the end of current word even if the cursor is already in the last position of the word.

Thanks.

Vim Solutions


Solution 1 - Vim

  • w moves you to the beginning of the next word
  • e moves you to the last letter of the word
  • i lets you start editing right where the cursor is
  • a lets you start editing one character ahead

In your situation, it seems like you want a consistent way to append to a word, even if you're on the last letter. I'd switch to using wi instead of ea

I advise against remapping the basic vim movement controls, they are the way they are for a reason.

Solution 2 - Vim

You can change the functionality of e if you desire. For example, you could put

nnoremap e he

in your ~/.vimrc. This maps e to he so that it works the way you want. However, you may find e useful and not want to make over it completely. You could use your <leader> key to add an alternate mapping.

nnoremap <leader>e he

So that you can use \e or ,e (depending on your mapleader setting) to achieve the desired effect. I tend to agree with the others that it's better to become used to vim's standard single keys though. In general, use maps for longer commands that you regularly use.

There's no builtin command like the one you're suggesting, but there is regex \> for the end of a word.

Solution 3 - Vim

  • Move to the end of the word: e
  • Move to the beginning of the word: b

Solution 4 - Vim

Try "bea". This will work regardless of where the cursor is within the word.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCykerView Question on Stackoverflow
Solution 1 - VimStephanView Answer on Stackoverflow
Solution 2 - VimConnerView Answer on Stackoverflow
Solution 3 - VimlogbasexView Answer on Stackoverflow
Solution 4 - Vimuser15464894View Answer on Stackoverflow