Emacs like indent in Atom?


#1

Dear all =)

The indentation in Emacs is so good that if I indent the code, and the identation looks wrong, then there is a bug in my code.

I also like the way Emacs allows me to select all the code and perform an indent. And I can press Tab many times on a line and it will never indent it wrong.

Searching for ident packages for Atom gives lots of candidates, and I can’t find one that can indent the following Ruby code:

for i in 1..num_of_invoices.to_i
body = {
'IW_Action' => 'SULJEBUTTON',
'IW_FormName' => 'TositeForm',
'IW_FormClass' => 'TTositeForm',
'IW_SessionID_' => sid,
'IW_TrackID_' => one_up.next
}
res = client.post(uri, body)
end

As an example, Emacs indents it as:

for i in 1..num_of_invoices.to_i
  body = {
    'IW_Action' => 'SULJEBUTTON',
    'IW_FormName' => 'TositeForm',
    'IW_FormClass' => 'TTositeForm',
    'IW_SessionID_' => sid,
    'IW_TrackID_' => one_up.next
  }
  res = client.post(uri, body)
end

and pressing Tab on any of the lines does not mess up the identation.

Identation seems like a basic core-feature for a code editor. Can it really be correct that Atom can’t do this?

Lots of love,

Jasmine =)


#2

Just to confirm, are you getting no indentation at all? If I type out that code you shared, as long as I have the file’s language set to Ruby, the indentation works ok for me.

Sorry if I’m mis-understanding anything!


#3

Ok, that is interesting. I don’t get any identation at all.

What I do is pasting it into a file in /tmp/test.rb and then open it in Atom. It looks like it colours the code correct.

If i select all and then Edit->Lines->Auto Indent, then it indents it correct, but pressing Tab on a line inserts a tab, which seams wrong to me. Tab should never mess up the indentation.

Is that the same you do?


#4

How would you tell the editor that you wanted to indent a line further?


#5

Do you paste the unindented version of the code into a file before opening it in Atom? If so, I would expect it to not be indented in Atom as well unless you select and auto indent like you did later.

Hmm, I agree with @DamnedScholar, I would expect that if I was on a line and inserted a tab or space, then that would change the line. If I did it accidentally, I would remove the tab or select the text and use Auto Indent again.


#6

I feel the same way and rebound tab to auto-indent.

# ~/.atom/keymap.json
{
  "atom-text-editor:not([mini])": {
    "tab": "editor:auto-indent"
  }
}

It is not exactly like Emacs but is I think as close as you can get in Atom without a package.


#7

I would never want that =)


#8

Do you use the default identation that comes with Atom or a 3rd party package?

If not, how do you auto-indent using the keyboard?


#9

When I add this my ~/.atom/keymap.json and restart Atom, I don’t see anything different from before when pressing Tab.

I would be fine with using a package, if I can get Emacs like identation. Do you know of such? =)


#10

It should be:

{
  "atom-text-editor:not([mini])": {
    "tab": "editor:auto-indent"
  }
}

#11

Because I had a typo (forgot the not part) as @pfitzseb points out.


#12

Okay, well, try to view the editor’s functionality from the perspective of everybody else. People use it for taking notes and writing prose and CoffeeScript, all of which are contexts where having precise control over how many tabs are on each line are important.

So your statement that tab should never change the indentation only applies to working in code languages where the punctuation itself is sufficient to tell the editor where the indentation should be and where indentation itself isn’t meaningful except in making the code readable for humans.


#13

That is perfect =) Thanks @pfitzseb and @john =)

You say it isn’t exactly like Emacs. So far it feels like it to me. How does it differ?


#14

Hmm okay. I see your point… My thought were just that Atom were meant to be a drop in replacement for all exitsting editors; Emacs, Vi, Sublime etc. so I suspected Atom would do at least the same out of the box.

It would be nice however if Tab always did “the right thing” depending on each langauge out of the box =)


#15

How could it be a replacement for all editors if it has a feature set that strictly enforces a specific style of working with code? As you can see in this thread, Atom has a lot of capability to emulate features and have its behavior changed, and that’s how it was intended to work. If you want it to constantly be enforcing the auto-indent rules, you can make it do that.

How does it know what the right thing is? When I’m writing in punctuation-heavy languages where tabs aren’t meaningful, I’ll sketch out what I intend to do in comments, and indent the ones that are marking out structures that are going to end up indented. If the editor enforced the rules that you think should be automatic, I wouldn’t be able to mark things out like I like to.