{"id":101,"date":"2020-05-19T10:00:48","date_gmt":"2020-05-19T08:00:48","guid":{"rendered":"http:\/\/starhackademy.net\/?p=101"},"modified":"2020-09-14T13:02:48","modified_gmt":"2020-09-14T11:02:48","slug":"vim-search-replace-string-with-a-file-content","status":"publish","type":"post","link":"https:\/\/starhackademy.net\/index.php\/2020\/05\/19\/vim-search-replace-string-with-a-file-content\/","title":{"rendered":"vim search &#038; replace string with a file content."},"content":{"rendered":"\n<p>Today, a colleague of mine came with an interesting question: how can I search &amp; replace a string with the content of a file. I naturally tought to <a href=\"https:\/\/www.vim.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">vim<\/a>.<br \/>I started digging, but couldn&#8217;t find a direct way to do it. So, I decided to use a named buffer to hold the content file.<br \/>So let&#8217;s say that you are editing a file A (current buffer) and want to replace all occurences of <em>MyOldString<\/em> with the content of <em>MyOtherFile.txt<\/em>.<br \/>Here is the way to do it:<br \/>While editing your file, type the following commands:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">:edit MyOtherFile.txt\ngg\"ayG\n:bdelete\n:%s\/MyOldString\/\\=@a\/g<\/pre>\n\n\n\n<p> Some explanation:<\/p>\n\n\n\n<ul><li><code>:edit MyOtherFile.txt<\/code> This command creates a new buffer and open the file MyOtherFileFile.txt.<\/li><li><code>gg <\/code>Go to the top of the file.<\/li><li><code>\"a <\/code>Use named buffer <em><strong>a<\/strong><\/em><\/li><li><code>yG <\/code>Yank (<em><strong>y<\/strong><\/em>) all text (<em><strong>G<\/strong><\/em>)<\/li><li><code>:bdelete <\/code>delete the file buffer (<em>MyOtherFile.txt<\/em>) and go back to the original file<\/li><li><code>:%s\/MyOldString\/\\=@a\/g <\/code>search the whole file (<em><strong>:%s<\/strong><\/em>) for the string <em>MyOldString<\/em> and replace it with the content of named buffer (<strong>\\<em>=@a<\/em><\/strong>) for all occurences (<em><strong>g<\/strong><\/em>).<\/li><\/ul>\n\n\n\n<p>This is quite usefull in some case. This could be transformed in a named macro&#8230;<\/p>\n\n\n\n<p>Any other method to vim search &amp; replace string with a file content (or without viw) is welcome.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Search &#038; replace a string with a file content in vim (vi)<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","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,"footnotes":"","_jetpack_memberships_contains_paid_content":false,"jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[23],"tags":[22,26,21,20,19,18,25,16,17],"jetpack_publicize_connections":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>vim search &amp; replace string with a file content. - Star Hackademy<\/title>\n<meta name=\"description\" content=\"Search &amp; replace a string with a file content in vim (vi)\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/starhackademy.net\/index.php\/2020\/05\/19\/vim-search-replace-string-with-a-file-content\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"vim search &amp; replace string with a file content. - Star Hackademy\" \/>\n<meta property=\"og:description\" content=\"Search &amp; replace a string with a file content in vim (vi)\" \/>\n<meta property=\"og:url\" content=\"https:\/\/starhackademy.net\/index.php\/2020\/05\/19\/vim-search-replace-string-with-a-file-content\/\" \/>\n<meta property=\"og:site_name\" content=\"Star Hackademy\" \/>\n<meta property=\"article:published_time\" content=\"2020-05-19T08:00:48+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-09-14T11:02:48+00:00\" \/>\n<meta name=\"author\" content=\"pfuks\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"pfuks\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/starhackademy.net\/index.php\/2020\/05\/19\/vim-search-replace-string-with-a-file-content\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/starhackademy.net\/index.php\/2020\/05\/19\/vim-search-replace-string-with-a-file-content\/\"},\"author\":{\"name\":\"pfuks\",\"@id\":\"https:\/\/starhackademy.net\/#\/schema\/person\/0ea9aadbf70dfd29540089c28eaceca3\"},\"headline\":\"vim search &#038; replace string with a file content.\",\"datePublished\":\"2020-05-19T08:00:48+00:00\",\"dateModified\":\"2020-09-14T11:02:48+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/starhackademy.net\/index.php\/2020\/05\/19\/vim-search-replace-string-with-a-file-content\/\"},\"wordCount\":204,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/starhackademy.net\/#\/schema\/person\/0ea9aadbf70dfd29540089c28eaceca3\"},\"keywords\":[\"file\",\"file content\",\"insert\",\"insert file\",\"replace\",\"search\",\"search&amp;replace\",\"vi\",\"vim\"],\"articleSection\":[\"DevOps\"],\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/starhackademy.net\/index.php\/2020\/05\/19\/vim-search-replace-string-with-a-file-content\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/starhackademy.net\/index.php\/2020\/05\/19\/vim-search-replace-string-with-a-file-content\/\",\"url\":\"https:\/\/starhackademy.net\/index.php\/2020\/05\/19\/vim-search-replace-string-with-a-file-content\/\",\"name\":\"vim search & replace string with a file content. - Star Hackademy\",\"isPartOf\":{\"@id\":\"https:\/\/starhackademy.net\/#website\"},\"datePublished\":\"2020-05-19T08:00:48+00:00\",\"dateModified\":\"2020-09-14T11:02:48+00:00\",\"description\":\"Search & replace a string with a file content in vim (vi)\",\"breadcrumb\":{\"@id\":\"https:\/\/starhackademy.net\/index.php\/2020\/05\/19\/vim-search-replace-string-with-a-file-content\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/starhackademy.net\/index.php\/2020\/05\/19\/vim-search-replace-string-with-a-file-content\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/starhackademy.net\/index.php\/2020\/05\/19\/vim-search-replace-string-with-a-file-content\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/starhackademy.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"vim search &#038; replace string with a file content.\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/starhackademy.net\/#website\",\"url\":\"https:\/\/starhackademy.net\/\",\"name\":\"Star Hackademy\",\"description\":\"Hacking and configuring, IT&#039;s a long and secret story...\",\"publisher\":{\"@id\":\"https:\/\/starhackademy.net\/#\/schema\/person\/0ea9aadbf70dfd29540089c28eaceca3\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/starhackademy.net\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-GB\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/starhackademy.net\/#\/schema\/person\/0ea9aadbf70dfd29540089c28eaceca3\",\"name\":\"pfuks\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\/\/starhackademy.net\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/starhackademy.net\/wp-content\/uploads\/2020\/06\/caricature-small.png\",\"contentUrl\":\"https:\/\/starhackademy.net\/wp-content\/uploads\/2020\/06\/caricature-small.png\",\"width\":640,\"height\":676,\"caption\":\"pfuks\"},\"logo\":{\"@id\":\"https:\/\/starhackademy.net\/#\/schema\/person\/image\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"vim search & replace string with a file content. - Star Hackademy","description":"Search & replace a string with a file content in vim (vi)","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/starhackademy.net\/index.php\/2020\/05\/19\/vim-search-replace-string-with-a-file-content\/","og_locale":"en_GB","og_type":"article","og_title":"vim search & replace string with a file content. - Star Hackademy","og_description":"Search & replace a string with a file content in vim (vi)","og_url":"https:\/\/starhackademy.net\/index.php\/2020\/05\/19\/vim-search-replace-string-with-a-file-content\/","og_site_name":"Star Hackademy","article_published_time":"2020-05-19T08:00:48+00:00","article_modified_time":"2020-09-14T11:02:48+00:00","author":"pfuks","twitter_card":"summary_large_image","twitter_misc":{"Written by":"pfuks","Estimated reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/starhackademy.net\/index.php\/2020\/05\/19\/vim-search-replace-string-with-a-file-content\/#article","isPartOf":{"@id":"https:\/\/starhackademy.net\/index.php\/2020\/05\/19\/vim-search-replace-string-with-a-file-content\/"},"author":{"name":"pfuks","@id":"https:\/\/starhackademy.net\/#\/schema\/person\/0ea9aadbf70dfd29540089c28eaceca3"},"headline":"vim search &#038; replace string with a file content.","datePublished":"2020-05-19T08:00:48+00:00","dateModified":"2020-09-14T11:02:48+00:00","mainEntityOfPage":{"@id":"https:\/\/starhackademy.net\/index.php\/2020\/05\/19\/vim-search-replace-string-with-a-file-content\/"},"wordCount":204,"commentCount":0,"publisher":{"@id":"https:\/\/starhackademy.net\/#\/schema\/person\/0ea9aadbf70dfd29540089c28eaceca3"},"keywords":["file","file content","insert","insert file","replace","search","search&amp;replace","vi","vim"],"articleSection":["DevOps"],"inLanguage":"en-GB","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/starhackademy.net\/index.php\/2020\/05\/19\/vim-search-replace-string-with-a-file-content\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/starhackademy.net\/index.php\/2020\/05\/19\/vim-search-replace-string-with-a-file-content\/","url":"https:\/\/starhackademy.net\/index.php\/2020\/05\/19\/vim-search-replace-string-with-a-file-content\/","name":"vim search & replace string with a file content. - Star Hackademy","isPartOf":{"@id":"https:\/\/starhackademy.net\/#website"},"datePublished":"2020-05-19T08:00:48+00:00","dateModified":"2020-09-14T11:02:48+00:00","description":"Search & replace a string with a file content in vim (vi)","breadcrumb":{"@id":"https:\/\/starhackademy.net\/index.php\/2020\/05\/19\/vim-search-replace-string-with-a-file-content\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/starhackademy.net\/index.php\/2020\/05\/19\/vim-search-replace-string-with-a-file-content\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/starhackademy.net\/index.php\/2020\/05\/19\/vim-search-replace-string-with-a-file-content\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/starhackademy.net\/"},{"@type":"ListItem","position":2,"name":"vim search &#038; replace string with a file content."}]},{"@type":"WebSite","@id":"https:\/\/starhackademy.net\/#website","url":"https:\/\/starhackademy.net\/","name":"Star Hackademy","description":"Hacking and configuring, IT&#039;s a long and secret story...","publisher":{"@id":"https:\/\/starhackademy.net\/#\/schema\/person\/0ea9aadbf70dfd29540089c28eaceca3"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/starhackademy.net\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-GB"},{"@type":["Person","Organization"],"@id":"https:\/\/starhackademy.net\/#\/schema\/person\/0ea9aadbf70dfd29540089c28eaceca3","name":"pfuks","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/starhackademy.net\/#\/schema\/person\/image\/","url":"https:\/\/starhackademy.net\/wp-content\/uploads\/2020\/06\/caricature-small.png","contentUrl":"https:\/\/starhackademy.net\/wp-content\/uploads\/2020\/06\/caricature-small.png","width":640,"height":676,"caption":"pfuks"},"logo":{"@id":"https:\/\/starhackademy.net\/#\/schema\/person\/image\/"}}]}},"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p32Xjl-1D","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/starhackademy.net\/index.php\/wp-json\/wp\/v2\/posts\/101"}],"collection":[{"href":"https:\/\/starhackademy.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/starhackademy.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/starhackademy.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/starhackademy.net\/index.php\/wp-json\/wp\/v2\/comments?post=101"}],"version-history":[{"count":7,"href":"https:\/\/starhackademy.net\/index.php\/wp-json\/wp\/v2\/posts\/101\/revisions"}],"predecessor-version":[{"id":140,"href":"https:\/\/starhackademy.net\/index.php\/wp-json\/wp\/v2\/posts\/101\/revisions\/140"}],"wp:attachment":[{"href":"https:\/\/starhackademy.net\/index.php\/wp-json\/wp\/v2\/media?parent=101"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/starhackademy.net\/index.php\/wp-json\/wp\/v2\/categories?post=101"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/starhackademy.net\/index.php\/wp-json\/wp\/v2\/tags?post=101"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}