4

So I know that an underscore (_) is considered part of a word in vim. Working with Python, I normally end up having a lot of underscores. Is there a way to select part of a word excluding the underscore? For example in long_name, I'd just love to quickly select long.

3 Answers 3

7

If you want, you can use

:set iskeyword-=_

...which will mean that underscores are no longer counted as parts of a word (this doesn't affect WORDs). You can reverse this with:

:set iskeyword+=_

These can easily be set to some keybinding:

:nnoremap <f2> :set iskeyword-=_
:nnoremap <s-f2> :set iskeyword+=_

Someone with a bit with a bit more vimscripting skill than I could probably work out a way to have a toggle button, rather than separate on and off keys.

1
  • Also, :set iskeyword& can reset it to its default according to vim doc.
    – Chase T.
    Jun 14, 2018 at 20:01
4

Have a look at vim-textobj-underscore plugin.

Excerpt from the README:

The textobj-underscore plugin provides two new text-objects which are triggered by a_ and i_ respectively. You can use them when you have to deal with the following type of words:

foo_bar_baz

Now, suppose you have to change bar to qux (* for cursor position). You can do the following:

foo_b*ar_bar

and type ci_ to get

foo_*_bar

Or you can type da_ to get foobar

1

You can use my camelcasemotion plugin; it provides motions to jump to each fragment, and text objects to select / work on such part (both including and excluding the underscore).

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .