How to get gedit to auto-indent code
Asked Answered
S

4

55

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.)

Sudatorium answered 23/11, 2011 at 16:33 Comment(4)
What do you mean by "autoindent"? The option from gedit does this: when you type a indented line (by pressing tab, or with some spaces) and press <kbd>Enter</kbd>, the new line will have the same indentation of the previous one. Dos it not work this way? If this works, what do you expect?Fungosity
I ment like an IDE where it will automatically insert a tab between brackets. Although that, which I did not know about, sounds fine. Thanks!Sudatorium
Ah, now I understand, it so something more like Eclipse and other IDEs... AFAIk gedit does not do it neither any plugin provides such functionality. It seems to be feasible through a plugin, however, so you may look for such plugin eventually.Fungosity
Hm. O well. I still prefer gedit to IDEs right now. Every IDE I find is java based, and java and my system aren't good friends--to say the least. Its slow and buggy on my Ubuntu. I've started using gedit for all my coding, from ruby to C++. Maybe I need to go back to an IDE?Sudatorium
M
28

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/
Manes answered 18/2, 2012 at 15:0 Comment(3)
using this plugin I get an error next time starting gedit3: ERROR:/build/buildd/pygobject-3.4.0/gi/_gobject/pygobject.c:946:pygobject_new_full: assertion failed: (tp != NULL) AbortedHighboy
I have heard these kind of erorrs before, but I can't seem to reproduce them on my system. If you would find the solution, please let me know.Manes
The code has moved to github: github.com/nymanjens/gedit-intelligent-text-completionLovel
T
38

gedit has an auto indentation feature, go to

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

Tamratamsky answered 6/11, 2012 at 20:0 Comment(2)
Thanks. I already knew about this, and it didn't do what I wanted. +1 anyway.Sudatorium
Gedit auto intendation seems to put only the cursor under the indendation from previous line. It does nothing else.Highboy
M
28

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/
Manes answered 18/2, 2012 at 15:0 Comment(3)
using this plugin I get an error next time starting gedit3: ERROR:/build/buildd/pygobject-3.4.0/gi/_gobject/pygobject.c:946:pygobject_new_full: assertion failed: (tp != NULL) AbortedHighboy
I have heard these kind of erorrs before, but I can't seem to reproduce them on my system. If you would find the solution, please let me know.Manes
The code has moved to github: github.com/nymanjens/gedit-intelligent-text-completionLovel
P
18

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

Primero answered 7/6, 2012 at 14:2 Comment(5)
That's awesome! Thanks! Good timing, because I just started using vim to do the editing itself :)Sudatorium
Great :) I wrote this in a hurry so the script might not seem very elegant but it just works !Primero
Hey @HusseinElMotayam I just tried your workaround and its giving me these errors: Running tool: Format Vim: Warning: Output is not to a terminal Vim: Warning: Input is not from a terminal cat: .tempvimfile: No such file or directory rm: cannot remove `.tempvimfile': No such file or directory Exited: 256Kelantan
@HusseinElMotayam You are my hero! ;) I'll edit your post a bit to fix some minor problems.Gros
I want to love this, but it breaks python code by indenting lines that shouldn't be indented.Spermicide
E
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

    Manage External Tools screenshot

Eada answered 25/2, 2017 at 7:51 Comment(1)
Please, be nice, could you paste the py code as textColangelo

© 2022 - 2024 — McMap. All rights reserved.