{"id":5319,"date":"2026-01-24T11:54:18","date_gmt":"2026-01-24T17:54:18","guid":{"rendered":"https:\/\/neosmart.net\/blog\/?p=5319"},"modified":"2026-01-24T14:56:38","modified_gmt":"2026-01-24T20:56:38","slug":"how-to-swap-the-contents-of-two-files-opened-in-neovim-buffers","status":"publish","type":"post","link":"https:\/\/neosmart.net\/blog\/how-to-swap-the-contents-of-two-files-opened-in-neovim-buffers\/","title":{"rendered":"How to (safely) swap the contents of two files opened in (neo)vim buffers"},"content":{"rendered":"<p><a href=\"https:\/\/neosmart.net\/blog\/wp-content\/uploads\/2026\/01\/neovim.svg\" rel=\"follow\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-5322 alignright colorbox-5319\" src=\"https:\/\/neosmart.net\/blog\/wp-content\/uploads\/2026\/01\/neovim.svg\" alt=\"\" width=\"114\" height=\"137\" \/><\/a>Raise your hand if you&#8217;ve been here before: you have <code>file1<\/code> open in a vim or neovim buffer and you want to &#8220;fork&#8221; its contents over to <code>file2<\/code>, but you need to reference <code>file1<\/code> while you do so. So you do the obvious: you open a split buffer with <code>:sp<\/code> or <code>:vsp<\/code>, run a quick <code>:saveas file2<\/code> then hack away at the file to make the changes you want followed by <code>:w<\/code> (or whatever shortcut you have mapped to the same) and call it a day\u2026 only to realize that you were in the wrong split and that you&#8217;ve accidentally switched <code>file1<\/code> and <code>file2<\/code> around?<\/p>\n<p><!--more--><\/p>\n<p>You <i>could<\/i> just do a quick <code>:saveas! file1<\/code> and <code>:saveas! file2<\/code> in the respective buffers to set things right, <em>but of course<\/em> you have <code>autoread<\/code> enabled, so correcting the file in one buffer will cause the other copy in the second buffer to be immediately lost!<sup id=\"rf1-5319\"><a href=\"#fn1-5319\" title=\"Yes, of course you can undo the autoread and then :saveas again, but do you really want to be taking that risk with your hours of work?\" rel=\"footnote\">1<\/a><\/sup><\/p>\n<p><a href=\"https:\/\/neosmart.net\/blog\/wp-content\/uploads\/2026\/01\/vim-help-0f.webp\" rel=\"follow\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-5327 size-full colorbox-5319\" src=\"https:\/\/neosmart.net\/blog\/wp-content\/uploads\/2026\/01\/vim-help-0f.webp\" alt=\" :0file :0f[ile][!] Remove the name of the current buffer. The optional ! avoids truncating the message, as with :file. :buffers :files :ls List all the currently known file names. See windows.txt :files :buffers :ls.\" width=\"846\" height=\"234\" \/><\/a><\/p>\n<p>The solution is a quick <code>:0f<\/code> away: it&#8217;s a shortcut for <code>:filename \"\"<\/code> that clears the association between the buffer and the underlying file, leaving the contents in the buffer unchanged but severing the link with the underlying inode, meaning after you run <code>:0f<\/code> in one buffer, you can swap back to the other and save it to the correct path without risking the other buffer&#8217;s contents from being lost, as it&#8217;s been dissociated from the underlying file. Then you can go back to the now-unnamed buffer and save it to correct path too, and all will be well.<\/p>\n<hr class=\"footnotes\"><ol class=\"footnotes\"><li id=\"fn1-5319\"><p>Yes, of course you can undo the <code>autoread<\/code> and then <code>:saveas<\/code> again, but do you really want to be taking that risk with your hours of work?&nbsp;<a href=\"#rf1-5319\" class=\"backlink\" title=\"Jump back to footnote 1 in the text.\">&#8617;<\/a><\/p><\/li><\/ol>","protected":false},"excerpt":{"rendered":"<p>Raise your hand if you&#8217;ve been here before: you have file1 open in a vim or neovim buffer and you want to &#8220;fork&#8221; its contents over to file2, but you need to reference file1 while you do so. So you &hellip; <a href=\"https:\/\/neosmart.net\/blog\/how-to-swap-the-contents-of-two-files-opened-in-neovim-buffers\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":505,"featured_media":5325,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[925,1],"tags":[1041,1040,1029],"class_list":["post-5319","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-random","category-software","tag-neovim","tag-tips-and-tricks","tag-vim"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/neosmart.net\/blog\/wp-content\/uploads\/2026\/01\/neovim-wide.webp","jetpack_shortlink":"https:\/\/wp.me\/p4xDa-1nN","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/posts\/5319","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/users\/505"}],"replies":[{"embeddable":true,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/comments?post=5319"}],"version-history":[{"count":7,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/posts\/5319\/revisions"}],"predecessor-version":[{"id":5332,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/posts\/5319\/revisions\/5332"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/media\/5325"}],"wp:attachment":[{"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/media?parent=5319"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/categories?post=5319"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/tags?post=5319"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}