Add semi-colon at end of line and return?


#1

Is there a shortcut or package that will move the cursor to the end of the current line, type a semi-colon and return?


#2

Not that I know of off the top of my head, but it shouldn’t be too hard to create one. You could even do it as part of your init.coffee:

atom.workspaceView.command 'custom:semicolonize', ->
  editor = atom.workspace.getActiveEditor()
  editor.moveToEndOfLine()
  editor.insertText(";\n")

#3

I must’ve done it wrong since I am getting an error when I invoke it via my keymap:

'.editor:not(.mini)':
 'alt-enter': 'editor:insert-br'
 'alt-space': 'editor:insert-nbsp'
 'ctrl-u': 'sftp-deployment:uploadCurrentFile'
 'ctrl-alt-u': 'sftp-deployment:uploadOpenFiles'
 'shift-enter': 'custom:semicolonize'

(I put the block you posted into my init.coffee)


#4

That’s what I get for not testing it :laughing:

If you change the first line of the function to:

editor = atom.workspace.getActiveEditor()

It should work. You may have to restart Atom first though.


#5

Perfect! Many thanks.


#6

Is anyone else running into issues with this using multiple panels?
Your solution adds the semicolon to the original panel, no matter what panel you’re currently editting


#7

I’m getting this error:

Cannot read property ‘command’ of undefined

for the init script where I put this. Can anyone help?


#8

The API has changed quite a bit since that snippet was posted. You should use the following now:

atom.commands.add 'atom-workspace', 'custom:semicolonize', ->
  editor = atom.workspace.getActiveTextEditor()
  editor.moveToEndOfLine()
  editor.insertText(";\n")

#9

@michaelaye Try this:

atom.commands.add atom.views.getView(atom.workspace), 'custom:semicolonize', ->
  editor = atom.workspace.getActiveTextEditor()
  editor.moveToEndOfLine()
  editor.insertText(";\n")

#10

Makes me think of a similar scenario for the autocomplete service:
If autocompleting a method name, it could/should add an opening and closing bracket immediately afterwards; and if the language of the file is one that requires semicolons, add that at the end as well.
After that it should return inside the brackets.


#11

Doesn’t autocomplete already support snippets? If so then this would seem doable.


#12

Ok, so I got the command implemented. Actually, I implemented the colonized version of it:

atom.commands.add 'atom-workspace', 'custom:colonize', ->
  editor = atom.workspace.getActiveTextEditor()
  editor.moveToEndOfLine()
  editor.insertText(":\n")

because I want this ability for Python. I got 2 probs with it.

  1. The keyboard command does not activate within a Python grammar, am I missing something? I got simply this in keymap.cson:

    ‘atom-text-editor’:
    ‘shift-enter’: ‘custom:colonize’

  2. If I activate the command via the command palette, it does not respect the current Python indent. How could I achieve that?

Thanks for helping an Atom noob. This editor is great fun!


#13

Have you tried using the Keybinding Resolver (Cmd+. on OS X and Ctrl+. on other platforms)? It will show you what key Atom saw you press and the command that was triggered because of it.

I’m not sure what you mean by this. Can you give some repro steps?


#14

Used the Keybinding Resolver. I used Shift-Enter as described above. It shows me that first (in green font) ‘editor:newline’ from atom-workspace atom-texteditor:not([mini]) has been used and in the 2nd line (in black font) my command is listed, with the 2nd column stating atom-text-editor. Do I have to control their order somehow?

With not respecting the current Python indent I simply mean that if my current line starts in column 5 and I execute my command via the command palette, the cursor ends up being in column 1 instead of column 5.


#15

In essence, yes. Key bindings follow CSS specificity rules:

https://atom.io/docs/v1.0.0/behind-atom-keymaps-in-depth#specificity-and-cascade-order

So what you have to do is make your key binding at least as specific as the built-in one. The easy way is to copy the selector from the winning one and use that for your key binding:

'atom-workspace atom-texteditor:not([mini])':
  'shift-enter': 'custom:colonize'

For this, you could extend the command to detect the indentation level of the current line and duplicate that when inserting the text.


#16

Does not work, now only the previous winning one appears and mine is gone.


#17

Oops … I copy-pasted your text for the selector without proofreading, it should look like this:

'atom-workspace atom-text-editor:not([mini])':
  'shift-enter': 'custom:colonize'

#18

Sorry, my bad. Works now. For the indentation, all that was required was adding this to the end of the function:

editor.autoIndentSelectedRows()

and it seems to work now, cool!


#19

But the enter/return key already knows how to do that, so wouldn’t it be better to execute that command instead of inserting a literal newline character?

In Python, if I’m not mistaken, then after a colon you get an additional indent.


#20

and how do I press programmatically the Enter/Return key? :wink: