How can I convert tabs to spaces in every file of a directory?

后端 未结 19 1278
既然无缘
既然无缘 2020-12-02 03:48

How can I convert tabs to spaces in every file of a directory (possibly recursively)?

Also, is there a way of setting the number of spaces per tab?

19条回答
  •  無奈伤痛
    2020-12-02 03:52

    Use backslash-escaped sed.

    On linux:

    • Replace all tabs with 1 hyphen inplace, in all *.txt files:

      sed -i $'s/\t/-/g' *.txt
      
    • Replace all tabs with 1 space inplace, in all *.txt files:

      sed -i $'s/\t/ /g' *.txt
      
    • Replace all tabs with 4 spaces inplace, in all *.txt files:

      sed -i $'s/\t/    /g' *.txt
      

    On a mac:

    • Replace all tabs with 4 spaces inplace, in all *.txt files:

      sed -i '' $'s/\t/    /g' *.txt
      

提交回复
热议问题