4 levels of vim editor commands
Tags: beginners, vim, linux, tutorial
Published: 2019-10-13 13:52:23

<!-- wp:paragraph -->
<p>This article&nbsp;will not make you an expert in very less time and&nbsp;learning vim editor commands will be painful at first. So, It will take time and It will be a lot like playing a musical instrument. Also, don't expect to be more efficient with vim than any another editor in less than 3 days. In fact, it will certainly take weeks instead of 3 days. Vim editor commands are hard to learn but incredible to use. </p>
<!-- /wp:paragraph -->

> /!\\\: Originally published @ [www.vishalchovatiya.com](http://www.vishalchovatiya.com).

<!-- wp:heading -->
<h2>Brief on learning vim editor commands</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>I have designed this article&nbsp;in different four levels, so you won't lose enthusiasm &amp; step up slowly and I suggest you start slow with level 1.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong><code>Level 1</code>: </strong>Beginner<br><strong><code>Level 2</code>: </strong>Feel Comfortable<br><strong><code>Level 3</code>: </strong>Better, Stronger, Faster<br><strong><code>Level 4</code>: </strong>Feel the power in your fingers</p>
<!-- /wp:paragraph -->


<!-- wp:heading -->
<h2><strong>Level 1: Beginner</strong></h2>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li>Open vim editor by typing vim filename (i.e. <code>vim main.c</code>).</li><li>In a standard editor, typing on the keyboard is enough to write something and see it on the screen, but not this time. Because vim is in normal mode. </li><li>To switch to insert mode, you need to type the letter <code>i</code>. As a result, you can type letters like in a standard editor. </li><li>After that, to get back to normal mode just press the <code style="font-size: 16px;">ESC</code> key.</li><li>By above all, you know how to switch between insert and normal mode. And, here are the commands that you need in order to survive in normal mode:</li></ul>
<!-- /wp:list -->

<!-- wp:table {"className":"is-style-stripes"} -->
<table class="wp-block-table is-style-stripes"><tbody><tr><td><strong>Command</strong></td><td><strong>Detail</strong></td></tr><tr><td><code>p</code></td><td> Paste </td></tr><tr><td><code>l</code></td><td> Insert mode. Type <code>ESC</code> to return to Normal mode. </td></tr><tr><td> <code>hjkl</code> </td><td> cursor move (<code>←↓↑</code><code>→</code>). Hint: j looks like a down arrow. </td></tr><tr><td> <code>dd</code> </td><td> (and copy) the current line </td></tr><tr><td> <code>:wq</code></td><td> Save and Quit (<code>:w</code> save, <code>:q</code> quit) </td></tr><tr><td><code>:help</code> </td><td> Show help about.</td></tr></tbody></table>
<!-- /wp:table -->

<!-- wp:heading -->
<h2><strong>Level 2: Feel comfortable&nbsp;</strong></h2>
<!-- /wp:heading -->

<!-- wp:heading {"level":3} -->
<h3><strong>Basic Moves</strong></h3>
<!-- /wp:heading -->

<!-- wp:table {"className":"is-style-stripes"} -->
<table class="wp-block-table is-style-stripes"><tbody><tr><td> <strong>Command </strong></td><td> <strong>Detail </strong></td></tr><tr><td><code>o</code>                                                     </td><td> Go to the first column                                </td></tr><tr><td> <code>$</code> </td><td> Go to the end of line </td></tr><tr><td><code>:/pattern </code></td><td> Search for a pattern in the current file  </td></tr><tr><td><code>gg</code></td><td> Go to the start of the file </td></tr><tr><td> <code>G</code>                                                  </td><td> Go to the last line </td></tr></tbody></table>
<!-- /wp:table -->

<!-- wp:heading {"level":3} -->
<h3><strong>Load / Save / Quit file</strong></h3>
<!-- /wp:heading -->

<!-- wp:table {"className":"is-style-stripes"} -->
<table class="wp-block-table is-style-stripes"><tbody><tr><td><strong>Command </strong></td><td> <strong>Detail  </strong></td></tr><tr><td><code>:w</code> </td><td> Save current file </td></tr><tr><td><code>:saveas</code></td><td> Save to </td></tr><tr><td><code>:x</code>, <code>ZZ</code> or <code>:wq</code></td><td>Save and quit (<code>:x</code> only save if necessary) </td></tr><tr><td><code>:q!</code></td><td>Quit without saving, also<code>:qa!</code> to quit even if there are modified hidden buffers </td></tr></tbody></table>
<!-- /wp:table -->

<!-- wp:heading {"level":3} -->
<h3><strong>Undo / Redo</strong></h3>
<!-- /wp:heading -->

<!-- wp:table {"className":"is-style-stripes"} -->
<table class="wp-block-table is-style-stripes"><tbody><tr><td> <strong>Command  </strong></td><td> <strong>Detail</strong>   </td></tr><tr><td> <code>u</code></td><td> Undo </td></tr><tr><td> <code>Ctrl+r</code> </td><td> Redo  </td></tr></tbody></table>
<!-- /wp:table -->

<!-- wp:heading {"level":3} -->
<h3><strong>Copy / Paste</strong></h3>
<!-- /wp:heading -->

<!-- wp:table {"className":"is-style-stripes"} -->
<table class="wp-block-table is-style-stripes"><tbody><tr><td> <strong>Command   </strong></td><td> <strong>Detail    </strong></td></tr><tr><td> <code>p</code></td><td>paste before, remember p is paste after the current position.</td></tr><tr><td> <code>yy</code>  </td><td>copy the current line, easier but equivalent to ddP </td></tr><tr><td><code>v</code></td><td>Start visual selection from the current cursor position (Mostly used to copy multiple lines by select area press <code>yy</code> and then paste by pressing <code>p</code>)  </td></tr></tbody></table>
<!-- /wp:table -->

<!-- wp:heading -->
<h2><strong>Level 3: Better, Stronger, Faster</strong></h2>
<!-- /wp:heading -->

<!-- wp:heading {"level":3} -->
<h3><strong>Put the line numbers in each line (Better)</strong></h3>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li><code>:set number</code></li></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":3} -->
<h3><strong>Copy/Paste multiple lines (Stronger)</strong></h3>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li>Firstly, go to address where you want to start copy. </li><li>Then, go to normal mode by pressing ESC and then v (visual selection).</li><li>After that, Go up and down until copy area complete.</li><li>Then, press yy which is a copy(yank) the selected area(press y key twice).</li><li>After that, Go with the cursor where you want to paste copied data and press p.</li></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":3} -->
<h3><strong>Vim find and replace</strong></h3>
<!-- /wp:heading -->

<!-- wp:enlighter/codeblock {"linenumbers":"false"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""> :%s/text to search for/text to replace/g</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:paragraph -->
<p>Search text occurrences in a file and replace all with the text given.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3><strong>Auto detecting/completing words (Faster)</strong></h3>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li>In Insert mode, just type the start of a word, then type <code>Ctrl+p</code>, just see the magic…</li></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":3} -->
<h3><strong>Use mouse in vim</strong></h3>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li><code>:set mouse=a</code></li></ul>
<!-- /wp:list -->

<!-- wp:heading -->
<h2><strong>Level 4: Feel the power in your fingers</strong></h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>This level will take you to a high of vim. When you master all following vim editor commands, you will be able to increase your productivity and I am sure you would never use any other editor than Vim.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3><strong>Open the file and go to a specific function or line number</strong></h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>1. Go to a specific line number, vim fileName +LineNumber i.e. <code>vim main.c +3</code><br>2. Go to a specific function, vim filename +/pattern i.e. <code>vim main.c +/main</code><br>3. Go to a specific line number, if your file is already opened i.e. <code>:linenumber </code><br>For example, In normal mode type ':42' and press enter, you will be at line number 42 location.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3><strong>Commenting or editing multiple lines</strong> in a <strong>single stroke</strong></h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Firstly, Go to Normal mode by pressing ESC key.<br>Then, Press Ctrl+v (visual block selection) from where you want to edit.<br>After that, Move cursor up and down to a number of the line you want to edit and press I//.<br>Finally, Press ESC again and wait for a second it will comment all the selected line or you can say add // in front of each selected line.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Typically: <code>ESC</code>, <code>Ctrl+v</code>, <code>↓</code>&nbsp;or <code>↑</code>&nbsp;, <code>I</code>, &lt;PatternToAdd&gt;, <code>ESC</code></p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3><strong>Align code in curly braces (Indent)</strong></h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Firstly, Go inside the curly braces in which you want to align code properly.<br>Then, Go to Normal mode by pressing ESC then press =.  <br>After that,  press i.<br>Finally, To see the magic press either { or }.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Typically: <code>ESC</code>, <code>=</code>, <code>i</code>, <code>{</code></p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3><strong>Working with multiple files in a single screen</strong></h3>
<!-- /wp:heading -->

<!-- wp:table {"className":"is-style-stripes"} -->
<table class="wp-block-table is-style-stripes"><tbody><tr><td><strong>Command</strong></td><td><strong>Detail</strong></td></tr><tr><td><code>:vs filename</code> </td><td> Split window vertically </td></tr><tr><td><code>:sp filename</code></td><td> Split window horizontally </td></tr></tbody></table>
<!-- /wp:table -->

<!-- wp:paragraph -->
<p>Note:- use <code>Ctrl+w</code> to shuffle between two opened section in normal mode.</p>
<!-- /wp:paragraph -->


<!-- wp:heading -->
<h2>Bonus point</h2>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li><a rel="noreferrer noopener" aria-label="https://vim-adventures.com/  (opens in a new tab)" href="https://vim-adventures.com/ " target="_blank"><span style="color:#2a10f2" class="color">https://vim-adventures.com/ </span></a></li></ul>
<!-- /wp:list -->