This site is now 100% read-only, and retired.

Hiding comments in configuration files

Posted by rossen on Wed 10 Sep 2008 at 14:05

Tags: , ,

Although comments can be a blessing in the configuration file of an unfamiliar system, they eventually become annoying if one is already very familiar with the file. In some extreme cases, they can actually be an obstruction to clarity.

Here are two methods for viewing files without hash (#) comments, one for the command line and one for VIM, and a bit of advice for Debian administrators.

nocomment shell script

Here is a simple one-line shell script that I have been using for years. I call it "nocomment":

#!/bin/sh
egrep -a -v '^[[:space:]]*#' $1 | egrep -a '[[:print:]]'

It can be used as "nocomment file_to_view_clearly" or with a pipe. It filters out any lines that consist of optional whitespace followed by a '#' character and any line that does not contain at least one printable character.

Using folds to hide comments in VIM

VIM has a nice feature called "folding" that allows one to represent blocks of text with one line or "fold" that can be opened or closed as desired. To learn lots more about it, type "vim" and then ":help fold.txt".

Folding can be done manually or automatically with several methods (indentation, regular expression, syntax, diffs, and markers). The automatic method based on regular expressions can be used to detect comment lines and automatically fold them out of the way.

Open a heavily-commented file with vim and do:

:set fdm=expr
:set fde=getline(v:lnum)=~'^\\s*#'?1:getline(prevnonblank(v:lnum))=~'^\\s*#'?1:getline(nextnonblank(v:lnum))=~'^\\s*#'?1:0

Blocks of consecutive comment lines are reduced (folded) to one high-lighted line that can be opened by moving the cursor to the line and typing "zo" and closed by typing "zc". Folding can be globally toggled off and on by typing "zi".

I wish to thank everyone who contributed to the thread http://www.nabble.com/Hide-comments--td17804345.html that I found by searching Google for ' vim "hide comment" ', especially Andreas Politz who contributed the above recipe.

Configuration file examples in Debian

In many cases, Debian packages come with a set of fully-commented configuration files typically found in /usr/share/doc/package_name/examples. On initial installation of the package, these files are copied into /etc to provide a starting point for configuring the system.

If you have been using Apache or Squid or ISC DHCP or Shorewall or whatever for many years, consider how much easier to understand your configuration files would be if all of the comments were removed. In many cases, the entire configuration file could fit on one screen. You lose nothing: you can always find the comments again in the examples/ directory. At the limit, you could replace all of those extraneous comments and examples with just one line:

# See /usr/share/doc/package_name/examples/config_file
# for a complete list of all options.

Hiding comments is good, but getting rid of them completely might be even better.

About this document

URL: http://www.rtfm-sarl.ch/articles/hide-comments.txt

HTML-conversion: txt2html --titlefirst --noanchors --preformat_trigger_lines 1 --bold_delimiter '' hide-comments.txt > hide-comments.html

Title: How to hide comments

Version: 2008-09-10-001

Author: Erik Rossen <rossen@rossen.ch>

Licence: Creative Commons Attribution-Share Alike 2.5 Switzerland, http://creativecommons.org/licenses/by-sa/2.5/ch/

 

 


Re: Hiding comments in configuration files
Posted by Anonymous (68.254.xx.xx) on Wed 10 Sep 2008 at 16:37
I am an amateur sysadmin (at best) but the 'nocomments' script get 3 thumbs up from me!

[ Parent ]

Re: Hiding comments in configuration files
Posted by Anonymous (198.136.xx.xx) on Wed 10 Sep 2008 at 17:13
Be aware that it is not always safe to remove comments. There are a few examples (such as GRUB's config) where the comments actually have meaning to the dpkg configuration infrastructure.

In general, the reason why Debian leaves a fully commented default config is that "out of the box", the package is not configured to do anything. This is a combination of security and genericism. In the one case, a user who hasn't yet intentionally configured a network service is probably not yet informed enough to understand and limit the risks. In the other case, one system's use of the package may be orthogonal to another system's needs and Debian doesn't preconfigure to favor either.

Besides, having the comments immediately in the config file significantly reduces the number of times a newbie (or even an experienced system administrator) has to be told to RTFM.

[ Parent ]

Re: Hiding comments in configuration files
Posted by Steve (80.68.xx.xx) on Wed 10 Sep 2008 at 17:37
[ View Weblogs ]

Indeed. That's why I use a cfcat (comment-free-cat) script to show files without comments.

Steve

[ Parent ]

Re: Hiding comments in configuration files
Posted by Anonymous (217.11.xx.xx) on Fri 12 Sep 2008 at 09:16
The most useless cat ever included... :-)

[ Parent ]

Re: Hiding comments in configuration files
Posted by Anonymous (2001:0xx:0xx:0xxx:0xxx:0xxx:xx) on Sun 7 Dec 2008 at 21:26
That sed code looks quite complex. This awk script does the same thing?
#!/usr/bin/awk -f

NF == 0   { next } # Skip empty lines
/[ \t]*#/ { next } # Skip comment lines

{
        if (match($0, /[^\\]#/)) # Strip trailing comments
                print substr($0, 1, RLENGTH)
        else if (sub(/\\$/, "")) # Continue lines ending with '\'
                printf $0
        else
                print
}

[ Parent ]

Re: Hiding comments in configuration files
Posted by simonw (84.45.xx.xx) on Wed 10 Sep 2008 at 21:15
[ View Weblogs ]
As someone who has been struggling to do a reverse proxy with Squid on Centos, first thing I did was remove the comments since they are all about normal proxy configuration.

Making comments meaningful, ala Grub, is a system admin anti-pattern. It sits alongside working outside the packaging system, config files that don't allow easy additions and other sins of system administration and system design.

Other anti-patterns - logging in the wrong place. BIND is a classic example, it logs lame servers on the resolver, rather than when it receives a query (with no-recurse bit set) on a server that isn't authoritative for a domain. Of course there is a DoS attack there that needs resolving but that isn't rocket science (and log space is cheap and easily recycled). So the log is on the machine of the person seeing the problem, rather than the machine of the person who could potentially fix the problem.

[ Parent ]

Re: Hiding comments in configuration files
Posted by Anonymous (146.232.xx.xx) on Fri 12 Sep 2008 at 07:49
Very useful. I did not know about the vim-possibilities.
Comments are useful as a form of documentation. We use the in-config-file comments regularly to comment any change to the configuration. Therefore I prefer hding them rather than deleting them.

[ Parent ]

Re: Hiding comments in configuration files
Posted by Utumno (118.168.xx.xx) on Fri 12 Sep 2008 at 18:45
[ View Weblogs ]

Great stuff man, I love it. Especially the vim tip.

[ Parent ]

Re: Hiding comments in configuration files
Posted by Anonymous (189.13.xx.xx) on Thu 18 Sep 2008 at 00:57
I prefer just:

cat file | grep -v ^#

[ Parent ]

Re: Hiding comments in configuration files
Posted by Anonymous (94.209.xx.xx) on Sat 20 Sep 2008 at 07:34
> I prefer just:
>
> cat file | grep -v ^#

Simple, and I use that too.

But it does not handle lines with whitespace like the solution in the article does. Which is really needed for some config files. For those I use:

grep -v ^# file | sed -e "/^ *$/d"

PJ

[ Parent ]

Hiding comments in configuration files
Posted by bhante (220.227.xx.xx) on Fri 3 Oct 2008 at 05:25
grep '^[^ #]' config_file

[ Parent ]

Re: Hiding comments in configuration files
Posted by Anonymous (94.209.xx.xx) on Mon 6 Oct 2008 at 01:55
Neat. But fails if you indent with spaces, which is not uncommon.

PJ

[ Parent ]

Re: Hiding comments in configuration files
Posted by jijitus (200.69.xx.xx) on Thu 18 Sep 2008 at 04:26
[ View Weblogs ]
Pretty nice, specially for squid. Thanks!

[ Parent ]

ah.. that vim folding expression is an inspiration..
Posted by Anonymous (82.192.xx.xx) on Mon 29 Sep 2008 at 12:53

" i actually like comments.. so i don't delete them.. but this is an easy
" way to hide them if necessary (remove the set nofen line if you want to
" change the default to closed.. :)
function HideComments()
set fdm=expr
set fde=getline(v:lnum)=~'\\s*#'?1:getline(prevnonblank(v:lnum))=~'\\s*#'?1:getline(nextnonblank(v:lnum))=~'^\\s*#'?1:0
" folds are open per default..
set nofen
endfunction
au FileType config call HideComments()
au FileType conf call HideComments()
au FileType sensors call HideComments()
... more to come here 4 sure

[ Parent ]