Vim - how to replace one new line n with two n's
Clash Royale CLAN TAG#URR8PPP
In vim editor, I want to replace a newline character (n) with two new line characters (nn) using vim command mode.
Input file content:
This is my first line.
This is second line.
Command that I tried:
:%s/n/nn/g
it replaces the string with unwanted characters as
This is my first line.^@^@This is second line.^@^@
Then I tried the following command
:%s/n/rr/g
It is working properly. Can you explain me why it is working fine with second command?
vim regular-expression vi-mode
add a comment |
In vim editor, I want to replace a newline character (n) with two new line characters (nn) using vim command mode.
Input file content:
This is my first line.
This is second line.
Command that I tried:
:%s/n/nn/g
it replaces the string with unwanted characters as
This is my first line.^@^@This is second line.^@^@
Then I tried the following command
:%s/n/rr/g
It is working properly. Can you explain me why it is working fine with second command?
vim regular-expression vi-mode
See also unix.stackexchange.com/a/206404
– Stéphane Chazelas
Dec 4 '15 at 11:35
2
FYI: There is also a Vi and Vim (beta) SE.
– Dubu
Dec 4 '15 at 12:55
2
Side note, you don't have to use/
as the separator. This is more readable::%s;n;nn;g
– DJMcMayhem
Dec 5 '15 at 4:02
1
Search forn
, replace withrr
,:% s/n/rr/g
: stackoverflow.com/a/71334/911945
– Anton Tarasenko
Dec 7 '18 at 15:36
add a comment |
In vim editor, I want to replace a newline character (n) with two new line characters (nn) using vim command mode.
Input file content:
This is my first line.
This is second line.
Command that I tried:
:%s/n/nn/g
it replaces the string with unwanted characters as
This is my first line.^@^@This is second line.^@^@
Then I tried the following command
:%s/n/rr/g
It is working properly. Can you explain me why it is working fine with second command?
vim regular-expression vi-mode
In vim editor, I want to replace a newline character (n) with two new line characters (nn) using vim command mode.
Input file content:
This is my first line.
This is second line.
Command that I tried:
:%s/n/nn/g
it replaces the string with unwanted characters as
This is my first line.^@^@This is second line.^@^@
Then I tried the following command
:%s/n/rr/g
It is working properly. Can you explain me why it is working fine with second command?
vim regular-expression vi-mode
vim regular-expression vi-mode
edited Dec 4 '15 at 12:14
Michael Durrant
16.5k45121187
16.5k45121187
asked Dec 4 '15 at 10:21
RaghvendraRaghvendra
372139
372139
See also unix.stackexchange.com/a/206404
– Stéphane Chazelas
Dec 4 '15 at 11:35
2
FYI: There is also a Vi and Vim (beta) SE.
– Dubu
Dec 4 '15 at 12:55
2
Side note, you don't have to use/
as the separator. This is more readable::%s;n;nn;g
– DJMcMayhem
Dec 5 '15 at 4:02
1
Search forn
, replace withrr
,:% s/n/rr/g
: stackoverflow.com/a/71334/911945
– Anton Tarasenko
Dec 7 '18 at 15:36
add a comment |
See also unix.stackexchange.com/a/206404
– Stéphane Chazelas
Dec 4 '15 at 11:35
2
FYI: There is also a Vi and Vim (beta) SE.
– Dubu
Dec 4 '15 at 12:55
2
Side note, you don't have to use/
as the separator. This is more readable::%s;n;nn;g
– DJMcMayhem
Dec 5 '15 at 4:02
1
Search forn
, replace withrr
,:% s/n/rr/g
: stackoverflow.com/a/71334/911945
– Anton Tarasenko
Dec 7 '18 at 15:36
See also unix.stackexchange.com/a/206404
– Stéphane Chazelas
Dec 4 '15 at 11:35
See also unix.stackexchange.com/a/206404
– Stéphane Chazelas
Dec 4 '15 at 11:35
2
2
FYI: There is also a Vi and Vim (beta) SE.
– Dubu
Dec 4 '15 at 12:55
FYI: There is also a Vi and Vim (beta) SE.
– Dubu
Dec 4 '15 at 12:55
2
2
Side note, you don't have to use
/
as the separator. This is more readable: :%s;n;nn;g
– DJMcMayhem
Dec 5 '15 at 4:02
Side note, you don't have to use
/
as the separator. This is more readable: :%s;n;nn;g
– DJMcMayhem
Dec 5 '15 at 4:02
1
1
Search for
n
, replace with rr
, :% s/n/rr/g
: stackoverflow.com/a/71334/911945– Anton Tarasenko
Dec 7 '18 at 15:36
Search for
n
, replace with rr
, :% s/n/rr/g
: stackoverflow.com/a/71334/911945– Anton Tarasenko
Dec 7 '18 at 15:36
add a comment |
4 Answers
4
active
oldest
votes
Oddly enough, n
in vim for replacement does not mean newline, but null. ASCII nul is ^@
(control@).
Historically, vi replaces ^M
(controlM) as the line-ending, which is the newline. vim added an extension r
(like the C language) to mean the same as ^M
, but the developers chose to make n
mean null when replacing text. This is inconsistent with its use in searches, which find a newline.
Further reading:
Search and replace (Vim wiki)- vim replace character to n
add a comment |
Try this: %s/$/^V^M/
(where ^V
is Ctrl-V
and ^M
is Ctrl-M
- when you type ^V
it will print a ^
char then backspace over it and then when you type the ^M
it will appear as ^M
... the Ctrl-V is the standard tty literal next character - run the command stty -a
to show your tty's special chars).
Indeed, the very experienced will know this for the ever required conversion from CRLF as an alternative to dropping out and using dos2unix or similar. The Ctrl-V usage is a day 1 vi lesson.
– mckenzm
Dec 4 '15 at 20:29
add a comment |
vim
use n
to represent a null character in memory, that how
vim
handle file contain null character (while vi
can not).
The use of n
only match end of line in the buffer, not the newline in the string when using in expression.
See :h NL-used-for-Null and :h CR-used-for-NL for more details.
add a comment |
Tested with Neovim (nvim
) and Vim
versions
NVIM v0.3.0
VIM - Vi IMproved 8.1
Remove a newline:
cell
allele
rs2981578
fgfr2
Here, n
matches newlines (to insert a newline however, use r
):
:'<,'>s/n/|/g
cell|allele|rs2981578|fgfr2
Add a newline:
Nope:
cell|allele|rs2981578|fgfr2
:'<,'>s/n/|/g
cell^@allele^@rs2981578^@fgfr2
^@
is a diacritic mark for LF (line feed; see
http://vimhelp.appspot.com/digraph.txt.html#digraphs-default)
Yes:
cell|allele|rs2981578|fgfr2
:'<,'>s/|/r/g
cell
allele
rs2981578
fgfr2
To address the OP's question (how to replace one n
with two, nn
), just add another r
:
cell
allele
rs2981578
fgfr2
:'<,'>s/n/rr/g
cell
allele
rs2981578
fgfr2
... i.e. match the original newline (n
), and replace it with two newlines/carriage returns (rr
).
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f247329%2fvim-how-to-replace-one-new-line-n-with-two-ns%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Oddly enough, n
in vim for replacement does not mean newline, but null. ASCII nul is ^@
(control@).
Historically, vi replaces ^M
(controlM) as the line-ending, which is the newline. vim added an extension r
(like the C language) to mean the same as ^M
, but the developers chose to make n
mean null when replacing text. This is inconsistent with its use in searches, which find a newline.
Further reading:
Search and replace (Vim wiki)- vim replace character to n
add a comment |
Oddly enough, n
in vim for replacement does not mean newline, but null. ASCII nul is ^@
(control@).
Historically, vi replaces ^M
(controlM) as the line-ending, which is the newline. vim added an extension r
(like the C language) to mean the same as ^M
, but the developers chose to make n
mean null when replacing text. This is inconsistent with its use in searches, which find a newline.
Further reading:
Search and replace (Vim wiki)- vim replace character to n
add a comment |
Oddly enough, n
in vim for replacement does not mean newline, but null. ASCII nul is ^@
(control@).
Historically, vi replaces ^M
(controlM) as the line-ending, which is the newline. vim added an extension r
(like the C language) to mean the same as ^M
, but the developers chose to make n
mean null when replacing text. This is inconsistent with its use in searches, which find a newline.
Further reading:
Search and replace (Vim wiki)- vim replace character to n
Oddly enough, n
in vim for replacement does not mean newline, but null. ASCII nul is ^@
(control@).
Historically, vi replaces ^M
(controlM) as the line-ending, which is the newline. vim added an extension r
(like the C language) to mean the same as ^M
, but the developers chose to make n
mean null when replacing text. This is inconsistent with its use in searches, which find a newline.
Further reading:
Search and replace (Vim wiki)- vim replace character to n
edited May 23 '17 at 12:40
Community♦
1
1
answered Dec 4 '15 at 10:29
Thomas DickeyThomas Dickey
54.1k5106178
54.1k5106178
add a comment |
add a comment |
Try this: %s/$/^V^M/
(where ^V
is Ctrl-V
and ^M
is Ctrl-M
- when you type ^V
it will print a ^
char then backspace over it and then when you type the ^M
it will appear as ^M
... the Ctrl-V is the standard tty literal next character - run the command stty -a
to show your tty's special chars).
Indeed, the very experienced will know this for the ever required conversion from CRLF as an alternative to dropping out and using dos2unix or similar. The Ctrl-V usage is a day 1 vi lesson.
– mckenzm
Dec 4 '15 at 20:29
add a comment |
Try this: %s/$/^V^M/
(where ^V
is Ctrl-V
and ^M
is Ctrl-M
- when you type ^V
it will print a ^
char then backspace over it and then when you type the ^M
it will appear as ^M
... the Ctrl-V is the standard tty literal next character - run the command stty -a
to show your tty's special chars).
Indeed, the very experienced will know this for the ever required conversion from CRLF as an alternative to dropping out and using dos2unix or similar. The Ctrl-V usage is a day 1 vi lesson.
– mckenzm
Dec 4 '15 at 20:29
add a comment |
Try this: %s/$/^V^M/
(where ^V
is Ctrl-V
and ^M
is Ctrl-M
- when you type ^V
it will print a ^
char then backspace over it and then when you type the ^M
it will appear as ^M
... the Ctrl-V is the standard tty literal next character - run the command stty -a
to show your tty's special chars).
Try this: %s/$/^V^M/
(where ^V
is Ctrl-V
and ^M
is Ctrl-M
- when you type ^V
it will print a ^
char then backspace over it and then when you type the ^M
it will appear as ^M
... the Ctrl-V is the standard tty literal next character - run the command stty -a
to show your tty's special chars).
answered Dec 4 '15 at 12:10
Murray JensenMurray Jensen
1,349176
1,349176
Indeed, the very experienced will know this for the ever required conversion from CRLF as an alternative to dropping out and using dos2unix or similar. The Ctrl-V usage is a day 1 vi lesson.
– mckenzm
Dec 4 '15 at 20:29
add a comment |
Indeed, the very experienced will know this for the ever required conversion from CRLF as an alternative to dropping out and using dos2unix or similar. The Ctrl-V usage is a day 1 vi lesson.
– mckenzm
Dec 4 '15 at 20:29
Indeed, the very experienced will know this for the ever required conversion from CRLF as an alternative to dropping out and using dos2unix or similar. The Ctrl-V usage is a day 1 vi lesson.
– mckenzm
Dec 4 '15 at 20:29
Indeed, the very experienced will know this for the ever required conversion from CRLF as an alternative to dropping out and using dos2unix or similar. The Ctrl-V usage is a day 1 vi lesson.
– mckenzm
Dec 4 '15 at 20:29
add a comment |
vim
use n
to represent a null character in memory, that how
vim
handle file contain null character (while vi
can not).
The use of n
only match end of line in the buffer, not the newline in the string when using in expression.
See :h NL-used-for-Null and :h CR-used-for-NL for more details.
add a comment |
vim
use n
to represent a null character in memory, that how
vim
handle file contain null character (while vi
can not).
The use of n
only match end of line in the buffer, not the newline in the string when using in expression.
See :h NL-used-for-Null and :h CR-used-for-NL for more details.
add a comment |
vim
use n
to represent a null character in memory, that how
vim
handle file contain null character (while vi
can not).
The use of n
only match end of line in the buffer, not the newline in the string when using in expression.
See :h NL-used-for-Null and :h CR-used-for-NL for more details.
vim
use n
to represent a null character in memory, that how
vim
handle file contain null character (while vi
can not).
The use of n
only match end of line in the buffer, not the newline in the string when using in expression.
See :h NL-used-for-Null and :h CR-used-for-NL for more details.
answered Dec 4 '15 at 10:58
cuonglmcuonglm
105k25210307
105k25210307
add a comment |
add a comment |
Tested with Neovim (nvim
) and Vim
versions
NVIM v0.3.0
VIM - Vi IMproved 8.1
Remove a newline:
cell
allele
rs2981578
fgfr2
Here, n
matches newlines (to insert a newline however, use r
):
:'<,'>s/n/|/g
cell|allele|rs2981578|fgfr2
Add a newline:
Nope:
cell|allele|rs2981578|fgfr2
:'<,'>s/n/|/g
cell^@allele^@rs2981578^@fgfr2
^@
is a diacritic mark for LF (line feed; see
http://vimhelp.appspot.com/digraph.txt.html#digraphs-default)
Yes:
cell|allele|rs2981578|fgfr2
:'<,'>s/|/r/g
cell
allele
rs2981578
fgfr2
To address the OP's question (how to replace one n
with two, nn
), just add another r
:
cell
allele
rs2981578
fgfr2
:'<,'>s/n/rr/g
cell
allele
rs2981578
fgfr2
... i.e. match the original newline (n
), and replace it with two newlines/carriage returns (rr
).
add a comment |
Tested with Neovim (nvim
) and Vim
versions
NVIM v0.3.0
VIM - Vi IMproved 8.1
Remove a newline:
cell
allele
rs2981578
fgfr2
Here, n
matches newlines (to insert a newline however, use r
):
:'<,'>s/n/|/g
cell|allele|rs2981578|fgfr2
Add a newline:
Nope:
cell|allele|rs2981578|fgfr2
:'<,'>s/n/|/g
cell^@allele^@rs2981578^@fgfr2
^@
is a diacritic mark for LF (line feed; see
http://vimhelp.appspot.com/digraph.txt.html#digraphs-default)
Yes:
cell|allele|rs2981578|fgfr2
:'<,'>s/|/r/g
cell
allele
rs2981578
fgfr2
To address the OP's question (how to replace one n
with two, nn
), just add another r
:
cell
allele
rs2981578
fgfr2
:'<,'>s/n/rr/g
cell
allele
rs2981578
fgfr2
... i.e. match the original newline (n
), and replace it with two newlines/carriage returns (rr
).
add a comment |
Tested with Neovim (nvim
) and Vim
versions
NVIM v0.3.0
VIM - Vi IMproved 8.1
Remove a newline:
cell
allele
rs2981578
fgfr2
Here, n
matches newlines (to insert a newline however, use r
):
:'<,'>s/n/|/g
cell|allele|rs2981578|fgfr2
Add a newline:
Nope:
cell|allele|rs2981578|fgfr2
:'<,'>s/n/|/g
cell^@allele^@rs2981578^@fgfr2
^@
is a diacritic mark for LF (line feed; see
http://vimhelp.appspot.com/digraph.txt.html#digraphs-default)
Yes:
cell|allele|rs2981578|fgfr2
:'<,'>s/|/r/g
cell
allele
rs2981578
fgfr2
To address the OP's question (how to replace one n
with two, nn
), just add another r
:
cell
allele
rs2981578
fgfr2
:'<,'>s/n/rr/g
cell
allele
rs2981578
fgfr2
... i.e. match the original newline (n
), and replace it with two newlines/carriage returns (rr
).
Tested with Neovim (nvim
) and Vim
versions
NVIM v0.3.0
VIM - Vi IMproved 8.1
Remove a newline:
cell
allele
rs2981578
fgfr2
Here, n
matches newlines (to insert a newline however, use r
):
:'<,'>s/n/|/g
cell|allele|rs2981578|fgfr2
Add a newline:
Nope:
cell|allele|rs2981578|fgfr2
:'<,'>s/n/|/g
cell^@allele^@rs2981578^@fgfr2
^@
is a diacritic mark for LF (line feed; see
http://vimhelp.appspot.com/digraph.txt.html#digraphs-default)
Yes:
cell|allele|rs2981578|fgfr2
:'<,'>s/|/r/g
cell
allele
rs2981578
fgfr2
To address the OP's question (how to replace one n
with two, nn
), just add another r
:
cell
allele
rs2981578
fgfr2
:'<,'>s/n/rr/g
cell
allele
rs2981578
fgfr2
... i.e. match the original newline (n
), and replace it with two newlines/carriage returns (rr
).
edited Mar 5 at 16:21
answered Jun 14 '18 at 18:31
Victoria StuartVictoria Stuart
17114
17114
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f247329%2fvim-how-to-replace-one-new-line-n-with-two-ns%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
See also unix.stackexchange.com/a/206404
– Stéphane Chazelas
Dec 4 '15 at 11:35
2
FYI: There is also a Vi and Vim (beta) SE.
– Dubu
Dec 4 '15 at 12:55
2
Side note, you don't have to use
/
as the separator. This is more readable::%s;n;nn;g
– DJMcMayhem
Dec 5 '15 at 4:02
1
Search for
n
, replace withrr
,:% s/n/rr/g
: stackoverflow.com/a/71334/911945– Anton Tarasenko
Dec 7 '18 at 15:36