Select entire line in VIM, without the new line character
Asked Answered
U

11

65

Which is the shortest way to select an entire line without the new line character in VIM?

I know that SHIFT + v selects the entire line, but with new line character.


To do this I go to the line and I press:

  • ^ (puts the cursor at the start of the line)
  • v (starts the visual select)
  • $ (selects the entire line including new line character)
  • Left (unselects the new line character)

I also know that I can create a recording that does such a thing. But I am asking if is there any built-in shortcuts...

Ulric answered 23/11, 2013 at 17:34 Comment(1)
Related question: vi.stackexchange.com/questions/12607/…Meloniemelony
P
32

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.
Pittman answered 23/11, 2013 at 17:48 Comment(3)
Can you include the installation steps?Ringnecked
The answer below g_ is pretty to the point and satisfying many casesLipoma
Jesus H this is a game changer....... this has bothered me for so long and I eventually just had moment of self clarity "why do i not just figure out how to fix this"Kilpatrick
P
125

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.

Perambulate answered 4/1, 2017 at 4:47 Comment(6)
man that underscore is pretty useful . Didn't know that >) thanks :DMccain
:help g_ describes g_ as "to the last non-blank character of the line and [count - 1] lines downward inclusive". Help also says 0 goes to beginning of the line while ^ goes to first non-blank character of the line. So, ^vg_ will select from first non-blank character to last non-blank character of the current line.Normanormal
In my opinion this should be the accepted answer because OP asked for "shortest way", and this uses almost the same number of key presses as the accepted answer, minus the need to install a plugin.Sacerdotal
I map it as noremap Y 0vg_y in my .vimrc so that now I just have to press shift+y to copy line without new lineEon
This is so good. Default keybindings eat plugins for breakfast.Leer
You can also shorten it to noremap Y 0yg_ and get the same resultAeromedical
P
32

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.
Pittman answered 23/11, 2013 at 17:48 Comment(3)
Can you include the installation steps?Ringnecked
The answer below g_ is pretty to the point and satisfying many casesLipoma
Jesus H this is a game changer....... this has bothered me for so long and I eventually just had moment of self clarity "why do i not just figure out how to fix this"Kilpatrick
D
13
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.

Dusty answered 23/11, 2013 at 19:23 Comment(2)
this is insanely post-hoc, but can you expand on what you mean by "selecting text is often unnecessary in vim"? Thanks for the answer btw.Assignor
@PeterDolan, Vim has operators and motions and text-objects that cover so many common use cases that selection is often unnecessary. For example, dip means "delete this paragraph" while vipd means "select this paragraph and delete it". The former is both shorter and more intuitive than the former. Familiarity with operators and text-objects is pretty much a requirement if one wants to become efficient with Vim.Dusty
C
8

Adding on to the answer by @glts, you can replicate the functionality of the textobj-line plugin using only vanilla vim mappings, no plugin installation required.

To do so, add the following to your .vimrc

vnoremap al :<C-U>normal 0v$h<CR>
omap al :normal val<CR>
vnoremap il :<C-U>normal ^vg_<CR>
omap il :normal vil<CR>

The al text object (short for 'a line') includes all characters in a line, but not the terminating newline. This includes all white space.

The il text object (short for 'inside line') goes from the first non-blank character to the last non-blank character.

Commands such as yil,val, and cil work as expected.

Cutlerr answered 5/5, 2020 at 22:35 Comment(0)
D
5

If you want to copy line into the buffer, you can use Du, which will delete from the cursor position to the end of line with D, and then revert changes with u. Text will be copied to the buffer without new line symbol.

Dogmatism answered 30/6, 2017 at 17:4 Comment(2)
Y performs copy with new line characterDogmatism
You might edit your answer to use Y for copying to the end of the line and D for deleting to the end of the line, and then there is no need to undo. (By the wayy$ and d$ have the same effect. More to type but it might be easier to remember.)Allier
R
1

Redefine $ for visual mode

The unwanted selection of the linefeed in visual mode can be permanently eliminated by adding the following visual mapping to .vimrc:

vnoremap $ g_

This replaces the standard behaviour of $ in visual mode for the move towards right before the linefeed g_.

Ruler answered 12/2, 2023 at 18:29 Comment(0)
H
1

You can use the selection option, it allows you to customize the behavior of line selection:

set selection=old

will not select the newline character.

Harryharsh answered 15/5, 2023 at 9:30 Comment(1)
In neovim <End> and "$" doesn't seem to be re-mappeable, so this would be the solution.Lister
P
0

You can still a mapping to what you want, e.g.:

nnoremap <leader>v 0v$
Palacios answered 23/11, 2013 at 18:53 Comment(1)
This still copies the newline, because $ includes the newline character at the end of the current line.Perambulate
T
0

Another solution $ will be working as you want it to

:vnoremap $ $h

maps your original $ command to new one

Trichocyst answered 9/6, 2021 at 15:27 Comment(0)
S
0

Not exactly an answer to your question, but I wonder if you can skip selecting the line and do directly what you want next:

  • If you want to change the line, just cc
  • If you want to yank the line, 0y$ (note $ here does not capture the line break because it does not move over it in normal mode, unlike in visual mode)
Scifi answered 5/6, 2022 at 18:44 Comment(1)
I thought the same thing. And if the goal is to delete the line and go back to normal mode immediately, then use 0d$. (Thanks for cc! New to me.)Allier
M
0

%v0

%v_

Go to end of the line and select to start of the line.

Mandie answered 19/3 at 12:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.