nano

How to configure nano to set tab to 4 spaces except for Makefiles?


Is there a way to tell nano use 4 spaces insead of TABs for every file except for Makefiles?


Solution

  • I use the following in my .bashrc file:

    nano() {
      [[ -n $1 ]] && [[ `basename "$1"` =~ (Makefile|makefile|GNUmakefile|.+\.mk) ]] && command nano --tabsize 2 "$@" || command nano --tabstospaces --tabsize 4 "$@"
    }
    

    The names checked are taken from the What Name to Give Your Makefile chapter of the GNU Make manual and are ordered according to their recommendations.

    To keep things simple I only check the first argument. In my case this is sufficient as any other configuration settings are set in .nanorc (nb: you will need to remove set tabstospaces from .nanorc if present).

    I've only used this with BASH 4.x and do not yet have enough experience with the shell to know whether I've used anything incompatible with earlier versions.

    edit

    Also consider the --ignorercfiles command line flag as a way to improve upon this.