Hiding comments in configuration files
Posted by rossen on Wed 10 Sep 2008 at 14:05
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/
[ Parent | Reply to this comment ]
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 | Reply to this comment ]
[ Send Message | View Steve's Scratchpad | View Weblogs ]
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
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 | Reply to this comment ]
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 | Reply to this comment ]
[ Send Message | View Utumno's Scratchpad | View Weblogs ]
Great stuff man, I love it. Especially the vim tip.
[ Parent | Reply to this comment ]
[ Send Message | View Jerais's Scratchpad ]
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
cat file | grep -v ^#
[ Parent | Reply to this comment ]
>
> 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 | Reply to this comment ]
[ Parent | Reply to this comment ]
PJ
[ Parent | Reply to this comment ]
grep -v "^#\|^[[:space:]]*$" logfile
[ Parent | Reply to this comment ]
grep -v "^#\|^ *$" logfile
just because escaping the pipe will impress people and make me seem more wizardly ;-)
There's also the option of:
egrep -v "^#|^ *$" logfile
which is slightly easier to type. I'll use that in private I think.
PJ
[ Parent | Reply to this comment ]
So you'd then use:
egrep -v "^ *#|^ *$" logfile
Though now it's almost matching what the article itself proposed :-)
PJ
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
" 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 | Reply to this comment ]