Thursday, June 10, 2010

fixing cgi newline character problem in vim

I recently had problems with a python "hello world" cgi script. Turns out, I originally wrote the file in a windows system and when I copied the cgi directory to a linux system, I encountered newline issues.

use cat -v /path/to/file.py to print the file to stdout exposing the newline characters.

in vim use

:set fileformat=unix

or

:set fileformat=dos

to write the correct newline characters when saving a cgi file.


Another way to remove the window$ style new line character is to execute the following bash script:
>find /code/cgi-bin/ -type f -iname '*.py' -exec dos2unix {} \;

/code/cgi-bin/ is the path to search
-type f is search for file
-iname is search by name, case insensitive (python files in this example)
-exec is execute the dos2unix program with the results from find being represented by the {}

Hope these tips help.