Emacs new empty file!

emacs perhaps misses a “new empty file” button, that it’s available in other editors.
The button on the top left of the toolbar does a similar task, but it asks for the filename, which can be annoying.

Looking up this issue on the net i found some info (for example in stack overflow).

If you want to add a button the does the above mentioned function and possibily replace the existing top left button (in the GUI mode of course), here’s what you can add to your .emacs:
(defun new-file-tmp()
"Create a new empty file."
(interactive)
(let ((buf (generate-new-buffer "untitled")))
(switch-to-buffer buf)
(put 'buffer-offer-save 'permanent-local t)
(setq buffer-offer-save t)))
(defun tool-bar-local-item-pre (icon def key map after_item &rest props)
"Add an item to the tool bar in map MAP.
ICON names the image, DEF is the key definition and KEY is a symbol
for the fake function key in the menu keymap. Remaining arguments
PROPS are additional items to add to the menu item specification. See
Info node ‘(elisp)Tool Bar’. The item is added after AFTER_ITEM.
ICON is the base name of a file containing the image to use. The
function will first try to use low-color/ICON.xpm if ‘
display-color-cells
is less or equal to 256, then ICON.xpm, then ICON.pbm, and finally
ICON.xbm, using ‘
find-image’."
(let* ((image-exp (tool-bar--image-expression icon)))
(define-key-after map (vector key)
`(menu-item ,(symbol-name key) ,def :image ,image-exp ,@props) after_item)
(force-mode-line-update)))
(when (boundp 'tool-bar-map)
(tool-bar-local-item-pre "new" 'new-file-tmp 'new-file-tmp tool-bar-map 'new-file :label "" :help "New untitled File")
(define-key tool-bar-map (vector 'new-file) nil)
;; comment the above line if you want to keep the button for the default behavior
)
(global-set-key (kbd "C-n") 'new-file-tmp)

I’ve tested this on emacs 25 for linux and 26 for windows.
Let me know what you think of it!

Leave a Reply

Your email address will not be published. Required fields are marked *