vi, search string and comment line howto?

529 views
Skip to first unread message

omonte7

unread,
Feb 26, 2010, 5:26:43 PM2/26/10
to vim...@googlegroups.com

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 #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.

John Beckett

unread,
Feb 26, 2010, 5:44:02 PM2/26/10
to vim...@googlegroups.com
omonte7 wrote:
> I should be able to string all the above commands together
> separated by a pipe:
> vi -c "/my_string | I# | :wq" file

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

Tim Chase

unread,
Feb 26, 2010, 5:44:48 PM2/26/10
to vim...@googlegroups.com
> 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
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

omonte7

unread,
Feb 28, 2010, 5:24:54 PM2/28/10
to vim...@googlegroups.com

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:

Tim Chase

unread,
Feb 28, 2010, 5:55:16 PM2/28/10
to vim...@googlegroups.com
Did my reply not come through on the 26th?

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

John Beckett

unread,
Feb 28, 2010, 7:27:58 PM2/28/10
to vim...@googlegroups.com
omonte7 wrote:
> Subject: vi, search string and comment line howto?

The answers to your previous post are here:
http://groups.google.com/group/vim_use/browse_thread/thread/3ff28355fd3416fa

John

omonte7

unread,
Mar 2, 2010, 7:00:21 PM3/2/10
to vim...@googlegroups.com

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 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:

John Beckett

unread,
Mar 2, 2010, 11:23:41 PM3/2/10
to vim...@googlegroups.com, olv...@gmail.com
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/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

monte

unread,
Mar 3, 2010, 1:21:22 PM3/3/10
to vim_use
Thanks to all of you who posted, and I apologize for posting in the
wrong forum and for reposting (something was up with my post being
accepted to nabble). The following both worked great, I needed a vim
only solution as I'm running on various distributed operating systems
and sed (with out the -i option) was to much work.

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...

Reply all
Reply to author
Forward
0 new messages