简体   繁体   中英

How can I use gvim to add a carriage return (aka ENTER) to a pattern?

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

Use the "escape" encoding:

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

See the Vim documentation for pattern whitespace escapes.

: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.)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM