You may want to use some plugin, for example: commentary by tim pope is a lightweight plugin with basic functionality: https://vimawesome.com/plugin/commentary-vim
"Comment stuff out." is the first line in the Readme.
With commentary-vim installed and running, navigate to the line you want to comment out (your cursor can be in the middle of the line), then hit gcl
to comment out that line and stay with the cursor at the exact position. In python a #
(hash and space) will be inserted, so the text will be shifted 2 positions to the right, but the cursor will stay exactly at the position as it was before. Use gcl
again to uncomment the line.
If you want your plugin to do more fancy stuff, take a look at the Nerd Commenter: https://vimawesome.com/plugin/the-nerd-commenter
Another lightweight plugin you may want to take a look at: https://github.com/tomtom/tcomment_vim
If you want to avoid installing plugins I'd suggest you write your custom key binding / shortcut.