Repeating through lines in vim
up vote
7
down vote
favorite
I have some lines of text like this:
firstName
lastName
email
Now I add private readonly string
to the start of the first line. How do I repeat this edit for the other lines? (not manually visiting each line of course) (BTW I do realize this could be done with a regex, but I'm looking for a "vi way" if possible)
Just in case, I'm using VsVim
vim vi
add a comment |
up vote
7
down vote
favorite
I have some lines of text like this:
firstName
lastName
email
Now I add private readonly string
to the start of the first line. How do I repeat this edit for the other lines? (not manually visiting each line of course) (BTW I do realize this could be done with a regex, but I'm looking for a "vi way" if possible)
Just in case, I'm using VsVim
vim vi
add a comment |
up vote
7
down vote
favorite
up vote
7
down vote
favorite
I have some lines of text like this:
firstName
lastName
email
Now I add private readonly string
to the start of the first line. How do I repeat this edit for the other lines? (not manually visiting each line of course) (BTW I do realize this could be done with a regex, but I'm looking for a "vi way" if possible)
Just in case, I'm using VsVim
vim vi
I have some lines of text like this:
firstName
lastName
email
Now I add private readonly string
to the start of the first line. How do I repeat this edit for the other lines? (not manually visiting each line of course) (BTW I do realize this could be done with a regex, but I'm looking for a "vi way" if possible)
Just in case, I'm using VsVim
vim vi
vim vi
asked Mar 11 '11 at 20:44
Mauricio Scheffer
586724
586724
add a comment |
add a comment |
6 Answers
6
active
oldest
votes
up vote
7
down vote
accepted
Navigate up to firstName
in Normal mode and type
qaIprivate readonly string <ESC>jq
This will record the macro (in register a
) of you adding "private readonly string " to the beginning of the line, then moving one line down. If you want to repeat this macro twice (thus repeating the command for the next two lines), in command mode type 2@a
on the lastName
line, which will execute the macro twice more.
This method fulfills your requirement of "not manually visiting each line"; however it's a little heavyweight for your application.
If it's the typing of private readonly string
that you object to, a quick solution is to make your edit on the first line, then move down to each line you want to make the edit and use .
in Normal mode. This will repeat your last command (in this case adding the text to the line) with no fuss.
cool, I was actually looking for a way to repeat.
but I guess it's not possible in this case...
– Mauricio Scheffer
Mar 11 '11 at 21:38
@muasch: Well, you can, you'd just useqa.jq
as your macro (repeat command, then go one line down), and use2@a
to replay it. It's a little messy though.
– Zeke
Mar 11 '11 at 21:47
+1 The qa ... q macro is flexible and can solve a lot of problems, it's good to know. A little overkill for this question, but it will probably save you a lot of time in the future.
– Johan
Mar 17 '11 at 7:48
add a comment |
up vote
7
down vote
Another way to do this is to using Visual Block mode:
- Start with your cursor at the beginning of the first line.
- Press Ctrl+v to enter Visual Block mode.
- Press j to move down, extending the selection to include the lines you want.
- Press I to go into (prepending) Insert mode.
- Type
private readonly string
. - Press Esc. This will cause you to go back to command mode, and the text you typed will be repeated on each line before the start of your visual block selection (in this case, at the start of each line, since that's where you began the selection).
To extend the selection to every line in the file, step 1 could begg
and step 3 could beG0
.
– Dennis Williamson
Mar 12 '11 at 1:24
add a comment |
up vote
6
down vote
:normal is great (and shorter) too:
:.,+2norm Iprivate readonly string
.,+2
- from here to 2 two lines bellow (or you could select the lines using visual mode shift+v and then typing : to get into ex mode)
norm
- type these commands as if I typed them in normal (command) mode
I(...)
- insert the following string in the beginning of the line
+1 did not know about :norm, it's quite nice
– Johan
Mar 17 '11 at 7:58
add a comment |
up vote
2
down vote
Just type "private readonly string" at the first line. Then for the rest of the 2 lines type 2(dot character) like 2. from the second line.
add a comment |
up vote
1
down vote
Give this a try:
:%s/^/private readonly string /
I suppose you could write a script, but why?
sorry, I said I'm looking for a "vi way", not a regex. I already know regexes and I'm trying to learn vim.
– Mauricio Scheffer
Mar 11 '11 at 21:03
add a comment |
up vote
0
down vote
For interactively doing this without much thought and preparation (in case you have already entered the text on the first line but you later notice that you need it on several lines):
Insert the text as you would normally do using
I
in the first line.Turn on the visual mode by pressing
v
orV
or<c-v>
.Grab all the lines you want to apply the change. In this case,
jj
will do.Hit
:norm .
then enter.
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fsuperuser.com%2fquestions%2f256461%2frepeating-through-lines-in-vim%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
7
down vote
accepted
Navigate up to firstName
in Normal mode and type
qaIprivate readonly string <ESC>jq
This will record the macro (in register a
) of you adding "private readonly string " to the beginning of the line, then moving one line down. If you want to repeat this macro twice (thus repeating the command for the next two lines), in command mode type 2@a
on the lastName
line, which will execute the macro twice more.
This method fulfills your requirement of "not manually visiting each line"; however it's a little heavyweight for your application.
If it's the typing of private readonly string
that you object to, a quick solution is to make your edit on the first line, then move down to each line you want to make the edit and use .
in Normal mode. This will repeat your last command (in this case adding the text to the line) with no fuss.
cool, I was actually looking for a way to repeat.
but I guess it's not possible in this case...
– Mauricio Scheffer
Mar 11 '11 at 21:38
@muasch: Well, you can, you'd just useqa.jq
as your macro (repeat command, then go one line down), and use2@a
to replay it. It's a little messy though.
– Zeke
Mar 11 '11 at 21:47
+1 The qa ... q macro is flexible and can solve a lot of problems, it's good to know. A little overkill for this question, but it will probably save you a lot of time in the future.
– Johan
Mar 17 '11 at 7:48
add a comment |
up vote
7
down vote
accepted
Navigate up to firstName
in Normal mode and type
qaIprivate readonly string <ESC>jq
This will record the macro (in register a
) of you adding "private readonly string " to the beginning of the line, then moving one line down. If you want to repeat this macro twice (thus repeating the command for the next two lines), in command mode type 2@a
on the lastName
line, which will execute the macro twice more.
This method fulfills your requirement of "not manually visiting each line"; however it's a little heavyweight for your application.
If it's the typing of private readonly string
that you object to, a quick solution is to make your edit on the first line, then move down to each line you want to make the edit and use .
in Normal mode. This will repeat your last command (in this case adding the text to the line) with no fuss.
cool, I was actually looking for a way to repeat.
but I guess it's not possible in this case...
– Mauricio Scheffer
Mar 11 '11 at 21:38
@muasch: Well, you can, you'd just useqa.jq
as your macro (repeat command, then go one line down), and use2@a
to replay it. It's a little messy though.
– Zeke
Mar 11 '11 at 21:47
+1 The qa ... q macro is flexible and can solve a lot of problems, it's good to know. A little overkill for this question, but it will probably save you a lot of time in the future.
– Johan
Mar 17 '11 at 7:48
add a comment |
up vote
7
down vote
accepted
up vote
7
down vote
accepted
Navigate up to firstName
in Normal mode and type
qaIprivate readonly string <ESC>jq
This will record the macro (in register a
) of you adding "private readonly string " to the beginning of the line, then moving one line down. If you want to repeat this macro twice (thus repeating the command for the next two lines), in command mode type 2@a
on the lastName
line, which will execute the macro twice more.
This method fulfills your requirement of "not manually visiting each line"; however it's a little heavyweight for your application.
If it's the typing of private readonly string
that you object to, a quick solution is to make your edit on the first line, then move down to each line you want to make the edit and use .
in Normal mode. This will repeat your last command (in this case adding the text to the line) with no fuss.
Navigate up to firstName
in Normal mode and type
qaIprivate readonly string <ESC>jq
This will record the macro (in register a
) of you adding "private readonly string " to the beginning of the line, then moving one line down. If you want to repeat this macro twice (thus repeating the command for the next two lines), in command mode type 2@a
on the lastName
line, which will execute the macro twice more.
This method fulfills your requirement of "not manually visiting each line"; however it's a little heavyweight for your application.
If it's the typing of private readonly string
that you object to, a quick solution is to make your edit on the first line, then move down to each line you want to make the edit and use .
in Normal mode. This will repeat your last command (in this case adding the text to the line) with no fuss.
edited Mar 11 '11 at 21:26
answered Mar 11 '11 at 21:16
Zeke
18619
18619
cool, I was actually looking for a way to repeat.
but I guess it's not possible in this case...
– Mauricio Scheffer
Mar 11 '11 at 21:38
@muasch: Well, you can, you'd just useqa.jq
as your macro (repeat command, then go one line down), and use2@a
to replay it. It's a little messy though.
– Zeke
Mar 11 '11 at 21:47
+1 The qa ... q macro is flexible and can solve a lot of problems, it's good to know. A little overkill for this question, but it will probably save you a lot of time in the future.
– Johan
Mar 17 '11 at 7:48
add a comment |
cool, I was actually looking for a way to repeat.
but I guess it's not possible in this case...
– Mauricio Scheffer
Mar 11 '11 at 21:38
@muasch: Well, you can, you'd just useqa.jq
as your macro (repeat command, then go one line down), and use2@a
to replay it. It's a little messy though.
– Zeke
Mar 11 '11 at 21:47
+1 The qa ... q macro is flexible and can solve a lot of problems, it's good to know. A little overkill for this question, but it will probably save you a lot of time in the future.
– Johan
Mar 17 '11 at 7:48
cool, I was actually looking for a way to repeat
.
but I guess it's not possible in this case...– Mauricio Scheffer
Mar 11 '11 at 21:38
cool, I was actually looking for a way to repeat
.
but I guess it's not possible in this case...– Mauricio Scheffer
Mar 11 '11 at 21:38
@muasch: Well, you can, you'd just use
qa.jq
as your macro (repeat command, then go one line down), and use 2@a
to replay it. It's a little messy though.– Zeke
Mar 11 '11 at 21:47
@muasch: Well, you can, you'd just use
qa.jq
as your macro (repeat command, then go one line down), and use 2@a
to replay it. It's a little messy though.– Zeke
Mar 11 '11 at 21:47
+1 The qa ... q macro is flexible and can solve a lot of problems, it's good to know. A little overkill for this question, but it will probably save you a lot of time in the future.
– Johan
Mar 17 '11 at 7:48
+1 The qa ... q macro is flexible and can solve a lot of problems, it's good to know. A little overkill for this question, but it will probably save you a lot of time in the future.
– Johan
Mar 17 '11 at 7:48
add a comment |
up vote
7
down vote
Another way to do this is to using Visual Block mode:
- Start with your cursor at the beginning of the first line.
- Press Ctrl+v to enter Visual Block mode.
- Press j to move down, extending the selection to include the lines you want.
- Press I to go into (prepending) Insert mode.
- Type
private readonly string
. - Press Esc. This will cause you to go back to command mode, and the text you typed will be repeated on each line before the start of your visual block selection (in this case, at the start of each line, since that's where you began the selection).
To extend the selection to every line in the file, step 1 could begg
and step 3 could beG0
.
– Dennis Williamson
Mar 12 '11 at 1:24
add a comment |
up vote
7
down vote
Another way to do this is to using Visual Block mode:
- Start with your cursor at the beginning of the first line.
- Press Ctrl+v to enter Visual Block mode.
- Press j to move down, extending the selection to include the lines you want.
- Press I to go into (prepending) Insert mode.
- Type
private readonly string
. - Press Esc. This will cause you to go back to command mode, and the text you typed will be repeated on each line before the start of your visual block selection (in this case, at the start of each line, since that's where you began the selection).
To extend the selection to every line in the file, step 1 could begg
and step 3 could beG0
.
– Dennis Williamson
Mar 12 '11 at 1:24
add a comment |
up vote
7
down vote
up vote
7
down vote
Another way to do this is to using Visual Block mode:
- Start with your cursor at the beginning of the first line.
- Press Ctrl+v to enter Visual Block mode.
- Press j to move down, extending the selection to include the lines you want.
- Press I to go into (prepending) Insert mode.
- Type
private readonly string
. - Press Esc. This will cause you to go back to command mode, and the text you typed will be repeated on each line before the start of your visual block selection (in this case, at the start of each line, since that's where you began the selection).
Another way to do this is to using Visual Block mode:
- Start with your cursor at the beginning of the first line.
- Press Ctrl+v to enter Visual Block mode.
- Press j to move down, extending the selection to include the lines you want.
- Press I to go into (prepending) Insert mode.
- Type
private readonly string
. - Press Esc. This will cause you to go back to command mode, and the text you typed will be repeated on each line before the start of your visual block selection (in this case, at the start of each line, since that's where you began the selection).
answered Mar 11 '11 at 21:41
MikeSep
1,09985
1,09985
To extend the selection to every line in the file, step 1 could begg
and step 3 could beG0
.
– Dennis Williamson
Mar 12 '11 at 1:24
add a comment |
To extend the selection to every line in the file, step 1 could begg
and step 3 could beG0
.
– Dennis Williamson
Mar 12 '11 at 1:24
To extend the selection to every line in the file, step 1 could be
gg
and step 3 could be G0
.– Dennis Williamson
Mar 12 '11 at 1:24
To extend the selection to every line in the file, step 1 could be
gg
and step 3 could be G0
.– Dennis Williamson
Mar 12 '11 at 1:24
add a comment |
up vote
6
down vote
:normal is great (and shorter) too:
:.,+2norm Iprivate readonly string
.,+2
- from here to 2 two lines bellow (or you could select the lines using visual mode shift+v and then typing : to get into ex mode)
norm
- type these commands as if I typed them in normal (command) mode
I(...)
- insert the following string in the beginning of the line
+1 did not know about :norm, it's quite nice
– Johan
Mar 17 '11 at 7:58
add a comment |
up vote
6
down vote
:normal is great (and shorter) too:
:.,+2norm Iprivate readonly string
.,+2
- from here to 2 two lines bellow (or you could select the lines using visual mode shift+v and then typing : to get into ex mode)
norm
- type these commands as if I typed them in normal (command) mode
I(...)
- insert the following string in the beginning of the line
+1 did not know about :norm, it's quite nice
– Johan
Mar 17 '11 at 7:58
add a comment |
up vote
6
down vote
up vote
6
down vote
:normal is great (and shorter) too:
:.,+2norm Iprivate readonly string
.,+2
- from here to 2 two lines bellow (or you could select the lines using visual mode shift+v and then typing : to get into ex mode)
norm
- type these commands as if I typed them in normal (command) mode
I(...)
- insert the following string in the beginning of the line
:normal is great (and shorter) too:
:.,+2norm Iprivate readonly string
.,+2
- from here to 2 two lines bellow (or you could select the lines using visual mode shift+v and then typing : to get into ex mode)
norm
- type these commands as if I typed them in normal (command) mode
I(...)
- insert the following string in the beginning of the line
edited Mar 13 '11 at 19:13
answered Mar 13 '11 at 18:56
Leonardo Constantino
52645
52645
+1 did not know about :norm, it's quite nice
– Johan
Mar 17 '11 at 7:58
add a comment |
+1 did not know about :norm, it's quite nice
– Johan
Mar 17 '11 at 7:58
+1 did not know about :norm, it's quite nice
– Johan
Mar 17 '11 at 7:58
+1 did not know about :norm, it's quite nice
– Johan
Mar 17 '11 at 7:58
add a comment |
up vote
2
down vote
Just type "private readonly string" at the first line. Then for the rest of the 2 lines type 2(dot character) like 2. from the second line.
add a comment |
up vote
2
down vote
Just type "private readonly string" at the first line. Then for the rest of the 2 lines type 2(dot character) like 2. from the second line.
add a comment |
up vote
2
down vote
up vote
2
down vote
Just type "private readonly string" at the first line. Then for the rest of the 2 lines type 2(dot character) like 2. from the second line.
Just type "private readonly string" at the first line. Then for the rest of the 2 lines type 2(dot character) like 2. from the second line.
answered Mar 17 '11 at 6:30
adithya
511
511
add a comment |
add a comment |
up vote
1
down vote
Give this a try:
:%s/^/private readonly string /
I suppose you could write a script, but why?
sorry, I said I'm looking for a "vi way", not a regex. I already know regexes and I'm trying to learn vim.
– Mauricio Scheffer
Mar 11 '11 at 21:03
add a comment |
up vote
1
down vote
Give this a try:
:%s/^/private readonly string /
I suppose you could write a script, but why?
sorry, I said I'm looking for a "vi way", not a regex. I already know regexes and I'm trying to learn vim.
– Mauricio Scheffer
Mar 11 '11 at 21:03
add a comment |
up vote
1
down vote
up vote
1
down vote
Give this a try:
:%s/^/private readonly string /
I suppose you could write a script, but why?
Give this a try:
:%s/^/private readonly string /
I suppose you could write a script, but why?
answered Mar 11 '11 at 20:58
Dennis Williamson
75.9k14129167
75.9k14129167
sorry, I said I'm looking for a "vi way", not a regex. I already know regexes and I'm trying to learn vim.
– Mauricio Scheffer
Mar 11 '11 at 21:03
add a comment |
sorry, I said I'm looking for a "vi way", not a regex. I already know regexes and I'm trying to learn vim.
– Mauricio Scheffer
Mar 11 '11 at 21:03
sorry, I said I'm looking for a "vi way", not a regex. I already know regexes and I'm trying to learn vim.
– Mauricio Scheffer
Mar 11 '11 at 21:03
sorry, I said I'm looking for a "vi way", not a regex. I already know regexes and I'm trying to learn vim.
– Mauricio Scheffer
Mar 11 '11 at 21:03
add a comment |
up vote
0
down vote
For interactively doing this without much thought and preparation (in case you have already entered the text on the first line but you later notice that you need it on several lines):
Insert the text as you would normally do using
I
in the first line.Turn on the visual mode by pressing
v
orV
or<c-v>
.Grab all the lines you want to apply the change. In this case,
jj
will do.Hit
:norm .
then enter.
add a comment |
up vote
0
down vote
For interactively doing this without much thought and preparation (in case you have already entered the text on the first line but you later notice that you need it on several lines):
Insert the text as you would normally do using
I
in the first line.Turn on the visual mode by pressing
v
orV
or<c-v>
.Grab all the lines you want to apply the change. In this case,
jj
will do.Hit
:norm .
then enter.
add a comment |
up vote
0
down vote
up vote
0
down vote
For interactively doing this without much thought and preparation (in case you have already entered the text on the first line but you later notice that you need it on several lines):
Insert the text as you would normally do using
I
in the first line.Turn on the visual mode by pressing
v
orV
or<c-v>
.Grab all the lines you want to apply the change. In this case,
jj
will do.Hit
:norm .
then enter.
For interactively doing this without much thought and preparation (in case you have already entered the text on the first line but you later notice that you need it on several lines):
Insert the text as you would normally do using
I
in the first line.Turn on the visual mode by pressing
v
orV
or<c-v>
.Grab all the lines you want to apply the change. In this case,
jj
will do.Hit
:norm .
then enter.
answered Nov 27 '16 at 11:04
off99555
1012
1012
add a comment |
add a comment |
Thanks for contributing an answer to Super User!
- 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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2fsuperuser.com%2fquestions%2f256461%2frepeating-through-lines-in-vim%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