tags:

views:

1031

answers:

3

What's the vi/gvim syntax to replace a pattern with a pattern that includes <ENTER>? I know this is possible but never felt like diving too deep in the documentation to know how to do it.

Something like this:

 :s/\(word\)/\1<ENTER>/

But correctly :)

Thanks

+5  A: 

Use the "escape" encoding:

:s/\(word\)/\1\r/

See the Vim documentation for pattern whitespace escapes.

dwc
You have to use \r for substitutions, not \n; see http://stackoverflow.com/questions/350661/vim-n-vs-r
Brian Carper
Edited to add doc link and remove \n
dwc
Ahh... that's the kind of documentation page that makes me skip the reading and NOT learn new things. I will read it... mmhh tomorrow :)
OscarRyz
\r worked perfectly btw thanks
OscarRyz
Yeah, sometimes the docs can be hard to read. I meant it more as a quick ref on which escapes are which.
dwc
+7  A: 
:s/\(word\)/\1\r/

Alternatively, use Ctrl+V or Ctrl+Q to quote (escape) the Enter key:

:s/\(word\)\1^QENTER/

Where ^Q is Ctrl+Q and ENTER is the Enter key.

Clarification: Depending on your installation, either ^Q or ^V should work. The quoting character differs on some platforms.

(This has the helpful side-effect of inserting the appropriate end-of-line character for whichever platform you're using, eliminating the CR vs. LF vs. CRLF problem.)

Adam Liss
I have always used ^v<Enter>. Just checked with a gnome-terminal on debian and vim 7.1 and ^q<Enter> does not work.
David Rodríguez - dribeas
uuh? Ctrl+V added the word "ENTER" all over the place :-/ Ctrl+Q+VK_ENTER worked perfectly
OscarRyz
@Oscar: The quoting char may differ from platform to platform. Either ^V or ^Q (but probably not both) should work.
Adam Liss
A: 

Just for clarification purposes, now that we're talking about carriage return, it should be noted that RETURN and ENTER key are not the same, or it would be more correct to say, they should not be the same.

I haven't used a desktop keyboard for some time now, but the ENTER key is usually the one on the down right side, while the RETURN key is the big one in the middle.

RETURN key is the one that should be used for entering a carriage return, while ENTER key is the one that should be used for entering commands. I remember an old DOS editor EDT, in which RETURN key was for newline and ENTER key was for giving commands. You couldn't give a command with RETURN. I think ENTER also gave ^1 (line feed).

Today that difference is somewhat lost, although I still, now and then, run into an editor that respects it.

2 examples: One, two and an even more obvious three

ldigas
I didn't quite catch that. Could you post some diagram/screenshot to clarify this point?
OscarRyz
He's saying that traditionally the enter key on the keypad is call "ENTER" and the one on the main keyboard (next to right-shift) is called "RETURN". Programs used to often treat them differently.ldigas: have a look at a desktop keyboard, they're almost always *both* labeled ENTER these days.
gravious
@Gravious: I got it. I don't remember these different labels. I'm using a laptop for some time now and I'm trying to make a picture of the last desktop keyboard I used..... mmmmhh nope, they both are label as ENTER in my mind... :)
OscarRyz
@gravious - Yes, I see that now. Guess they simplified it to avoid another "any key". I clearly remember few keyboards I've used had both keys, but that was some time ago. Would be interesting to find out if today's models send out the same signal, though.
ldigas