line 1 and stuff
line 2 contains my_string
line 2 and stuff
Now I want to vi the "file" and comment out the line containing "my_string"
by running the following commands:
vi file #to start editing "file"
/my_string #to search for the line I want to comment, containing
"my_string"
I# #to append a comment (#) to the beginning of the
current line
:wq #to write and quit
I should be able to string all the above commands together separated by a
pipe:
vi -c "/my_string | I# | :wq" file #give all the editing
commands to vi on the command line
vi file
/my_string | I# | :wq #or open the file in
vi and then enter the commands
but the above fails with: E486: Pattern not found: my_string | I# | wq
so, what am I doing wrong, anyone know how to accomplish searching for a
particular string and then comment out the line that it's on? Thanks.
--
View this message in context: http://old.nabble.com/vi%2C-search-string-and-comment-line-howto--tp27724017p27724017.html
Sent from the Vim - General mailing list archive at Nabble.com.
This is the Vim mailing list (not vi)!
There is an example using g// at:
http://vim.wikia.com/wiki/Power_of_g
However I would just do a substitute:
vim -c "%s/.*my_string.*/# &/" myfile.txt
The .* before and after means it finds the whole line, and the &
in the replacement represents that whole line.
John
You're passing command-line (Ex) commands to vim, not normal-mode
commands, so you can do this using ex commands instead:
vi -c "/my_string/s/^/#/" -c "wq" file.txt
Or, if you want to comment all instances of it:
vi -c "g/my_string/s/^/#/" -c "wq" file.txt
or even just do it in sed:
sed -iBAK '/my_string/s/^/#/' file.txt
-tim
line 1 and stuff
line 2 contains my_string
line 2 and stuff
Now I want to vi the "file" and comment out the line containing "my_string"
by running the following commands:
vi file #to start editing "file"
/my_string #to search for the line I want to comment, containing
"my_string"
I# #to append a comment (#) to the beginning of the
current line
:wq #to write and quit
I should be able to string all the above commands together separated by a
pipe:
In short, the pipe can be used to string together *ex* commands,
not normal-mode commands. Even then only certain ex commands can
be separated by the pipe as detailed at
:help :bar
I've pasted my original reply below in case it didn't come
through for you on the first pass.
-tim
>> Hello, I can't figure out the syntax to search
>> for a certain string and then comment out that
>> line. I know you can have multiple commands on
>> a single line, but I can't figure it out. As
>> an example I have a "file" with the following
>> contents:
>>
>> line 1 and stuff
>> line 2 contains my_string
>> line 2 and stuff
>>
>> Now I want to vi the "file" and comment out the line
containing "my_string"
>> by running the following commands:
>> vi file #to start editing "file"
>> /my_string #to search for the line I want to
comment, containing
>> "my_string"
>> I# #to append a comment (#) to the
beginning of the
>> current line
>> :wq #to write and quit
>>
>> I should be able to string all the above
>> commands together separated by a
>> pipe:
>> vi -c "/my_string | I# | :wq" file
You're passing command-line (Ex) commands to vim, not normal-mode
The answers to your previous post are here:
http://groups.google.com/group/vim_use/browse_thread/thread/3ff28355fd3416fa
John
line 1 and stuff
line 2 contains my_string
line 3 and stuff
Now I want to vi the "file" and comment out the line containing "my_string"
by running the following commands:
vi file #to start editing "file"
/my_string #to search for the line I want to comment, containing
"my_string"
I# #to append a comment (#) to the beginning of the
current line
:wq #to write and quit
I should be able to string all the above commands together separated by a
pipe:
You must be having trouble with your mail system because
you have posted the same message three times.
The replies to your message are here:
http://groups.google.com/group/vim_use/browse_thread/thread/3ff28355fd3416fa
I have set your account to "moderated" so that any further
repeats can be removed before appearing on the list.
While doing that I notice that you are set to
No Email - read this group on the web
so I will send a CC.
John
vim -c "/my_string/s/^/#/" -c "wq" file
vim -c "%s/.*my_string.*/# &/" file
Produced desired output:
# cat file
line 1 and stuff
# line 2 contains my_string
line 3 and stuf
Thanks!
On Mar 2, 11:23 pm, "John Beckett" <johnb.beck...@gmail.com> wrote:
> omonte7 wrote:
> > Subject: vi, search string and comment line howto?
>
> You must be having trouble with your mail system because
> you have posted the same message three times.
>
> The replies to your message are here:http://groups.google.com/group/vim_use/browse_thread/thread/3ff28355f...