How to hide comments

Introduction

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

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/