Ryu S.
Ryu S.

Reputation: 1989

In Atom Editor: How to remain inside sidebar focus while navigating through tree using arrowskeys

I can open the TreeView with ctrl-\ (Linux/Windows) and get focus. At this point I can navigate around with the arrow keys on the keyboard, but only by hitting enter and losing focus, am I able to see the file which was selected.

Is there a way to remain focused on the TreeView and with arrow key navigation enabled, and as each new file is navigated over, the editor will automatically switch to that tab or open a new tab with that file open?

The functionality would be similar to the synced-sidebar Package except in the opposite direction, i.e., you would navigate the TreeView with the arrow keys and the tab view would change instead.

Upvotes: 5

Views: 683

Answers (3)

Grey
Grey

Reputation: 58

Unfortunately, A. Campbell's solution doesn't seem to work; it's called on right-arrow press, but doesn't appear to do anything.

For others ending up here searching for a solution, there's a long-opened github issue with a working answer by ThomasChef.

Put this in your init.coffee: (mind the spaces...)

atom.commands.add '.tree-view', 'custom:expand-item-down': ->
  fs = require 'fs'
  item = atom.workspace.getActivePaneItem()
  atom.commands.dispatch(item.element, 'core:move-down')
  if fs.lstatSync(item.selectedPath).isDirectory()
    return
  else
    item.openSelectedEntry(pending: true, activatePane: false)
    return
atom.commands.add '.tree-view', 'custom:expand-item-up': ->
  fs = require 'fs'
  item = atom.workspace.getActivePaneItem()
  atom.commands.dispatch(item.element, 'core:move-up')
  if fs.lstatSync(item.selectedPath).isDirectory()
    return
  else
    item.openSelectedEntry(pending: true, activatePane: false)
    return

and put this in your keymap.cson:

'.tree-view':
  'down': 'custom:expand-item-down',
  'up': 'custom:expand-item-up'

Upvotes: 1

Rizqi N. Assyaufi
Rizqi N. Assyaufi

Reputation: 1000

For toggle open & close sidebar tree view CTRL+\.

For move focus around sidebar and editor page ALT+\.

Tested on Atom for GNU/Linux.

I'm so sorry if my answer not related with your question.

enter image description here

Upvotes: 0

A. Campbell
A. Campbell

Reputation: 414

You could add the following code to your init.coffee file:

atom.commands.add '.tree-view', 'tree-view:preview', ->
    for panel in atom.workspace.getLeftPanels()
        if panel.item.constructor.name == "TreeView"
            entry = panel.item.selectedEntry()
            if entry.classList[0] == "directory"
              entry.toggleExpansion()
              return
            else
              atom.workspace.open(entry.getPath(), pending: true, activatePane: false)
              return

After which you can set the code to execute by adding a new keymap to the keymap.cson file, like so:

'.tree-view':
  'right': 'tree-view:preview'

Using the right arrow will open files and directories in the tree view without moving focus to the editor. I'd suggest using enter to toggle focus once you're ready to edit a file.

Upvotes: 2

Related Questions