Buffer Toggling - A Vim-Inspired Hack for Atom

vim, atom, coffee-script, productivity

Many people know in VIM, there is ctrl-6 shortcut. This shortcut is very useful to change fast between two recent files. I always liked this feature in VIM. So, I thought, why not in Atom editor? After some time trying and playing with code, I got this feature to work in Atom. It was not easy, but I am happy with result. This is like mixing two things I like: VIM’s quick buffer change and Atom’s friendly interface.

In next part, I want to share how I make this ctrl-6 shortcut work in Atom. It’s my little experiment. Maybe not perfect, but I think many people will find it helpful. If you like to experiment with your tools and make them better for your work, this guide can be interesting for you. We look at steps and how to apply this hack in Atom.

Setting up Buffer Toggling #

Let’s dive into the steps to set up Buffer Toggling in your Atom editor.

Step 1: Modify init.coffee #

To get started, locate your init.coffee file in Atom (create one if it’s not there). Add the following script:

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()

Step 2: Update keymap.cson #

Next, open your keymap.cson file (or create one if it’s not present) and add this line:

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

How Buffer Toggling Works #

Buffer Toggling keeps track of your current and previous buffers, allowing you to switch between them using a simple key combination (ctrl-6, just like in Vim!). This smooth navigation eliminates the need for endless scrolling or searching for previously accessed files.

Please note that Buffer Toggling is an experimental feature, not a built-in part of Atom. It’s intended to enhance our coding experience and improve workflow efficiency. If you’re intrigued by this idea, I encourage you to give Buffer Toggling a try in your Atom editor.

By the way, I’ve also shared this Buffer Toggling script on Coderwall.com. If you find this hack helpful, you can show your support by liking my post there. Your feedback and appreciation mean a lot to me! Check it out here.