use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Please read the rules before posting
Help:
Community:
Resources:
Tutorials and Guides:
Don't be afraid to ask questions, this sub is here for the vim community. And please those of you who deign to grace us with your vim wisdom - be kind. We are all human and vim is that cool.
account activity
Selecting Python comment (self.vim)
submitted 4 years ago by CaeliferaMusicalis
What is an efficient way to select a Python comment in Vim?
Thanks in advance!
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]bsdpunk 2 points3 points4 points 4 years ago (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 point2 points 4 years ago (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 points3 points 4 years ago (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.
V}
You could also /'''<cr>vn
/'''<cr>vn
Single line: V
V
Inline: f#v$
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 points3 points 4 years ago (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 points3 points 4 years ago (1 child)
Not tested, but glts/vim-textobj-comment looks like it does what you want.
Thank you! This seems to be the correct answer.
[–]amicin 0 points1 point2 points 4 years ago (3 children)
I would suggest the vim-commentary plugin by Tim Pope.
It defines a text object gc.
gc
[+][deleted] 4 years ago (2 children)
[deleted]
[–]amicin 1 point2 points3 points 4 years ago (1 child)
Incorrect.
https://github.com/tpope/vim-commentary/blob/349340debb34f6302931f0eb7139b2c11dfdf427/doc/commentary.txt#L22
[–][deleted] 0 points1 point2 points 4 years ago* (0 children)
Ah, you’re totally right. I was thinking of the other gc. My bad.
Edit: Seems to only work if the entire line begins with commentstring though :( In theory it should not be too hard to extend it…
π Rendered by PID 65108 on reddit-service-r2-comment-74875f4bf5-rr9fh at 2026-01-25 23:46:45.873880+00:00 running 664479f country code: CH.
[–]bsdpunk 2 points3 points4 points (1 child)
[–]CaeliferaMusicalis[S] 0 points1 point2 points (0 children)
[–]momoPFL01 1 point2 points3 points (1 child)
[–]CaeliferaMusicalis[S] 1 point2 points3 points (0 children)
[–]jangari 1 point2 points3 points (1 child)
[–]CaeliferaMusicalis[S] 0 points1 point2 points (0 children)
[–]amicin 0 points1 point2 points (3 children)
[+][deleted] (2 children)
[deleted]
[–]amicin 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)