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
You can still a mapping to what you want, e.g.:
nnoremap <leader>v 0v$
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.
If you want to copy line into the buffer, you can 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. So the command is:
Du
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.
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:
~/.vim
.~/.vim
.:helptags ~/.vim/doc
.