How to add a space or a string into the substitute expression?
I am trying Vim
capabilities and stuck with this task - addition the incrementing number to the end of each line.
Testing lines:
text
text
text
text
text
This command works partially:
:let n=1 | g/text/s/$/=n/ | let n+=1
Result:
text1
text2
text3
text4
text5
But I want to have space between the added numbers and the 'text'.
The adding of space ' ' before the =
doesn't work, because the =
should be in the beginning of substitute expression, else it is not parsed as an expression, but inserted literally - text =n
:
:let n=1 | g/text/s/$/ =n/ | let n+=1 ### doesn't work as expected
So, the questions:
- Is it possible to insert a string in the substitute expression?
Like this (the n
is the variable):
s/$/string=n/
s/$/'string'=n/
or this:
s/$/='string'n/
- Can I use multiple variables in the substitute expression by separating them from each other like in the
bash
?
Example:
s/$/={var_1}{var_2}{var_3}/
3. Do you know more suitable/simple way for solving this task?
substitute
New contributor
add a comment |
I am trying Vim
capabilities and stuck with this task - addition the incrementing number to the end of each line.
Testing lines:
text
text
text
text
text
This command works partially:
:let n=1 | g/text/s/$/=n/ | let n+=1
Result:
text1
text2
text3
text4
text5
But I want to have space between the added numbers and the 'text'.
The adding of space ' ' before the =
doesn't work, because the =
should be in the beginning of substitute expression, else it is not parsed as an expression, but inserted literally - text =n
:
:let n=1 | g/text/s/$/ =n/ | let n+=1 ### doesn't work as expected
So, the questions:
- Is it possible to insert a string in the substitute expression?
Like this (the n
is the variable):
s/$/string=n/
s/$/'string'=n/
or this:
s/$/='string'n/
- Can I use multiple variables in the substitute expression by separating them from each other like in the
bash
?
Example:
s/$/={var_1}{var_2}{var_3}/
3. Do you know more suitable/simple way for solving this task?
substitute
New contributor
Does your buffer contain lines other thantext
? And do thetext
lines start on line 0?
– DJMcMayhem♦
1 hour ago
@DJMcMayhem No, the buffer can contains any characters, the Python source code, for example. Also, this action can be required in the any line number.
– MiniMax
34 mins ago
add a comment |
I am trying Vim
capabilities and stuck with this task - addition the incrementing number to the end of each line.
Testing lines:
text
text
text
text
text
This command works partially:
:let n=1 | g/text/s/$/=n/ | let n+=1
Result:
text1
text2
text3
text4
text5
But I want to have space between the added numbers and the 'text'.
The adding of space ' ' before the =
doesn't work, because the =
should be in the beginning of substitute expression, else it is not parsed as an expression, but inserted literally - text =n
:
:let n=1 | g/text/s/$/ =n/ | let n+=1 ### doesn't work as expected
So, the questions:
- Is it possible to insert a string in the substitute expression?
Like this (the n
is the variable):
s/$/string=n/
s/$/'string'=n/
or this:
s/$/='string'n/
- Can I use multiple variables in the substitute expression by separating them from each other like in the
bash
?
Example:
s/$/={var_1}{var_2}{var_3}/
3. Do you know more suitable/simple way for solving this task?
substitute
New contributor
I am trying Vim
capabilities and stuck with this task - addition the incrementing number to the end of each line.
Testing lines:
text
text
text
text
text
This command works partially:
:let n=1 | g/text/s/$/=n/ | let n+=1
Result:
text1
text2
text3
text4
text5
But I want to have space between the added numbers and the 'text'.
The adding of space ' ' before the =
doesn't work, because the =
should be in the beginning of substitute expression, else it is not parsed as an expression, but inserted literally - text =n
:
:let n=1 | g/text/s/$/ =n/ | let n+=1 ### doesn't work as expected
So, the questions:
- Is it possible to insert a string in the substitute expression?
Like this (the n
is the variable):
s/$/string=n/
s/$/'string'=n/
or this:
s/$/='string'n/
- Can I use multiple variables in the substitute expression by separating them from each other like in the
bash
?
Example:
s/$/={var_1}{var_2}{var_3}/
3. Do you know more suitable/simple way for solving this task?
substitute
substitute
New contributor
New contributor
New contributor
asked 1 hour ago
MiniMaxMiniMax
1163
1163
New contributor
New contributor
Does your buffer contain lines other thantext
? And do thetext
lines start on line 0?
– DJMcMayhem♦
1 hour ago
@DJMcMayhem No, the buffer can contains any characters, the Python source code, for example. Also, this action can be required in the any line number.
– MiniMax
34 mins ago
add a comment |
Does your buffer contain lines other thantext
? And do thetext
lines start on line 0?
– DJMcMayhem♦
1 hour ago
@DJMcMayhem No, the buffer can contains any characters, the Python source code, for example. Also, this action can be required in the any line number.
– MiniMax
34 mins ago
Does your buffer contain lines other than
text
? And do the text
lines start on line 0?– DJMcMayhem♦
1 hour ago
Does your buffer contain lines other than
text
? And do the text
lines start on line 0?– DJMcMayhem♦
1 hour ago
@DJMcMayhem No, the buffer can contains any characters, the Python source code, for example. Also, this action can be required in the any line number.
– MiniMax
34 mins ago
@DJMcMayhem No, the buffer can contains any characters, the Python source code, for example. Also, this action can be required in the any line number.
– MiniMax
34 mins ago
add a comment |
2 Answers
2
active
oldest
votes
For your answer specifically, you could get around this by concatenating a space with the number, i.e.
:let n=1 | g/text/s/$/=" ".n/ | let n+=1
If you want to do this to every line, there are some much shorter ways to do this. For example:
:%s/$/=" ".line('.')
Or if you only want to number the lines matching "text", then either of these:
:%s/text/=submatch(0)." ".line('.')
:%s/textzs/=" ".line('.')
You could even do the entire thing in normal mode. For example, you could do this:
gg<C-v>G$A 0<esc>gvg<C-a>
Where <C-v>
means ctrl-v and <C-a>
means ctrl-a
Thanks, it works. I was trying concatenation, but either without quotes, likes/$/= .num/
or with quotes, but without dot:s/$/=' 'n/
:). No, the line number can be random, as well as its content. The 'text' string were picked just for example.
– MiniMax
32 mins ago
The second solution should be:%s/$/=" ".line('.')
, otherwise it replaces thetext
part to the line number, that is not what I want. Also, it numbers all lines in the buffer, empty lines included.
– MiniMax
23 mins ago
@MiniMax Yes, good catch. I know that will do it to every line, but that was before I realized you only wanted specific lines. I'll leave that part up in case it can help someone else, but I'll edit it to be more clear about what it's doing.
– DJMcMayhem♦
18 mins ago
The third one is beautiful. I knew about incrementing number byCtrl-a
, but didn't knowg CTRL-A
. It will be good to add description forgv
andg CTRL-A
to the answer for future. (The information about them located in the:h gv
and:h Ctrl-a
).
– MiniMax
9 mins ago
add a comment |
I found the solution:
:let n=1 | g/text/s/$/=printf(" %d", n)/ | let n+=1
Result
text 1
text 2
text 3
text 4
text 5
New contributor
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "599"
};
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
});
}
});
MiniMax is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
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%2fvi.stackexchange.com%2fquestions%2f18603%2fhow-to-add-a-space-or-a-string-into-the-substitute-expression%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
For your answer specifically, you could get around this by concatenating a space with the number, i.e.
:let n=1 | g/text/s/$/=" ".n/ | let n+=1
If you want to do this to every line, there are some much shorter ways to do this. For example:
:%s/$/=" ".line('.')
Or if you only want to number the lines matching "text", then either of these:
:%s/text/=submatch(0)." ".line('.')
:%s/textzs/=" ".line('.')
You could even do the entire thing in normal mode. For example, you could do this:
gg<C-v>G$A 0<esc>gvg<C-a>
Where <C-v>
means ctrl-v and <C-a>
means ctrl-a
Thanks, it works. I was trying concatenation, but either without quotes, likes/$/= .num/
or with quotes, but without dot:s/$/=' 'n/
:). No, the line number can be random, as well as its content. The 'text' string were picked just for example.
– MiniMax
32 mins ago
The second solution should be:%s/$/=" ".line('.')
, otherwise it replaces thetext
part to the line number, that is not what I want. Also, it numbers all lines in the buffer, empty lines included.
– MiniMax
23 mins ago
@MiniMax Yes, good catch. I know that will do it to every line, but that was before I realized you only wanted specific lines. I'll leave that part up in case it can help someone else, but I'll edit it to be more clear about what it's doing.
– DJMcMayhem♦
18 mins ago
The third one is beautiful. I knew about incrementing number byCtrl-a
, but didn't knowg CTRL-A
. It will be good to add description forgv
andg CTRL-A
to the answer for future. (The information about them located in the:h gv
and:h Ctrl-a
).
– MiniMax
9 mins ago
add a comment |
For your answer specifically, you could get around this by concatenating a space with the number, i.e.
:let n=1 | g/text/s/$/=" ".n/ | let n+=1
If you want to do this to every line, there are some much shorter ways to do this. For example:
:%s/$/=" ".line('.')
Or if you only want to number the lines matching "text", then either of these:
:%s/text/=submatch(0)." ".line('.')
:%s/textzs/=" ".line('.')
You could even do the entire thing in normal mode. For example, you could do this:
gg<C-v>G$A 0<esc>gvg<C-a>
Where <C-v>
means ctrl-v and <C-a>
means ctrl-a
Thanks, it works. I was trying concatenation, but either without quotes, likes/$/= .num/
or with quotes, but without dot:s/$/=' 'n/
:). No, the line number can be random, as well as its content. The 'text' string were picked just for example.
– MiniMax
32 mins ago
The second solution should be:%s/$/=" ".line('.')
, otherwise it replaces thetext
part to the line number, that is not what I want. Also, it numbers all lines in the buffer, empty lines included.
– MiniMax
23 mins ago
@MiniMax Yes, good catch. I know that will do it to every line, but that was before I realized you only wanted specific lines. I'll leave that part up in case it can help someone else, but I'll edit it to be more clear about what it's doing.
– DJMcMayhem♦
18 mins ago
The third one is beautiful. I knew about incrementing number byCtrl-a
, but didn't knowg CTRL-A
. It will be good to add description forgv
andg CTRL-A
to the answer for future. (The information about them located in the:h gv
and:h Ctrl-a
).
– MiniMax
9 mins ago
add a comment |
For your answer specifically, you could get around this by concatenating a space with the number, i.e.
:let n=1 | g/text/s/$/=" ".n/ | let n+=1
If you want to do this to every line, there are some much shorter ways to do this. For example:
:%s/$/=" ".line('.')
Or if you only want to number the lines matching "text", then either of these:
:%s/text/=submatch(0)." ".line('.')
:%s/textzs/=" ".line('.')
You could even do the entire thing in normal mode. For example, you could do this:
gg<C-v>G$A 0<esc>gvg<C-a>
Where <C-v>
means ctrl-v and <C-a>
means ctrl-a
For your answer specifically, you could get around this by concatenating a space with the number, i.e.
:let n=1 | g/text/s/$/=" ".n/ | let n+=1
If you want to do this to every line, there are some much shorter ways to do this. For example:
:%s/$/=" ".line('.')
Or if you only want to number the lines matching "text", then either of these:
:%s/text/=submatch(0)." ".line('.')
:%s/textzs/=" ".line('.')
You could even do the entire thing in normal mode. For example, you could do this:
gg<C-v>G$A 0<esc>gvg<C-a>
Where <C-v>
means ctrl-v and <C-a>
means ctrl-a
edited 15 mins ago
answered 1 hour ago
DJMcMayhem♦DJMcMayhem
10.6k12861
10.6k12861
Thanks, it works. I was trying concatenation, but either without quotes, likes/$/= .num/
or with quotes, but without dot:s/$/=' 'n/
:). No, the line number can be random, as well as its content. The 'text' string were picked just for example.
– MiniMax
32 mins ago
The second solution should be:%s/$/=" ".line('.')
, otherwise it replaces thetext
part to the line number, that is not what I want. Also, it numbers all lines in the buffer, empty lines included.
– MiniMax
23 mins ago
@MiniMax Yes, good catch. I know that will do it to every line, but that was before I realized you only wanted specific lines. I'll leave that part up in case it can help someone else, but I'll edit it to be more clear about what it's doing.
– DJMcMayhem♦
18 mins ago
The third one is beautiful. I knew about incrementing number byCtrl-a
, but didn't knowg CTRL-A
. It will be good to add description forgv
andg CTRL-A
to the answer for future. (The information about them located in the:h gv
and:h Ctrl-a
).
– MiniMax
9 mins ago
add a comment |
Thanks, it works. I was trying concatenation, but either without quotes, likes/$/= .num/
or with quotes, but without dot:s/$/=' 'n/
:). No, the line number can be random, as well as its content. The 'text' string were picked just for example.
– MiniMax
32 mins ago
The second solution should be:%s/$/=" ".line('.')
, otherwise it replaces thetext
part to the line number, that is not what I want. Also, it numbers all lines in the buffer, empty lines included.
– MiniMax
23 mins ago
@MiniMax Yes, good catch. I know that will do it to every line, but that was before I realized you only wanted specific lines. I'll leave that part up in case it can help someone else, but I'll edit it to be more clear about what it's doing.
– DJMcMayhem♦
18 mins ago
The third one is beautiful. I knew about incrementing number byCtrl-a
, but didn't knowg CTRL-A
. It will be good to add description forgv
andg CTRL-A
to the answer for future. (The information about them located in the:h gv
and:h Ctrl-a
).
– MiniMax
9 mins ago
Thanks, it works. I was trying concatenation, but either without quotes, like
s/$/= .num/
or with quotes, but without dot: s/$/=' 'n/
:). No, the line number can be random, as well as its content. The 'text' string were picked just for example.– MiniMax
32 mins ago
Thanks, it works. I was trying concatenation, but either without quotes, like
s/$/= .num/
or with quotes, but without dot: s/$/=' 'n/
:). No, the line number can be random, as well as its content. The 'text' string were picked just for example.– MiniMax
32 mins ago
The second solution should be
:%s/$/=" ".line('.')
, otherwise it replaces the text
part to the line number, that is not what I want. Also, it numbers all lines in the buffer, empty lines included.– MiniMax
23 mins ago
The second solution should be
:%s/$/=" ".line('.')
, otherwise it replaces the text
part to the line number, that is not what I want. Also, it numbers all lines in the buffer, empty lines included.– MiniMax
23 mins ago
@MiniMax Yes, good catch. I know that will do it to every line, but that was before I realized you only wanted specific lines. I'll leave that part up in case it can help someone else, but I'll edit it to be more clear about what it's doing.
– DJMcMayhem♦
18 mins ago
@MiniMax Yes, good catch. I know that will do it to every line, but that was before I realized you only wanted specific lines. I'll leave that part up in case it can help someone else, but I'll edit it to be more clear about what it's doing.
– DJMcMayhem♦
18 mins ago
The third one is beautiful. I knew about incrementing number by
Ctrl-a
, but didn't know g CTRL-A
. It will be good to add description for gv
and g CTRL-A
to the answer for future. (The information about them located in the :h gv
and :h Ctrl-a
).– MiniMax
9 mins ago
The third one is beautiful. I knew about incrementing number by
Ctrl-a
, but didn't know g CTRL-A
. It will be good to add description for gv
and g CTRL-A
to the answer for future. (The information about them located in the :h gv
and :h Ctrl-a
).– MiniMax
9 mins ago
add a comment |
I found the solution:
:let n=1 | g/text/s/$/=printf(" %d", n)/ | let n+=1
Result
text 1
text 2
text 3
text 4
text 5
New contributor
add a comment |
I found the solution:
:let n=1 | g/text/s/$/=printf(" %d", n)/ | let n+=1
Result
text 1
text 2
text 3
text 4
text 5
New contributor
add a comment |
I found the solution:
:let n=1 | g/text/s/$/=printf(" %d", n)/ | let n+=1
Result
text 1
text 2
text 3
text 4
text 5
New contributor
I found the solution:
:let n=1 | g/text/s/$/=printf(" %d", n)/ | let n+=1
Result
text 1
text 2
text 3
text 4
text 5
New contributor
New contributor
answered 1 hour ago
MiniMaxMiniMax
1163
1163
New contributor
New contributor
add a comment |
add a comment |
MiniMax is a new contributor. Be nice, and check out our Code of Conduct.
MiniMax is a new contributor. Be nice, and check out our Code of Conduct.
MiniMax is a new contributor. Be nice, and check out our Code of Conduct.
MiniMax is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Vi and Vim 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');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
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%2fvi.stackexchange.com%2fquestions%2f18603%2fhow-to-add-a-space-or-a-string-into-the-substitute-expression%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');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
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');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
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');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
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
Does your buffer contain lines other than
text
? And do thetext
lines start on line 0?– DJMcMayhem♦
1 hour ago
@DJMcMayhem No, the buffer can contains any characters, the Python source code, for example. Also, this action can be required in the any line number.
– MiniMax
34 mins ago