How to get gedit to auto-indent code

后端 未结 4 717
刺人心
刺人心 2021-01-30 10:48

I\'m trying to get gedit to auto-indent my code. The auto-indent check-box in preferences doesn\'t work. Is there a way to get gedit to auto-indent code? (By the way, I only rea

4条回答
  •  没有蜡笔的小新
    2021-01-30 11:01

    Here is another workaround, you can use vim to auto indent and auto format your code from inside Gedit.

    First make sure that vim is installed. Next, add an "external tool" to Gedit from the "tools" menu and use the following code:

    #!/bin/sh
    CMD_FILE_NAME=.formatcommand;
    TMP_FILE_NAME=.tempvimfile;
    touch $CMD_FILE_NAME&&echo "gg=G :wq! "$TMP_FILE_NAME > $CMD_FILE_NAME&&(vim $GEDIT_CURRENT_DOCUMENT_NAME -s $CMD_FILE_NAME > /dev/null 2>/dev/null)&&rm $CMD_FILE_NAME;
    cat $TMP_FILE_NAME
    rm $TMP_FILE_NAME
    

    Also make sure that:

    • "Save" is set to "Current document".
    • "Input" is set to "Nothing".
    • "Output" is set to "Replace current document".

    You can also setup a hotkey, I prefer Alt+Shift+F like Netbeans. Now whenever you press the hotkey, the current file will be saved and auto formatted.

    This will work on any programming/scripting language given that the file's extension is correct

    If you don't get the indentation put filetype indent on in your ~/.vimrc file and it will work.

    For More details check my personal blog

提交回复
热议问题