Last Updated: February 25, 2016
·
1.041K
· Zidni Mubarock

Buffer Toggling for Atom ( like ctrl-6 in VIM)

put this on your init.coffee

class BufferToggling
  constructor: ->
    @currentBuffer = null
    @previousBuffer = null

  activate: =>
    @saveActiveBuffer()
    atom.workspace.paneContainer.onDidChangeActivePaneItem @saveActiveBuffer
    atom.commands.add '.platform-darwin',
      'user:toggle-last-pane' : @togglePreviousBuffer

  saveActiveBuffer: =>
    activePane = atom.workspace.getActivePane()
    activeEditor = activePane.getActiveEditor()
    return unless activeEditor?

    @previousBuffer = @currentBuffer if @currentBuffer?

    @currentBuffer = {
      pane: activePane,
      editor: activeEditor,
      position: activeEditor.cursors[0]?.getBufferPosition()
    }

  togglePreviousBuffer: =>
    return unless @previousBuffer?
    targetBuffer = @previousBuffer
    return if targetBuffer is @currentBuffer

    if targetBuffer.editor.buffer.file?.path
      options = {
        initialLine: targetBuffer.position.row,
        initialColumn: targetBuffer.position.column,
        activatePane: true,
        searchAllPanes: true
      }
      atom.workspace.open(targetBuffer.editor.buffer.file?.path, options)
    else
      if targetBuffer.editor not in atom.workspace.getTextEditors()
        return
      if targetBuffer.pane isnt atom.workspace.getActivePane()
        targetBuffer.pane.activate()
      if targetBuffer.editor isnt atom.workspace.getActiveTextEditor()
        atom.workspace.getActivePane().activateItem(targetBuffer.editor)
      atom.workspace.getActiveTextEditor().setCursorBufferPosition(
        targetBuffer.position,
        autoscroll: false
      )

(new BufferToggling).activate()

and add this to your keymap.cson

'.platform-darwin':
  'ctrl-6' : 'user:toggle-last-pane'