FoldingCodeInVim

Folding Code Lines in Vim

Yet another fairly obvious article on the best editor in the world, Vim! :-)

1. Folding code lines in Vim

Very useful to make a long listing more readable:

Select lines to be folded with Shift+V (Visual selection), then enter 'zf' (fold): all the selected lines will neatly "fold" into one line.

For instance, here is the full listing of a small script, where I added a lot of comments at the end:

#!/bin/bash

# test double if

file_list=`ls`

for fichier in ${file_list}; do
        if [ ! -x ${fichier} ] && [ ! -d ${fichier} ]; then
                echo "${fichier} is not a directory nor an executable"
        else
                echo "${fichier} is either a directory or an executable"
        fi
done

# end of script.

# -------------------- CORRECT OUTPUT ----------------------------
# sysop@galactus:/export/home/sysop $ ls -alhF
# total 78
# drwxr-xr-x   3 l807904  other        512 Oct 29 11:08 ./
# drwxr-xr-x  69 root     other       1.5K Oct 27 08:25 ../
# -rw-------   1 l807904  other         99 Oct 29 08:24 .bash_history
# -rw-r--r--   1 l807904  other        12K Oct 29 10:48 .check_pdl.sh.swp
# -rw-r--r--   1 l807904  other        136 Oct  8 16:03 .cshrc
# -rw-r--r--   1 l807904  other        167 Oct  8 16:03 .login
# -rw-r--r--   1 l807904  other       2.7K Oct  8 16:03 .profile
# -rw-r--r--   1 l807904  other       1.0K Oct 27 15:53 .screenrc
# -rw-------   1 l807904  other       5.7K Oct 29 08:25 .sh_history
# -rw-------   1 l807904  other       1.1K Oct 28 16:32 .viminfo
# -rw-r--r--   1 l807904  other       2.4K Oct 27 15:52 .vimrc
# -rw-r--r--   1 l807904  other       2.2K Oct 29 10:48 check_pdl.sh
# drwxr-xr-x   2 l807904  other        512 Oct 29 11:07 repertoire/
# -rwxr-xr-x   1 l807904  other        264 Oct 29 11:08 test_double_if.sh*
#
# sysop@galactus:/export/home/sysop $ ./test_double_if.sh
# check_pdl.sh is not a directory nor an executable
# repertoire is either a directory or an executable
# test_double_if.sh is either a directory or an executable

And here is the result folding the last lines of comments:

#!/bin/bash

# test double if

file_list=`ls`

for fichier in ${file_list}; do
        if [ ! -x ${fichier} ] && [ ! -d ${fichier} ]; then
                echo "${fichier} is not a directory nor an executable"
        else
                echo "${fichier} is either a directory or an executable"
        fi
done

# end of script.

+--- 22 lines: -------------------- CORRECT OUTPUT --------------------------

Much more readable, isn't it?

Please note that you can search within folds even if they are closed.

It's also possible to quickly fold long sections of code by hitting Shift+V (visual selection) and using the / search function.

2. Opening tabs in Gvim

Yet another useful function, and one that is available from the keyboard in Vim!

With these functions, I have discovered the joys of using tabs in Vim: now my editor window is crowded with tabs! :-)

3. Opening several views on the same file

Use the 'split' function, by typing: :sp.

4. Highlight current line/current column

Not something that I switch on, or use, very often, but it has its uses. Simply enter the following commands under vim:

:set cursorline cursorcolumn

And that's all there is to it!

5. How to search without case sensitivity

Something that can be quite annoying when looking through a large file, when you don't remember when you have typed "KBX-32", "Kbx-32" or "kbx-32": vim searches are case-sensitive by default. Here is how to make them case-insensitive:

:set ic

Or the long version:

:set ignorecase

To switch back to case-sensitive searches, use:

:set noic
:set noignorecase

To ignore the case in a normal search in Vim, use the \c option. For instance:

/kbx-32\c

The search command above will look for all possible variants of "kbx-32" (for instance: "KBX-32", "Kbx-32", "kbx-32", etc...)

See Also: