{"componentChunkName":"component---src-pages-markdown-remark-fields-slug-js","path":"/engineering/guest-post/vim-level-up/","result":{"data":{"markdownRemark":{"id":"cb7b9df3-1aca-5e57-8d28-8bd2cafd9bcc","excerpt":"Prerequisites You should know the basics of Vim beforehand. You may refer to a previous blog Vim: Getting started. This tutorial explores the following concepts…","html":"<h2 id=\"prerequisites\" style=\"position:relative;\"><a href=\"#prerequisites\" aria-label=\"prerequisites permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Prerequisites</h2>\n<p>You should know the basics of Vim beforehand. You may refer to a previous blog <a href=\"https://www.loginradius.com/blog/engineering/vim-getting-started/\">Vim: Getting started</a>.</p>\n<p>This tutorial explores the following concepts:</p>\n<ul>\n<li>Macros</li>\n<li>Buffers and splitting</li>\n<li>Global  command</li>\n</ul>\n<p>In addition, this tutorial explores a few other tips and tricks. It is not uncommon for folks who have been using Vim for a long time and not being familiar with these handy, useful features.</p>\n<h2 id=\"macros\" style=\"position:relative;\"><a href=\"#macros\" aria-label=\"macros permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Macros</h2>\n<p>You don't want to miss out on macros if you want to level up your Vim game. Vim macro is a powerful feature that allows you to record a sequence of keys on the fly. It helps you to automate a lot of stuff. </p>\n<p>Macros are based on the concept of registers. If you don't know what registers are, you can think of them as a bunch of spaces in memory that Vim uses to store some text.</p>\n<p>Macros are editable registers, which you can record, store, and edit whenever you want.</p>\n<p>Here's the basic syntax of  macros:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">q&lt;key you want to record your macro into&gt;&lt;series of commands&gt;q</span></code></pre>\n<p>Example:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">q1f;xq</span></code></pre>\n<p>Let's break it down for you:</p>\n<p>q : Starts recording a macro<br>\n1 : The recorded macro will be stored on key 1<br>\nf;x : Find the first occurrence of <code>;</code>in that line and delete <code>;</code><br>\nq : stop recording the macro  </p>\n<p>That's it. Now, you can use this macro by <code>@&#x3C;your key></code> i.e <code>@1</code></p>\n<p>If you want to repeat a macro <em>n</em> number of times, then <code>n@1</code>is the syntax.</p>\n<h3 id=\"use-case\" style=\"position:relative;\"><a href=\"#use-case\" aria-label=\"use case permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Use Case</h3>\n<p>Not impressed yet? Huhh! Let's show you a practical use case to demonstrate the power of a macro. I work in ReactJs a lot and like to export all my components in one file. Let's try exporting 99 HeroBlocks in one go.</p>\n<p>The desired result is:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">export {default as HeroBlock1 } from &#39;./HeroBlock1/HeroBlock1.js&#39;</span>\n<span class=\"grvsc-line\">export {default as HeroBlock2 } from &#39;./HeroBlock1/HeroBlock2.js&#39;</span>\n<span class=\"grvsc-line\">export {default as HeroBlock3 } from &#39;./HeroBlock1/HeroBlock3.js&#39;</span>\n<span class=\"grvsc-line\">...</span>\n<span class=\"grvsc-line\">..</span>\n<span class=\"grvsc-line\">so on</span></code></pre>\n<p>Perform <code>vim index.js</code></p>\n<p>And, write this line:</p>\n<p><code>export {default as HeroBlock1 } from './HeroBlock1/HeroBlock1.js'</code></p>\n<p>Now, follow along:</p>\n<p><code>q1yyp4E ^A6e ^Aq</code></p>\n<p>Breaking it down:</p>\n<p>q1 : Start recording a macro in key 1.<br>\nyy : yank the current line.<br>\np : paste the yanked line.<br>\n4E : Get the cursor to 1 of Heroblock1. (<em><code>f1</code> might be tempting but it will give you wrong results as not all lines will have 1, they can have any number. Try making the macro very generalized.</em>)<br>\n^A : this is <code>ctrl + a</code>. It increments the current version by one.<br>\n6e : Get the cursor to the second number.<br>\n^A : increment the number by one.<br>\nq : stop recording.  </p>\n<p>Now, to repeat your macro 99 times:</p>\n<p><code>99@1</code></p>\n<p>And that's all.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 768px; \"\n    >\n      <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 53.38461538461539%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAIAAADwazoUAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABvUlEQVQoz22Qa5OaMBSGIzDtOnWXWkAIIhCEXJA1ARFUZMHVma792P//Yxppa68zz5w5+fDM++aAyj52+NKmXw70WrhNy9vz9rWKdrFeoseNNxaLyQa+y2wFmxKV+JMcm3tsNf60BgW8HOLPDbm27Fp7Tceb1+KlDCo05ssxR++fw4e1p6X2iBgD7sM6fCqRvnU/FCC1zzm6bMLTIT6XsD6uqi6rC5gjhcYai7U00dJQpXOFWINsjoilUHMAEP3AF73w+zzoM2N7TKs+q7dQJIo02VJlREvl4isUjshskI2fAPxYc6/jfr9e9Ktp2bLqdJNzKUszVKicd9n5W57UwutksvTTT2UzyBso0OhWO1FZorFIZQuFOkOy9bucDMnC72R5Nt3sWPWS1cIRIaBYY3joHCjU/TPzP8lsWkr5mO3WDncBlndCKpWF5yNi/2P++LPw+lvyIO/Tqs3qzOYzgB1A7swAMb4jK9yvHY0LPu/EoufeiX4s96xqWF2yEvvPyCUhJAjSX7jEHmMdRDpATyACYn5dWjkyODJEoK9ie5U4WQyzcLYKZ2lgstBKgwH5jN11aOYMvuXzr1Xw9g0ZnFaA2Q5PqwAAAABJRU5ErkJggg=='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"macro demo\"\n        title=\"macro demo\"\n        src=\"/static/3fc1ce475f85e20f3e8953cea9830341/e5715/vim-macro-demo.png\"\n        srcset=\"/static/3fc1ce475f85e20f3e8953cea9830341/a6d36/vim-macro-demo.png 650w,\n/static/3fc1ce475f85e20f3e8953cea9830341/e5715/vim-macro-demo.png 768w,\n/static/3fc1ce475f85e20f3e8953cea9830341/26c3a/vim-macro-demo.png 1907w\"\n        sizes=\"(max-width: 768px) 100vw, 768px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n      />\n    </span></p>\n<h2 id=\"buffers--splitting\" style=\"position:relative;\"><a href=\"#buffers--splitting\" aria-label=\"buffers  splitting permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Buffers &#x26; Splitting</h2>\n<p>In simple terms, a <em>buffer</em> is a file that has been loaded into memory. Everything you do in Vim is in a buffer. You can list all the buffers by <code>:buffers</code>. And then, if you want to check out a particular buffer, use <code>:b n</code> -- where n is the buffer number. </p>\n<p>If you want to delete a buffer, use <code>:bd n</code> is the command.</p>\n<p>To create a new buffer in the same view -- creating a split in the current window -- use <code>:sp filename</code>. </p>\n<p>For a vertical split, use <code>:vsp filename</code>.</p>\n<blockquote>\n<p>To be honest, using buffer numbers to switch buffers is not what I prefer to do. I prefer to switch based on what I see, like <code>ctrl-w</code> then <code>j</code>to focus on a buffer below if I'm splitting the window. <code>:tabprevious</code> to move a tab on the left if I'm using tabs.</p>\n</blockquote>\n<p>Here are some key bindings you might want to use for a smooth workflow.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"3\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">nnoremap &lt;C-t&gt; :tabnew&lt;CR&gt;</span>\n<span class=\"grvsc-line\">nnoremap &lt;C-Left&gt; :tabprevious&lt;CR&gt;</span>\n<span class=\"grvsc-line\">nnoremap &lt;C-Right&gt; :tabnext&lt;CR&gt;</span>\n<span class=\"grvsc-line\">nnoremap &lt;C-J&gt;  &lt;C-W&gt;&lt;C-J&gt;</span>\n<span class=\"grvsc-line\">nnoremap &lt;C-K&gt;  &lt;C-W&gt;&lt;C-K&gt;</span>\n<span class=\"grvsc-line\">nnoremap &lt;C-L&gt;  &lt;C-W&gt;&lt;C-L&gt;</span>\n<span class=\"grvsc-line\">nnoremap &lt;C-H&gt;  &lt;C-W&gt;&lt;C-H&gt;</span></code></pre>\n<h2 id=\"global-command\" style=\"position:relative;\"><a href=\"#global-command\" aria-label=\"global command permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Global Command</h2>\n<p>The global command in Vim is very powerful and will solve a lot of your problems quickly.</p>\n<p>The basic syntax goes something as follows:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"4\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">:[range]global/{pattern}/{command}</span></code></pre>\n<p>Let's go through some use cases to learn more about it.</p>\n<p>The following will delete any line containing the word console in the entire file:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"5\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">:g/console/d</span></code></pre>\n<p>The following will delete any line between line 10-20 containing the word console in it:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"6\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">:10,20g/console/d</span></code></pre>\n<p>You can even do the inverse of it by using <code>:g!</code></p>\n<p>The following will delete any line <strong>not</strong> containing the word console in it in the entire file:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"7\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">:g!/console/d</span></code></pre>\n<p>You can even combine the two.</p>\n<p>The following will delete any line containing <em>console</em> and not containing <em>hello</em> in it in the entire file.</p>\n<p>The global command can work on any regular expression, which makes it even more powerful, provided you're creative enough...</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"8\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">:g/console/g!/Hello/d</span></code></pre>\n<p>The following will delete any line starting with <code>#</code> i.e., comments in python will be deleted in one go:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"9\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">:g/^#/d</span></code></pre>\n<p>So far, we've been using d as the command, but it can be anything else like <code>m</code> for move <code>t</code> for copy etc.</p>\n<p>One of the very powerful commands is <code>norm</code>. This command allows you to do anything that would have worked in normal mode.</p>\n<p>The following will comment out all the print statements in a python code in one go.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"10\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">:g/print/norm I#</span></code></pre>\n<p>The global command can be coupled with macros using <em>norm</em>, which means you can apply a macro to certain lines matching the regex given. This was enough to blow my mind, and it made the time I spent reading about Vim worthy.</p>\n<p>Here's the syntax for using macros with global commands:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"11\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">:g/const/normal @q</span></code></pre>\n<h2 id=\"you-made-it\" style=\"position:relative;\"><a href=\"#you-made-it\" aria-label=\"you made it permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>You Made It</h2>\n<p>Congratulations! You made it this far. You now have leveled up your Vim game. All these tips and tricks will eventually be useful to you. Here's a little something for reading this far: <a href=\"https://pintovim.dev/\">Pintovim</a> You can use this tool to make your own customized Vim color scheme.</p>\n<p>Thanks for reading!</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n  .dark-default-dark {\n    background-color: #1E1E1E;\n    color: #D4D4D4;\n  }\n</style>","headings":[{"value":"Prerequisites","depth":2},{"value":"Macros","depth":2},{"value":"Use Case","depth":3},{"value":"Buffers & Splitting","depth":2},{"value":"Global Command","depth":2},{"value":"You Made It","depth":2}],"fields":{"slug":"/engineering/guest-post/vim-level-up/"},"frontmatter":{"metatitle":null,"metadescription":null,"description":"Vim is Vi's newest and most common reincarnation, which is supported on every known platform. Go through this tutorial to learn about what Vim is and how to make the most out of it.","title":"How to Upgrade Your Vim Skills","canonical":null,"date":"September 23, 2021","updated_date":null,"tags":["Vim","Text Editor","UNIX"],"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1.5037593984962405,"src":"/static/0e8a15779baa24ee0c8d1487ce2f4794/03979/Vim.png","srcSet":"/static/0e8a15779baa24ee0c8d1487ce2f4794/f5f11/Vim.png 200w,\n/static/0e8a15779baa24ee0c8d1487ce2f4794/6d133/Vim.png 400w,\n/static/0e8a15779baa24ee0c8d1487ce2f4794/03979/Vim.png 800w,\n/static/0e8a15779baa24ee0c8d1487ce2f4794/aca38/Vim.png 1200w","sizes":"(max-width: 800px) 100vw, 800px"}}},"author":{"id":"Shubhankar Khare","github":"iamshubhankarkhare","bio":"He is a passionate full stack web developer from India. He loves learning new technologies and is an open source enthusiast.","avatar":null}}}},"pageContext":{"id":"cb7b9df3-1aca-5e57-8d28-8bd2cafd9bcc","fields__slug":"/engineering/guest-post/vim-level-up/","__params":{"fields__slug":"engineering"}}},"staticQueryHashes":["1171199041","1384082988","1711371485","1753898100","2100481360","229320306","23180105","528864852"]}