Eclipse multi-line indent uses tabs instead of spaces (pydev)
Asked Answered
A

4

20

When I highlight a block of text in Eclipse, and press TAB, it inserts a tab at the head of each line. I want it to insert 4 spaces instead.

I need to do this on a files with a custom extension: *.shpaml

I have PyDev installed.

Aloeswood answered 25/1, 2012 at 19:26 Comment(1)
This is because tabs are the one true indent, a single character created for the sole purpose of indenting your code by one, which you can choose to display in your editor as whatever depth you prefer.Overtly
E
12

I'm considering that *.shpaml is opened with the PyDev editor...

There are 2 preferences related to that:

In window > preferences > PyDev > Editor, you can choose 'replace tabs with spaces when typing?' and in that same page 'assume tab spacing when file contains tabs?'.

So, probably you have both checked and your *.shpaml has some tab in it, which makes PyDev work with tabs... if you uncheck the 'assume tab spacing when file contains tabs?', it should do what you want.

Elodia answered 27/1, 2012 at 14:56 Comment(1)
The option 'assume tab spacing when file contains tabs?' is what was tripping me up. I couldn't figure out why the tab to space feature was "broken" for me until seeing this.Sid
D
8

You should be able to set "spaces instead of tabs" here:

  Eclipse, Window, Preferences, General, Editors, Text Editors,
      Insert spaces for tabs=Y
Duaneduarchy answered 25/1, 2012 at 19:31 Comment(2)
That only seems to work for single line indentation. With only "Insert spaces for tabs=Y" set, I still get TAB chars inserted when trying to indent a block of code.Looby
Me too. You get tabs if you select some text and then hit the tab key (it doesn't have to be a multiple line selection)Gustie
D
7

Use the settings: Eclipse, Window, Preferences, General, Editors, Text Editors, Tick 'Replace tabs with spaces when typing' This was 'Insert spaces for tabs' in older Eclipse versions)

The new name makes it clearer that this option only applies to typing and not indenting with the tab key.

 Also untick setting 'Assume tab spacing when files contain tabs'
 Otherwise Eclipse will still fall vack to tab spacing when files contain tabs'. 

Opening files in other editors may still lead to tabs.

It is also wise to remove all the existing tabs from your file. Open each file, then from the Find-Replace Function (Ctrl-F), tick 'Regular expressions' then Find: \t and Replace by 4 spaces.

Or Source -> 'Convert tabs to space-tabs'

However, tabs will still be inserted by the Shift Left and Multi-line tab key. Two options are to convert the tabs back to spaces using one of the above methods or instead of using tab or shift-right, right-click on the selected block and choose one of the 'Quick fix' options such as surround the block with an if, while or try - if that is appropriate in the context.

Doorplate answered 29/4, 2014 at 2:7 Comment(1)
This is an underappreciated tip untick setting 'Assume tab spacing when files contain tabs' Thanks!Sedimentary
N
0

Additionally, I had this same problem when making sure a module was PEP8 compliant and had to do this for code that was already written in which case the method above does not work.

Go to windows > preferences ? PyDev ? text editor click show whitespace characters. Highlight a tab and click cntrl f. Paste the tab whitespace into find box and four spaces into the replace with box. Hit replace all.

Nickelplate answered 19/11, 2013 at 20:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.