Search code examples
tabsparenthesesatom-editor

Jump over end parenthesis/bracket/quotation in atom editor with TAB


In atom editor, when I type console.log( for example, it becomes console.log() and the cursor stays between the two parenthesis. So I have to use End button or Right arrow key to jump out of there. Is there any way to use Tab instead (to jump out of ending parenthesis/brackets/quotation) ?


Solution

  • If you just keep typing, then the closing ) will be 'swallowed' by Atom's bracket matcher, so you don't need to press End or .

    However, there are some situations where Atom's bracket matcher doesn't swallow up keypresses and you can't just keep typing. For example, when you enter the following code, after pressing ; you might need to move the cursor past the closing curly brace (which Atom automatically inserted):

    if (someCondition) {
        doSomething();
    }
    

    In situations like this, you can use a custom command and a custom keymap to jump the cursor forward. Here's how...


    Go to the file menu and select 'Open Your Init Script', then paste the following code into the file. This defines a command that can move the cursor forward, jumping over a single bracket, brace, or quotation mark.

    SymbolRegex = /\s*[(){}<>[\]/'"]/
    atom.commands.add 'atom-text-editor', 'custom:jump-over-symbol': (event) ->
      editor = atom.workspace.getActiveTextEditor()
      cursorMoved = false
      for cursor in editor.getCursors()
        range = cursor.getCurrentWordBufferRange(wordRegex: SymbolRegex)
        unless range.isEmpty()
          cursor.setBufferPosition(range.end)
          cursorMoved = true
      event.abortKeyBinding() unless cursorMoved
    

    You have to close and re-open Atom to reload the init script.

    Next, go to the file menu, select 'Open Your Keymap', and enter a keybinding for the new command. You could use the TAB key, but that would conflict with Atom's default keymap for snippets, so here I have used Alt+) instead:

    'atom-text-editor:not([mini])':
      'alt-)': 'custom:jump-over-symbol'
    

    Another option is just to disable Atom's automatic insertion of closing brackets. I think you can do that by going to Settings → Packages → bracket-matcher → Settings, and clearing the option 'Autocomplete Brackets'.