{"id":785,"date":"2013-05-01T14:27:00","date_gmt":"2013-05-01T19:27:00","guid":{"rendered":"http:\/\/www.alandmoore.com\/blog\/?p=785"},"modified":"2013-05-03T11:08:18","modified_gmt":"2013-05-03T16:08:18","slug":"better-window-splitting-in-emacs","status":"publish","type":"post","link":"https:\/\/alandmoore.com\/blog\/2013\/05\/01\/better-window-splitting-in-emacs\/","title":{"rendered":"Better window splitting in Emacs"},"content":{"rendered":"<p>Emacs&#8217; window-splitting functionality is an ergonomic way to view multiple files at once without having to deal with shuffling around floating windows or clicking between tabs.  Anyone who does much with Emacs probably knows already that they can use C-x 2 or C-x 3 to split the window vertically or horizontally.\n<\/p>\n<p>\nWhat always bothered me about this feature was that the newly-created window defaulted to the current buffer, which in layspeak means you had the same file or content open in both your new window and the old.  The was almost (but not quite) <i>never<\/i> the behavior I wanted, since usually you split the window to have multiple buffers on screen at once. Typically, I would do this right after opening a new file or buffer to compare with whatever I already had open, so that I would have the old buffer in one window and the new buffer in another.\n<\/p>\n<p>\nThis is where having an extensible editor rocks; because in Emacs, if you get tired enough of a behavior, you start hacking elisp and fix it&hellip;\n<\/p>\n<p><!--more--><\/p>\n<p>\nThe fix is simply a matter of writing some custom elisp functions and overriding the default keybindings with the new functions, and adding it all to your .emacs file.  Here&#8217;s the code:\n<\/p>\n<pre lang=\"lisp\">\r\n;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\r\n;; Custom splitting functions ;;\r\n;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\r\n\r\n(defun vsplit-last-buffer ()\r\n  (interactive)\r\n  (split-window-vertically)\r\n  (other-window 1 nil)\r\n  (switch-to-next-buffer)\r\n  )\r\n(defun hsplit-last-buffer ()\r\n  (interactive)\r\n  (split-window-horizontally)\r\n  (other-window 1 nil)\r\n  (switch-to-next-buffer)\r\n  )\r\n\r\n(global-set-key (kbd \"C-x 2\") 'vsplit-last-buffer)\r\n(global-set-key (kbd \"C-x 3\") 'hsplit-last-buffer)\r\n<\/pre>\n<p>\nWhat each of these functions does is perform the regular vertical\/horizontal split, then switches to the newly-created window and performs &#8220;switch-to-next-buffer&#8221;.  &#8220;Next buffer&#8221; is the buffer you&#8217;d get if you did a &#8220;switch-buffer&#8221; (C-x b) and just accepted the default.  Obviously, Emacs can&#8217;t (yet) read your mind and open the buffer you want every time, but &#8220;next-buffer&#8221; is most likely to be the buffer you want (at least, much more likely than the current buffer, which you already have open in another window).\n<\/p>\n<p>\nCopy this code into ~\/.emacs, evaluate the buffer (or just the functions), and you&#8217;re ready to roll.\n<\/p>\n<p>\nI&#8217;m going to try this change for a while and see if it sticks, I&#8217;ll keep this post updated with changes.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The author describes an improvement to window splitting in Emacs<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[61,12,22,28],"class_list":["post-785","post","type-post","status-publish","format-standard","hentry","category-floss","tag-elisp","tag-emacs","tag-instructional","tag-programming-2"],"_links":{"self":[{"href":"https:\/\/alandmoore.com\/blog\/wp-json\/wp\/v2\/posts\/785","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alandmoore.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alandmoore.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alandmoore.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/alandmoore.com\/blog\/wp-json\/wp\/v2\/comments?post=785"}],"version-history":[{"count":14,"href":"https:\/\/alandmoore.com\/blog\/wp-json\/wp\/v2\/posts\/785\/revisions"}],"predecessor-version":[{"id":813,"href":"https:\/\/alandmoore.com\/blog\/wp-json\/wp\/v2\/posts\/785\/revisions\/813"}],"wp:attachment":[{"href":"https:\/\/alandmoore.com\/blog\/wp-json\/wp\/v2\/media?parent=785"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alandmoore.com\/blog\/wp-json\/wp\/v2\/categories?post=785"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alandmoore.com\/blog\/wp-json\/wp\/v2\/tags?post=785"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}