VIM Select Entire Line

How do you select a single line in VIM, when your cursor as at some random point along that line?

I know you can do (v, $) to get to the end of the line, or (v, ^) to get to the start, but when you do (v,$,^) it logically doesn't select the whole line, it selects from cursor, until end, then switches it to cursor until beginning... So this approach fails of course.


Solution 1:

Capital V selects the current line in one key stroke; two, if you include the "shift" in shift+v.

Solution 2:

V would be direct answer. However, I rarely need to do this because "selecting the current line" is generally part of a larger task. Example of such tasks includes copying the line and deleting the line. There's generally a better way to accomplish the task as a whole. The following are some of the tasks I can think of:

  • copy the line: yy
  • delete the line: dd
  • indent the line: >> or <<
  • select the current paragraph: vap or vip
  • delete from the current line to the end of the file 0dG
  • highlight the current line to see where my cursor is: use :set cursorline in .vimrc file

One case in which I do use V is to select multiple lines that are not a paragraph or some other text object. In this case, there's a tip that might be useful for you: once in the selection mode, you can use o to jump the cursor between the start and the end of the selection.