Vim
2007-12-28
Folding Shell Style Comments with Vim
I often experience with people new to Unix or even people who have been using it for a while, that they tend to brush off vi/emacs editors quickly and then use simpler editors like pico, nano, gedit and the like.
They then never see why a lot of old time Unix system administrators and developers use these tools. vi for example was designed to edit files over a very slow connection (300bps). It's still very useful now, as lag still exists. It's very painful for me to see young developers use the mouse and scroll up, scroll down, when without lifting your hands from the keyboard you can jump to different functions, mark locations and jump back.
If you need to comment out some lines over a laggy ssh connection you can simple type :4,12s/^/#/ instead of type # and using cursor keys over the next 8 lines.
Speaking of comments, here's a common scenario. Sometimes configuration files are huge, because they're well commented with examples. You want to edit it, but sometimes you just want to see your modifications and only comments for the section you're looking at and not scroll through everything. You can hide the # comments with vim's folding feature.
So let's define a function to do this in our ~/.vimrc:
function! FoldShellComments()
let &foldexpr = 'getline(v:lnum)[0]=="#"'
g/.*/ if foldlevel(line(".")) > 0 | s/$/ !!!/ | endif
set foldmethod=expr
endfunction
We then want to call this up easily so we define a user command:
command! -nargs=0 FoldShellComments :call FoldShellComments()
With this whenever we want to fold commented lines, we simple type :FoldShellComments This will tab complete by the way, you just need to type :Fo<tab>
That huge squid.conf file now looks like this:

2007-08-28
loading multiple files that match a string with vim
loading multiple files that match a string with vim using command line unix tools
Using the ever useful grep, with -l option, it outputs only the matching filenames:
grep -rl --exclude '*.svn*' CMFCore *
vim loads multiples file in one line separated by spaces. So I need to join the multiple lines into one, separated by space. Here we have the paste command. For each line, put a space as the separator:
paste -s -d " "
Finally I wanted vim to load this in GUI mode with tabs, so we put it together:
gvim -p $(grep -rl --exclude '*.svn*' CMFCore * | paste -s -d " " -)
- the part inside the $( ) is the output of the command.
- using pipes | the output of grep (the list of files) is read by paste
- the - in paste is telling it to input the stdout output of grep
I then quickly have my editor setup for work,

