{"id":741,"date":"2015-07-21T14:38:33","date_gmt":"2015-07-21T20:38:33","guid":{"rendered":"http:\/\/islascruz.org\/blog\/?p=741"},"modified":"2016-11-29T17:15:41","modified_gmt":"2016-11-29T23:15:41","slug":"search-and-replace-in-multiple-lines-in-vim","status":"publish","type":"post","link":"https:\/\/islascruz.org\/blog\/2015\/07\/21\/search-and-replace-in-multiple-lines-in-vim\/","title":{"rendered":"Search and replace in multiple lines in VIM"},"content":{"rendered":"<p><a href=\"http:\/\/islascruz.org\/blog\/wp-content\/uploads\/2014\/11\/Vim_logo.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignright wp-image-119\" src=\"http:\/\/islascruz.org\/blog\/wp-content\/uploads\/2014\/11\/Vim_logo.png\" alt=\"Vim_logo\" width=\"250\" height=\"250\" \/><\/a>So.. I had this issue where I have to replace stuff in the code where the text is splitted across several lines, this is basically because the string is too long and we usually keep the 80 characters width in the code (<a href=\"https:\/\/www.python.org\/dev\/peps\/pep-0008\/\" target=\"_blank\" rel=\"noopener\">PEP-8<\/a>) but this also works in HTML where for example, comments are splitted across several lines.<\/p>\n<p>Let&#8217;s say that I want to remove the call to a function that process a text, the most common function that process text is the one everyone uses for translations: <code>_()<\/code>. The easy way is just to try a simple search and replace in vim:<\/p>\n<pre class=\"lang:python decode:1 \" >\n:%s\/_(\\(.\\{-}\\))\/\\1\/gc\n<\/pre>\n<p>Which will replace search for something like <code>_(\"Translate this\")<\/code> and will leave it like this: <code>\"Translate this\"<\/code>. \u00a0the <code>:%s<\/code> \u00a0part indicates that we want to search across the whole file, <code>\/<\/code> is the delimiter, there are three delimiters, the pattern, the replacement and operation flags. Since we want\u00a0to match <code>_(.*)<\/code> but we want to preserve what&#8217;s inside the parenthesis we need to create a backreference and we need to surround that\u00a0between\u00a0<code>\\(<\/code> and <code>\\)<\/code>.\u00a0The dot <code>.<\/code> matches every character except new lines. Please note that we use <code>\\{-}<\/code> instead of the most common <code>*<\/code> (asterisk) for &#8220;one or more&#8221; occurrences. \u00a0This is because <code>\\{-}<\/code> will\u00a0match as few as possible, while <code>*<\/code> is too greedy and will stop at the last occurrence. Then we set the replacement string, we wanted to leave the string intact so we just use the backreference: <code>\\1<\/code> is the first backreference, <code>\\2<\/code> for the second and so on. The last <code>gc<\/code> are there just to ask you for every match, but you can just press &#8220;a&#8221; to apply to everyone.<\/p>\n<p>If you want to search\/replace multiple lines, then you can just use the <code>\\_.<\/code>\u00a0pattern in vim, that pattern matches every character including new lines.<\/p>\n<pre class=\"lang:python decode:1 \" >\n:%s\/_(\\(\\_.\\{-}\\))\/\\1\/gc\n<\/pre>\n<p>&nbsp;<\/p>\n<p>So, for example:<\/p>\n<pre class=\"lang:python decode:1 \" >\n_(&quot;This is a text&quot;\n&quot; That is splitted across&quot;\n&quot; several lines&quot;).upper()\n<\/pre>\n<p>after <code>:%s\/_(\\(\\_.\\{-}\\))\/\\1\/gc<\/code> will be<\/p>\n<pre class=\"lang:python decode:1 \" >\n&quot;This is a text&quot;\n&quot; That is splitted across&quot;\n&quot; several lines&quot;.upper()\n<\/pre>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_741\" class=\"pvc_stats all  \" data-element-id=\"741\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/islascruz.org\/blog\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>So.. I had this issue where I have to replace stuff in the code where the text is splitted across several lines, this is basically because the string is too long and we usually keep the 80 characters width in the code (PEP-8) but this also works in HTML where for example, comments are splitted [&hellip;]<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_741\" class=\"pvc_stats all  \" data-element-id=\"741\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/islascruz.org\/blog\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":119,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[33],"class_list":["post-741","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programming","tag-vim"],"brizy_media":[],"_links":{"self":[{"href":"https:\/\/islascruz.org\/blog\/wp-json\/wp\/v2\/posts\/741","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/islascruz.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/islascruz.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/islascruz.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/islascruz.org\/blog\/wp-json\/wp\/v2\/comments?post=741"}],"version-history":[{"count":12,"href":"https:\/\/islascruz.org\/blog\/wp-json\/wp\/v2\/posts\/741\/revisions"}],"predecessor-version":[{"id":3133,"href":"https:\/\/islascruz.org\/blog\/wp-json\/wp\/v2\/posts\/741\/revisions\/3133"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/islascruz.org\/blog\/wp-json\/wp\/v2\/media\/119"}],"wp:attachment":[{"href":"https:\/\/islascruz.org\/blog\/wp-json\/wp\/v2\/media?parent=741"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/islascruz.org\/blog\/wp-json\/wp\/v2\/categories?post=741"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/islascruz.org\/blog\/wp-json\/wp\/v2\/tags?post=741"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}