Close unsaved file without asking to save it


#1

I would like to have a way to be able to exit the current unsaved bugger without being prompted to save it.

As in vim there’s the command

:q!

Which closes the buffer and don’t asks to save it.


#2

Are you using the vim-mode package?


#3

Yes I am @AbeEstrada


#4

Not sure if there’s an API to remove the confirmation when trying to close a pane, but, can you please make that suggestion here https://github.com/atom/vim-mode/issues ? They can help more than me.


#5

As a simple workaround for when this potentially gets added to the vim-mode package, you can put something like this in your init script

atom.workspaceView.command "user:forcequit", => atom.exit()
atom.keymaps.addKeymap "user", ".workspace": "ctrl-cmd-q": "user:forcequit"

Be warned that this closes all windows without any prompts. If you just want to force close a single file, you can do something like

atom.workspaceView.command "user:forceclose", =>
  atom.workspaceView.getActivePaneItem()?.destroy()
atom.keymaps.addKeymap "user", ".workspace": "ctrl-cmd-w": "user:forceclose"

Obviously, if you want to, you can move the keymaps to your keymap file instead, but I inlined them to keep them close to the functionality they are tied to.

Edit: Wrap the long line in the second snippet to avoid confusion


#6

Excellent this accomplished what I needed. Thanks.


#7

I would like to second this request. vim-mode-plus doesn’t recognize the “:command” operations, nonetheless, this capability should exist out of the box by a simple menu item and/or key bindings to not prompt, but just close all files open in adjacent panes without saving any changes. I make this request because I frequently open many files, modify their appearance to make them easier to analyze, then close without saving my changes.


#8

This is still not implemented in current Atom. So I expanded on @thomasjo answer to add close all (ctrl-shift-w).

home/.atom/init.js

function centerPane() {
  const panes = atom.workspace.getPanes()
    .filter(pane => pane.getContainer().getLocation() === 'center' 
                    && pane.getContainer().getActivePane() === pane)
  if(panes.length !== 1) {
    throw 'No center panel found'
  }
  return panes[0]
}
function isTextEditor(item) {
  return item && item.constructor.name === 'TextEditor'
}
function forceCloseTabs() {
  atom.commands.add('atom-text-editor', 'Custom:Close Active Editor', () => {
    activeItem = centerPane().getActiveItem()
    isTextEditor(activeItem) && activeItem.destroy()
  })
  atom.commands.add('atom-workspace', 'Custom:Close All Editor', () => {
    centerPane().getItems()
      .filter(item => isTextEditor(item))
      .forEach(item => item.destroy())
    centerPane().destroy()
  })
  atom.keymaps.add("user", {"atom-text-editor": {"ctrl-w": "Custom:Close Active Editor"}})
  atom.keymaps.add("user", {"atom-workspace": {"ctrl-shift-w": "Custom:Close All Editor"}})
}
forceCloseTabs()