VIM Select Entire Line

Vim

Vim Problem Overview


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.

Vim Solutions


Solution 1 - Vim

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

Solution 2 - Vim

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.

Solution 3 - Vim

While this might be more keystrokes.

If you are already in visual mode you can use o to go to the other end of the visual selection.

So you can type

v0o$

To select the whole line. Take a look at :h visual-change


However from the comments it seems you just want to copy the whole line.

Which would just be yy

Solution 4 - Vim

Just change your order of operations. You almost have it.

^,v,$

Or as suggested by @Kent: because ^ goes to the first non-empty char, if the line has leading spaces:

0,v,$

Solution 5 - Vim

I know this thread is super old, but I just had the same question. This thread came up first, but I found a different answer than any found here. Use 'V' to select whole lines. That easy. One character to select the whole current line.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionappleLoverView Question on Stackoverflow
Solution 1 - Vimuser229044View Answer on Stackoverflow
Solution 2 - VimJason Jun GeView Answer on Stackoverflow
Solution 3 - VimFDinoffView Answer on Stackoverflow
Solution 4 - VimsamsquanchView Answer on Stackoverflow
Solution 5 - VimdowntimeView Answer on Stackoverflow