Skip to main content
  1. blog/

How to make edits on multiple lines in Vim

·198 words·1 min
TIL Vim
Dustin Wheeler
Author
Dustin Wheeler
Chemist, polymathist, usually lost

While my preferred (current) editor is Nova, occasionally I have reason to edit in Vim, particularly when I’m working on files on a remote server. Recently, I found myself wanting to comment multiple lines in a Python file. On my personal machines, I’ve installed [NerdCommenter][nerdcomment], but that isn’t always available.1

For those occasions, basic Vim line editing skills come in to play. First, you can toggle “Visual Line mode” by hitting Shift + V, then highlighting the relevant lines. From there, doing a simple replacement (:'<'>s/^/#/,2 or whatever your comment replacement should be) comments out the selected lines.

Alternately, you can do a block selection (enter that mode with Ctrl+V), select the start of each line, then press Shift+I. Type out the pattern (it will only show up on a single line for the moment), press Esc, then press Enter and the changes will be applied to the whole block selection.

These methods are outlined in a post on ostechnix.com in methods 1 and 4.


  1. Using NerdCommenter, selected (or current) line can be commented/uncommented with the toggle <Leader>c<space> (or commented with <Leader>cc and uncommented with <Leader>cu) [nerdcomment]: https://github.com/preservim/nerdcommenter ↩︎

  2. The '<'> gets inserted as soon as you type :↩︎