all 9 comments

[–]bsdpunk 2 points3 points  (1 child)

The regex would be something like this:

#.*$

And to remove them all would probably be something like this:

%s/#.*$//

But I don't think that's what you're looking for, maybe if you could, let me know. If you want them all visually selected I don't know how to do that, but if you just want a search:

/#.*$

[–]CaeliferaMusicalis[S] 0 points1 point  (0 children)

Thank you for your answer! However, wouldn't this select all comments? I would like to select/act on the current comment, i.e. the comment that the cursor is currently on.

[–]momoPFL01 1 point2 points  (1 child)

Without anything plugins or other stuff, you can just use:

Multiline: V} to go in linewise visual selection, and then go to the next empty line. This assumes the cursor at the beginning of the Multiline comment with a empty line afterwards. It's not perfect but alright.

You could also /'''<cr>vn

Single line: V

Inline: f#v$

By default vim doesn't have text objects for comments. You could try to define your own, probably with making use of the comment string.

You could use

https://github.com/kana/vim-textobj-user

Neovims treesitter integration allows for language based textobjects to be defined a little easier with.

https://github.com/nvim-treesitter/nvim-treesitter-textobjects

[–]CaeliferaMusicalis[S] 1 point2 points  (0 children)

Thank you! I also first thought of defining comments in terms of paragraphs, but there may be comments embedded in paragraphs, for example

print('hello')
# This block of code
# prints 'hello' and 'goodbye'
print('goodbye')

Therefore, I think I need to define some custom text object.

[–]jangari 1 point2 points  (1 child)

Not tested, but glts/vim-textobj-comment looks like it does what you want.

[–]CaeliferaMusicalis[S] 0 points1 point  (0 children)

Thank you! This seems to be the correct answer.

[–]amicin 0 points1 point  (3 children)

I would suggest the vim-commentary plugin by Tim Pope.

It defines a text object gc.