Is there a way to get v$y to not copy the newline character in Vim on Mac and Linux?
I've used the Windows version of Vim in the past, but am new to MacVim. In MacVim, when I switch to visual mode, use the $ motion (highlighting from my cursor to the end of the line), and yank, when I paste the copied content, it includes the carriage return, bumping everything after the paste point down to a new line.
This behavior seemed unfamiliar (not to mention annoying) to me. Is there any way to change this behavior to match the Wind开发者_高级运维ows version, where the newline is not included in the yank?
Is just copying the text until the end of the line inappropriate? y$
will just copy from your current cursor until the end of the line, without the newline.
There's a little-known motion that serves this need regardless of configuring Windows behaviors and can generally be useful in other contexts: g_
. Quoth :help g_
:
g_ To the last non-blank character of the line and
[count - 1] lines downward |inclusive|. {not in Vi}
Personally I don't tend to use this for yanking because I avoid the extra visual mode keystroke and use y$
(which doesn't copy the newline, as @zigdon suggested). Or better yet, nnoremap Y y$
so that Y
works consistently with C
and D
.
I do however often use g_
with surround.vim where the mappings to add surrounds are often harder to remember for me than using visual selection. If you want to select until the end of the line and surround with parens, for instance, it would be ys$)
, which isn't bad but I tend to forget the ys
mnemonic. v$S)
seems natural but has the same problem as your question: it includes the newline, and that's a total mess when adding surrounds. vg_S)
is exactly what you usually want.
One nice thing about doing it visually is that you can correct mid-selection: I still tend to hit v$
by muscle memory a lot, but if you see that you've overshot before acting, you can still hit g_
and fix the selection.
You may try Du. Effectively it does exactly what you want and it is more finger-friendly if you intend to use it in raw editing.
I discovered that the option that was causing the behavior I'm used to seeing is behave mswin
, which I believe is on by default in GVim for Windows.
精彩评论