Changing Search and Replace Newline Character

17 views
Skip to first unread message

Thomas Jakway

unread,
Nov 4, 2022, 6:32:50 AM11/4/22
to v...@vim.org

According to https://vim.fandom.com/wiki/Search_and_replace:


When searching, \n is newline, \r is CR (carriage return = Ctrl-M = ^M)… When replacing, \r is newline, \n is a null byte (0x00).”

There are also questions on StackOverflow about this, e.g. https://stackoverflow.com/q/71417.


Is there any way to change my settings to make this consistent? I’d like \n to mean “newline” in replace as well. I'm aware that this means \n can no longer be used to mean null when replacing. Ideally I’d be able to change that to some other control sequence, but not constantly tripping over this pothole is more important. If necessary I’ll insert nulls using sed or some other tool. I use search and replace all the time but hardly ever need to insert a null byte anywhere.

Christian Brabandt

unread,
Nov 4, 2022, 6:59:48 AM11/4/22
to v...@vim.org
No, this is not possible, except for creating a mapping that evaluates
your expression and replaces the result, see, :h c_CTRL-\_e


Best,
Christian
--
Letzte Worte von A. Senna:
"Irgendetwas klappert da."

Marek Stepanek

unread,
Nov 5, 2022, 12:37:12 PM11/5/22
to vim...@googlegroups.com
Hi Thomas,

I stumbled over this one too. Really annoying! I never understood the sense of it: \n means new line in search pattern and useless crap in the replace. Is there a reason behind, which I am not understanding as a middle advanced Vim-user?

Off-topic:
And while complaining about search and replace in Vim: I am still confused what to escape or not to escape (in Magic mode). With complicate patterns I fire up BBEdit (on macOS) or write a perl-filter. Much easier for me.

Best greetings to all


marek
> --
> --
> You received this message from the "vim_use" maillist.
> Do not top-post! Type your reply below the text you are replying to.
> For more information, visit http://www.vim.org/maillist.php
>
> ---
> You received this message because you are subscribed to the Google Groups "vim_use" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+u...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/20221104105931.GC1394649%40256bit.org.

Brian L. Matthews

unread,
Nov 6, 2022, 3:49:48 AM11/6/22
to v...@vim.org
I have to admit I've also found this inconsistency kind of baffling and
annoying. Although I don't replace something with a newline all that
often, so so far I've just lived with replacing whatever with nul,
undoing it, and editing the replace to do what I actually want. I have
also (mostly) trained myself to use ^V^M in the replacement instead,
which does what I want, although technically doesn't do what it says.
I've also never, in decades of programming and probably years spent in
vi then vim, wanted to replace anything with a nul.

I realize it's far, far too late to change this, so just venting a bit.
And it's the small imperfections that emphasize the perfection of the
whole. :-)

Brian
Reply all
Reply to author
Forward
0 new messages