2

I was editing a small configure script and noticed that vim's indentation of the file was not working properly. A sample of the original source follows:

while getopts 'dgSsx' opt; do
    case $opt in
    d) DEBUG=true ;;
    g) USE_GDB=true ;;
        S) USE_GEB=TRUE SMALL=true ;;
        s) SMALL=true ;;
        x) XFT=true ;;
    ?) for line in \
            '-d enable debugging with verbose log output' \
                '-g enable debug symbols' \
                '-s optimize for size' \
                '-x use Xft fonts' \
        '-? show usage'; \
        do echo $line; done ;;
    esac
done

After running gg=G, the source looks like this:

while getopts 'dgSsx' opt; do
case $opt in
d) DEBUG=true ;;
g) USE_GDB=true ;;
S) USE_GEB=TRUE SMALL=true ;;
s) SMALL=true ;;
x) XFT=true ;;
?) for line in \
'-d enable debugging with verbose log output' \
'-g enable debug symbols' \
'-s optimize for size' \
'-x use Xft fonts' \
'-? show usage'; \
do echo $line; done ;;
esac

Auto-indent works fine while editing. What is necessary to get vim's whole-file indentation to work on shell scripts?

3

After I did gg=G for the buffer with set ft=sh I get:

while getopts 'dgSsx' opt; do
    case $opt in
        d) DEBUG=true ;;
        g) USE_GDB=true ;;
        S) USE_GEB=TRUE SMALL=true ;;
        s) SMALL=true ;;
        x) XFT=true ;;
        ?) for line in \
            '-d enable debugging with verbose log output' \
            '-g enable debug symbols' \
            '-s optimize for size' \
            '-x use Xft fonts' \
            '-? show usage'; \
        do echo $line; done ;;
    esac
done

Which looks ok for me.

When I turned off filetype indent off and in a new buffer did the same I got:

while getopts 'dgSsx' opt; do
case $opt in
d) DEBUG=true ;;
g) USE_GDB=true ;;
S) USE_GEB=TRUE SMALL=true ;;
s) SMALL=true ;;
x) XFT=true ;;
?) for line in \
        '-d enable debugging with verbose log output' \
        '-g enable debug symbols' \
        '-s optimize for size' \
        '-x use Xft fonts' \
        '-? show usage'; \
        do echo $line; done ;;
esac
done

which looks almost like what you have.

  • Do you have filetype indent on in your vimrc?
  • What is your vim version? (I have the latest nightly so it might be something that was fixed)
  • What is the output of verbose set indentexpr? ?

PS

Turn on indent plugins by adding filetype indent on to your vimrc.

| improve this answer | |
  • I don't have filetype indent on in my vimrc. My vim version is 8.0, so rather old (packaged with Devuan GNU/Linux). indentexpr= is the output, so its empty. I tried setting ft=sh, but obtained the same results. I just tried to set filetype indent on in the buffer, but obtained the same results. – Jeffrey Bedard Feb 21 at 7:56
  • 1
    so then add filetype indent on to your vimrc... Usually it is filetype plugin indent on which turns on filetype and indent plugins – Maxim Kim Feb 21 at 8:00
  • 1
    That fixed it, thank you! – Jeffrey Bedard Feb 21 at 8:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.