{"id":580,"date":"2014-06-19T18:43:41","date_gmt":"2014-06-19T16:43:41","guid":{"rendered":"http:\/\/www.epischel.de\/wordpress\/?p=580"},"modified":"2014-06-24T14:25:01","modified_gmt":"2014-06-24T12:25:01","slug":"emacs-create-a-new-buffer-quickly","status":"publish","type":"post","link":"https:\/\/www.epischel.de\/wordpress\/2014\/06\/emacs-create-a-new-buffer-quickly\/","title":{"rendered":"Emacs: create a new buffer quickly"},"content":{"rendered":"<p>I love to use Emacs, mostly for org-mode. I also use Notepad++ as an text editor (when using MS Windows), although I would like to use Emacs more. I noticed I choose Notepad++ when I quickly need an editor buffer to paste some text to because Notepad++ has this &#8222;New&#8220; icon in its toolbar that creates a new buffer quickly.<\/p>\n<p>Emacs is easily extendible, so how about creating a &#8222;quick new buffer&#8220; function myself? It is really easy.<\/p>\n<p>To create a new buffer you use <code>Ctrl-x b<\/code> (for function <code>switch-to-buffer<\/code>) and type a buffer name. But even typing in a name is too much for me. Automatically name the buffer &#8222;New&#8220; and add a unique number to it! There is a function called <code>rename-uniquely<\/code> which does what it says. Now we can create a function <code>make-new-buffer<\/code>:<\/p>\n<div id=\"ig-sh-1\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">code<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"code\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">(defun make-new-buffer ()<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &quot;makes a new buffer, uniquely named&quot;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; (interactive)<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; (switch-to-buffer &quot;New&quot;)<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; (rename-uniquely))<\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<p>Super easy: it creates a buffer named &#8222;New&#8220; and renames it so that it has a unique name. Bind this to a key, I use F5:<\/p>\n<div id=\"ig-sh-2\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">code<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"code\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">(global-set-key (kbd &quot;&lt;f5&gt;&quot;) 'make-new-buffer)<\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<p>Put this code in your <code>.emacs<\/code> or whatever.<\/p>\n<p>When you have a bunch of buffers called &#8222;New&lt;2>&#8220;, &#8222;New&lt;3>&#8220; etc., take a look at <code>buffer-menu<\/code>. You get a list of all buffers. Now you can operate on them. For instance, mark a buffer for deletion with <code>d<\/code>, repeat for other buffers and execute the command with <code>x<\/code>. Now you have killed a bunch of buffers quickly and easily.<\/p>\n<p>If you are new to customizing keybindings and writing custom functions, Sacha Chua wrote a nice article &#8222;<a href=\"http:\/\/sachachua.com\/blog\/2014\/06\/read-lisp-tweak-emacs-beginner-34-can-make-things-convenient\/\">Read Lisp Tweak Emacs (Beginner 3\/4): How can I make things more convenient?<\/a>&#8222;<\/f5><\/p>\n<p><b>Update<\/b>: Xah Lee over at ErgoEmacs has published <a href=\"http:\/\/ergoemacs.org\/emacs\/emacs_new_empty_buffer.html\">a similar article<\/a>. He came up with the following, slightly more complex function:<\/p>\n<div id=\"ig-sh-3\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">code<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"code\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">(defun xah-new-empty-buffer ()<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &quot;Open a new empty buffer.&quot;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; (interactive)<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; (let ((buf (generate-new-buffer &quot;untitled&quot;)))<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; (switch-to-buffer buf)<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; (funcall (and initial-major-mode))<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; (setq buffer-offer-save t)))<\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<p>What is the difference? It uses <code>generate-new-buffer<\/code> to generate a new buffer and make sure it has a unique name (line 4). Then it switches to that buffer (line 5). It sets the major mode of the new buffer to the one declared in variable <code>initial-major-mode<\/code>, if any (line 6). Setting <code>buffer-offer-save<\/code> to <code>t<\/code> let Emacs ask you for saving that buffer before exiting Emacs (line 7). <\/p>\n<div class=\"syndication-links\"><\/div>","protected":false},"excerpt":{"rendered":"<p>I love to use Emacs, mostly for org-mode. I also use Notepad++ as an text editor (when using MS Windows), although I would like to use Emacs more. I noticed I choose Notepad++ when I quickly need an editor buffer to paste some text to because Notepad++ has this &#8222;New&#8220; icon in its toolbar that&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"mf2_syndication":[],"webmentions_disabled_pings":false,"webmentions_disabled":false,"footnotes":""},"categories":[2],"tags":[72],"series":[],"class_list":["post-580","post","type-post","status-publish","format-standard","hentry","category-entwicklung","tag-emacs","kind-"],"kind":false,"_links":{"self":[{"href":"https:\/\/www.epischel.de\/wordpress\/wp-json\/wp\/v2\/posts\/580","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.epischel.de\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.epischel.de\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.epischel.de\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.epischel.de\/wordpress\/wp-json\/wp\/v2\/comments?post=580"}],"version-history":[{"count":8,"href":"https:\/\/www.epischel.de\/wordpress\/wp-json\/wp\/v2\/posts\/580\/revisions"}],"predecessor-version":[{"id":597,"href":"https:\/\/www.epischel.de\/wordpress\/wp-json\/wp\/v2\/posts\/580\/revisions\/597"}],"wp:attachment":[{"href":"https:\/\/www.epischel.de\/wordpress\/wp-json\/wp\/v2\/media?parent=580"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.epischel.de\/wordpress\/wp-json\/wp\/v2\/categories?post=580"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.epischel.de\/wordpress\/wp-json\/wp\/v2\/tags?post=580"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/www.epischel.de\/wordpress\/wp-json\/wp\/v2\/series?post=580"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}