Search code examples
vimneovim

Ignore trailing spaces when yanking a word in neovim


So my issue when yanking a word (yw) is that it copies the trailing spaces as well:

Example: Doing a yw on this:

‖Hello World

Results in copying Hello

But the same applies to:

‖LongVariableName                        = 'SomeValue';

Results in copying LongVariableName

Is there a way to make as such I copy without copying any space? (which for me makes sense when copying words)


Solution

  • You can use yiw to yank the 'inner word' text object (yaw would include an additional space). See :help text-objects.

    A nice feature of text objects is that the cursor can be anywhere on the object (in this case the word) for it to work.

    There are many text objects in vim:

    • is means inner sentence
    • ip means inner paragraph
    • i) means inner parentheses

    And many more!

    Also the comment below by @DoktorOSwaldo points out that you can also use ye to yank from the current position until the end of the word without the space, e is the end of the current word, w is the start of the next word (this is not a text object, so repeating it on other words would require the cursor to be in the same position).