How to get gedit to auto-indent code

浪子不回头ぞ 提交于 2020-01-30 13:57:07

问题


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 really care about C++ indentation. I don't need indentation for any other language.)


回答1:


The plugin 'intelligent text completion' for gedit does exactly what you describe: https://github.com/nymanjens/gedit-intelligent-text-completion

For those who are trying to find out how to copy the files intelligent_text_completion.plugin and intelligent_text_completion.py to.local/share/gedit/plugins, please do the following:

user@example:~/.local/share$ cd ~/.local/share/
user@example:~/.local/share$ mkdir gedit
user@example:~/.local/share$ cd gedit/
user@example:~/.local/share/gedit$ mkdir plugins
user@example:~$ cp intelligent_text_completion.plugin intelligent_text_completion.py ~/.local/share/gedit/plugins/



回答2:


gedit has an auto indentation feature, go to

Edit -> Preferences -> Editor -> 3rd line




回答3:


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




回答4:


Ubuntu 16.04 and Gedit

  1. Activate External Tools Plugin

    Edit → Preferences → Plugins → External Tools

  2. Manage External Tools

    Tools → Manage External Tools

  3. Add a Tool

    • Click the + button at the bottom left of the Manage External Tools window
    • You can rename the new tool to whatever you like, e.g. JSON Formatter
    • Refer to the image below and set it up accordingly



来源:https://stackoverflow.com/questions/8245763/how-to-get-gedit-to-auto-indent-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!