Comment all lines in a text file?

11,054

Solution 1

In Vim: gg0<ctrl-v>GI;<Esc>

Solution 2

In Vim you can also just do the global replace on the start of all lines:

:%s/^/;/

Solution 3

For a simple task like this you could use sed or perl. For a small footprint, use sed for this simple task:

sed -i.old -e 's/^/;/' file

This preserves a copy of the original file as "*.old" and adds a ";" at the beginning of every line.

In the event that your sed isn't a GNU version (as is the case on many Unix variants), it is likely that you won't have the inplace (-i) option. Either simply redirect to a new output file or use Perl as perl -pi.old -e 's/^/;/' file

Solution 4

I would add to jw013's answer by changing the replace to

:%s/^[^;]/;\0/

That can be rerun as many times as you like and will not add semicolons endlessly to the beginning of lines already having it in the beginning.

It can also be used with sed or perl with the colon and percentage sign removed.

It replaces in all lines (%) every line that starts with something other than a semicolon to start with a semicolon and then the character that it started with originally (to keep it in the file).

Both commands (mine and jw013's) should be done in command mode, which is the default start mode of vi/vim and can be accessed with the key when in Insert or replace modes.

Share:
11,054

Related videos on Youtube

Nik
Author by

Nik

Updated on September 18, 2022

Comments

  • Nik
    Nik over 1 year

    In a text file I need to comment out all lines by adding a ";" as first character of each line. What is a good way to do this? I thought of Vim's visual block mode, but I couldn't find a "select all" option and marking several hundred lines manually also isn't great ;-) Any idea? I have nano, vi and vim at hand, I would prefer one of those for this task.

  • Nik
    Nik over 11 years
    Thank you, that works great, but I don't understand (and according to my testing also don't need) the "gg" at the beginning?
  • jw013
    jw013 over 11 years
    gg goes to the first line.
  • Nik
    Nik over 11 years
    thanks, a nice way for a script, for now I prefer the vim solution as I need to edit the file anyway and need an editor for that :-)
  • Izkata
    Izkata over 11 years
    Why is the 0 necessary? gg also goes to the beginning of the line (unless there's a configuration option I'm not aware of?)
  • jw013
    jw013 over 11 years
    gg goes to the first non-blank character. This is not what you want if the first character on the first line was a space or tab.
  • Alessio
    Alessio over 11 years
    explanation: : to enter Ex command mode, % to specify all lines in the file, s/^/;/ to insert a ; at the beginning of every line. The % is a range, it means all lines. type :help ranges in vim to get more information about ranges in 4. Ex command-line ranges
  • Paweł Rumian
    Paweł Rumian over 11 years
    After pressing I, the whole block selection disappears and the comment show only at the first line. What may be wrong?
  • echristopherson
    echristopherson over 11 years
    @gorkypl: While in insert mode, you'll just see your edit in the first line. Once you hit <kbd>Esc</kbd>, it will apply the edit to the whole block.
  • echristopherson
    echristopherson over 11 years
    Sorry about the "<kbd></kbd>" -- I keep forgetting that doesn't work in comments. I meant just Esc.
  • mc0e
    mc0e over 9 years
    Good to understand your options, and choose what's appropriate but I'd often prefer to not avoid the double commenting because then I can see what was previously already commented, and I can reverse the operation.