0

I write python code on vim/gedit/other text editors. So, each time I add a new loop to my code, I re-indent following lines.

#original code
for (something):
    do something.

#new code
for (something else):
    for (something): #line manually re-indented 
        do something #line manually re-indented

What is a better way of doing it on text editors (specifically, gedit)? I use python for scripting mostly, so would prefer not to use an IDE.

rivu
  • 2,004
  • 2
  • 29
  • 45
  • I don't know gedit, but in some IDEs you can mark a block of code and press TAB, and it will add another level of indentation to that block. – Barmar Sep 04 '14 at 01:04
  • @GWW The answers there don't seem to address this question, could you be more specific. – Barmar Sep 04 '14 at 01:06
  • @Barmar: You are right this does seem more specific, I retracted my close vote. – GWW Sep 04 '14 at 01:07
  • actually, the TAB works, but using space deletes the whole thing. – rivu Sep 04 '14 at 01:08
  • vim is `:set autoindent`, specifically, so long as you have the other python-specific tweaks to make tabs "soft." See http://stackoverflow.com/questions/65076/how-to-setup-vim-autoindentation-properly-for-editing-python-files-py – roippi Sep 04 '14 at 01:08
  • @rivu: Why is the question tagged for vim, when you are asking about gedit? – GWW Sep 04 '14 at 01:09
  • yes, should delete the vim tag, thought the solution might be same for both gedit and vim. – rivu Sep 04 '14 at 01:09
  • If the only answer you're looking for is "There is no better way, you really do need to indent those lines, but any decent editor can make that easy," then yes, it's the same for gedit and vim. If the answer you're looking for is how each decent editor makes it easy, then it's different for each one. – abarnert Sep 04 '14 at 01:15
  • Is gedit's way of indenting those lines by selecting them and using `>` command? If no, then gedit and vim use different keys to indent the lines. – Zach Sep 04 '14 at 01:16
  • Also, as far as "I use python for scripting mostly, so would prefer not to use an IDE": there are some IDEs that can be used in a minimalist way. PyDev or Xcode might get in the way of just editing a script so can you run it on the command line, but, say, PyCharm, or even IDLE can be used as nothing but an editor, or as an editor plus an organizer of files, or as an editor plus an embedded command line, or as an editor plus a nice wrapper around PDB, or whatever other combination of features you want and don't want. – abarnert Sep 04 '14 at 01:17

3 Answers3

1

In gedit: Select Edit>Preferences. Select Editor tab. Check "Enable automatic indentation". Since you are editing Python, also change tab width to 4 and check "Insert spaces instead of tabs". If you need to change the indentation blocks of code select the block and press tab to indent or shift-tab to dedent.

Steven Rumbalski
  • 44,786
  • 9
  • 89
  • 119
1

If you are using vim, you can shift blocks of code to the right/left easily. Note that amount by which the shift happens depends on your shiftwidth setting in vim.

After setting visual mode, select the block of code you wish to right-indent and hit > To indent blocks to the left, use <

linuxfan
  • 1,110
  • 2
  • 17
  • 29
1

It depends upon the editor that you are using. As a matter of practicality, rather than religious philosophy, I would recommend choosing one and getting very comfortable with it. If you have to struggle a bit in other editors, it's probably okay: from my own anecdotal evidence, this gives you the best value for time spent. That's all a bit of an aside, of course.


In gedit, you should be able to highlight the desired block and hit Tab to indent the block. Shift+Tab should dedent blocks as well. You might have to fiddle with preferences to get this working. This is a pretty common way for graphical editors to handle this (some IDEs do it too).

In vim, the way I like to do this is to set shiftwidth equal to my tabstop at four spaces with :set shiftwidth=4 tabstop=4 expandtab You can then indent or dedent blocks by selecting them in visual or visual line mode (V or Shift+V), and typing << or >>. One of the nice things about this is that it has a very intuitive feel, like you are drawing arrows to indent or dedent.

sirosen
  • 1,716
  • 13
  • 17