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
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
Does your buffer contain lines other thantext
? And do thetext
lines start on line 0?
– DJMcMayhem♦
Jan 17 at 19:31
@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
Jan 17 at 20:04
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
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
asked Jan 17 at 18:57
MiniMaxMiniMax
1185
1185
Does your buffer contain lines other thantext
? And do thetext
lines start on line 0?
– DJMcMayhem♦
Jan 17 at 19:31
@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
Jan 17 at 20:04
add a comment |
Does your buffer contain lines other thantext
? And do thetext
lines start on line 0?
– DJMcMayhem♦
Jan 17 at 19:31
@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
Jan 17 at 20:04
Does your buffer contain lines other than
text
? And do the text
lines start on line 0?– DJMcMayhem♦
Jan 17 at 19:31
Does your buffer contain lines other than
text
? And do the text
lines start on line 0?– DJMcMayhem♦
Jan 17 at 19:31
@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
Jan 17 at 20:04
@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
Jan 17 at 20:04
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
Jan 17 at 20:06
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
Jan 17 at 20:15
@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♦
Jan 17 at 20:20
1
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. Note for others: the information about them located in the:h gv
and:h Ctrl-a
.
– MiniMax
Jan 17 at 20:40
add a comment |
Answer to the question №1:
:let n=1 | g/text/s/$/=printf(" %d", n)/ | let n+=1
Result
text 1
text 2
text 3
text 4
text 5
Answer to the question №2:
The substitute expression can contain multiple variables separated (concatenated) by dot .
operator.
:let a = 'one'
:let b = 'two'
:let c = 'three'
:g/text/s/$/=a.b.c/
Result
textonetwothree
textonetwothree
textonetwothree
textonetwothree
textonetwothree
If it is needed separate them by space, then do:
:g/text/s/$/=' '.a.' '.b.' '.c/
or
:g/text/s/$/=printf(' %s %s %s', a, b, c)/
Result
text one two three
text one two three
text one two three
text one two three
text one two three
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
});
}
});
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%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
Jan 17 at 20:06
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
Jan 17 at 20:15
@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♦
Jan 17 at 20:20
1
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. Note for others: the information about them located in the:h gv
and:h Ctrl-a
.
– MiniMax
Jan 17 at 20:40
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
Jan 17 at 20:06
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
Jan 17 at 20:15
@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♦
Jan 17 at 20:20
1
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. Note for others: the information about them located in the:h gv
and:h Ctrl-a
.
– MiniMax
Jan 17 at 20:40
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 Jan 17 at 20:23
answered Jan 17 at 19:33
DJMcMayhem♦DJMcMayhem
11.1k12862
11.1k12862
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
Jan 17 at 20:06
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
Jan 17 at 20:15
@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♦
Jan 17 at 20:20
1
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. Note for others: the information about them located in the:h gv
and:h Ctrl-a
.
– MiniMax
Jan 17 at 20:40
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
Jan 17 at 20:06
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
Jan 17 at 20:15
@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♦
Jan 17 at 20:20
1
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. Note for others: the information about them located in the:h gv
and:h Ctrl-a
.
– MiniMax
Jan 17 at 20:40
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
Jan 17 at 20:06
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
Jan 17 at 20:06
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
Jan 17 at 20:15
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
Jan 17 at 20:15
@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♦
Jan 17 at 20:20
@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♦
Jan 17 at 20:20
1
1
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. Note for others: the information about them located in the :h gv
and :h Ctrl-a
.– MiniMax
Jan 17 at 20:40
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. Note for others: the information about them located in the :h gv
and :h Ctrl-a
.– MiniMax
Jan 17 at 20:40
add a comment |
Answer to the question №1:
:let n=1 | g/text/s/$/=printf(" %d", n)/ | let n+=1
Result
text 1
text 2
text 3
text 4
text 5
Answer to the question №2:
The substitute expression can contain multiple variables separated (concatenated) by dot .
operator.
:let a = 'one'
:let b = 'two'
:let c = 'three'
:g/text/s/$/=a.b.c/
Result
textonetwothree
textonetwothree
textonetwothree
textonetwothree
textonetwothree
If it is needed separate them by space, then do:
:g/text/s/$/=' '.a.' '.b.' '.c/
or
:g/text/s/$/=printf(' %s %s %s', a, b, c)/
Result
text one two three
text one two three
text one two three
text one two three
text one two three
add a comment |
Answer to the question №1:
:let n=1 | g/text/s/$/=printf(" %d", n)/ | let n+=1
Result
text 1
text 2
text 3
text 4
text 5
Answer to the question №2:
The substitute expression can contain multiple variables separated (concatenated) by dot .
operator.
:let a = 'one'
:let b = 'two'
:let c = 'three'
:g/text/s/$/=a.b.c/
Result
textonetwothree
textonetwothree
textonetwothree
textonetwothree
textonetwothree
If it is needed separate them by space, then do:
:g/text/s/$/=' '.a.' '.b.' '.c/
or
:g/text/s/$/=printf(' %s %s %s', a, b, c)/
Result
text one two three
text one two three
text one two three
text one two three
text one two three
add a comment |
Answer to the question №1:
:let n=1 | g/text/s/$/=printf(" %d", n)/ | let n+=1
Result
text 1
text 2
text 3
text 4
text 5
Answer to the question №2:
The substitute expression can contain multiple variables separated (concatenated) by dot .
operator.
:let a = 'one'
:let b = 'two'
:let c = 'three'
:g/text/s/$/=a.b.c/
Result
textonetwothree
textonetwothree
textonetwothree
textonetwothree
textonetwothree
If it is needed separate them by space, then do:
:g/text/s/$/=' '.a.' '.b.' '.c/
or
:g/text/s/$/=printf(' %s %s %s', a, b, c)/
Result
text one two three
text one two three
text one two three
text one two three
text one two three
Answer to the question №1:
:let n=1 | g/text/s/$/=printf(" %d", n)/ | let n+=1
Result
text 1
text 2
text 3
text 4
text 5
Answer to the question №2:
The substitute expression can contain multiple variables separated (concatenated) by dot .
operator.
:let a = 'one'
:let b = 'two'
:let c = 'three'
:g/text/s/$/=a.b.c/
Result
textonetwothree
textonetwothree
textonetwothree
textonetwothree
textonetwothree
If it is needed separate them by space, then do:
:g/text/s/$/=' '.a.' '.b.' '.c/
or
:g/text/s/$/=printf(' %s %s %s', a, b, c)/
Result
text one two three
text one two three
text one two three
text one two three
text one two three
edited Jan 18 at 11:22
answered Jan 17 at 19:25
MiniMaxMiniMax
1185
1185
add a comment |
add a comment |
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');
});
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');
});
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
Does your buffer contain lines other than
text
? And do thetext
lines start on line 0?– DJMcMayhem♦
Jan 17 at 19:31
@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
Jan 17 at 20:04