Select entire line in VIM, without the new line character

No, there is nothing built-in that does the job. That's why people have even created plugins to address the need.

Probably the most popular choice is textobj-line. With textobj-line you get two new text objects, al "a line" and il "inner line". Then,

  • vil selects the printable contents of the line (like ^vg_),
  • val selects the entire line contents (like 0v$h).

Both do not include the newline in the selection.

Pretty handy plugin if you ask me. And it works with operators, too.


By request, the installation:

  • With plain Vim:
    1. Get the latest textobj-user and extract its directories into ~/.vim.
    2. Get the latest textobj-line and extract its directories into ~/.vim.
    3. Generate the help tags :helptags ~/.vim/doc.
  • With a plugin manager (recommended): just follow the usual installation procedure for your plugin manager, and don't forget to install the textobj-user dependency as well.

Yes, g_ is what you are looking for. g_ is like $, but without the newline character at the end.

Use 0vg_ or ^vg_, depending if you want to copy from the beginning of the line, or the first character on the line, respectively.


0v$
^v$
0vg_
^vg_
$v0
$v^
g_v0
g_v^

all do the job with different conceptions of what a line is (from first column or from first printable character, to last character or to last printable character). You can create a custom mapping if you like.

Note that selecting text is often unnecessary in vim.

Tags:

Vim