选择整条生产线

在 VIM 中,当光标沿着该行的某个随机点时,如何选择单行?

我知道你可以通过(v,$)到达行尾,或者(v,^)到达行首,但是当你这样做时(v,$,^)它逻辑上不会选择整行,它从游标中选择,直到结束,然后切换到游标直到开始... ... 所以这种方法当然失败了。

73022 次浏览

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

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,$

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

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.

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.