<spanclass="tomenu"><ahref="#navigation">↓ Menu ↓</a></span>
<spanclass="flush"></span>
</div>
</div>
<divid="titre">
<h1>Learn Vim Progressively</h1>
</div>
<divclass="flush"></div>
<divid="afterheader"class="article">
<divclass="corps">
<div>
<imgsrc="../../../../Scratch/img/blog/Learn-Vim-Progressively/uber_leet_use_vim.jpg"alt="Über leet use vim!"/>
</div>
<divclass="intro">
<p><spanclass="sc"><abbrtitle="Too long; didn't read">tl;dr</abbr>: </span> You want to teach yourself vim (the best text editor known to human kind) in the fastest way possible. This is my way of doing it. You start by learning the minimal to survive, then you integrate all the tricks slowly.</p>
</div>
<p><ahref="http://www.vim.org">Vim</a> the Six Billion Dollar editor</p>
<blockquote>
<p>Better, Stronger, Faster.</p>
</blockquote>
<p>Learn <ahref="http://www.vim.org">vim</a> and it will be your last text editor. There isn’t any better text editor that I know of. It is hard to learn, but incredible to use.</p>
<p>I suggest you teach yourself Vim in 4 steps:</p>
<oltype="1">
<li>Survive</li>
<li>Feel comfortable</li>
<li>Feel Better, Stronger, Faster</li>
<li>Use superpowers of vim</li>
</ol>
<p>By the end of this journey, you’ll become a vim superstar.</p>
<p>But before we start, just a warning. Learning vim will be painful at first. It will take time. It will be a lot like playing a musical instrument. Don’t expect to be more efficient with vim than with another editor in less than 3 days. In fact it will certainly take 2 weeks instead of 3 days.</p>
<p>In a standard editor, typing on the keyboard is enough to write something and see it on the screen. Not this time. Vim is in <em>Normal</em> mode. Let’s go to <em>Insert</em> mode. Type the letter <code>i</code>.</p>
<p>You should feel a bit better. You can type letters like in a standard editor. To get back to <em>Normal</em> mode just press the <code>ESC</code> key.</p>
<p>You now know how to switch between <em>Insert</em> and <em>Normal</em> mode. And now, here are the commands that you need in order to survive in <em>Normal</em> mode:</p>
<blockquote>
<ul>
<li><code>i</code> → <em>Insert</em> mode. Type <code>ESC</code> to return to Normal mode.</li>
<li><code>x</code> → Delete the char under the cursor</li>
<li><code>:wq</code> → Save and Quit (<code>:w</code> save, <code>:q</code> quit)</li>
<li><code>dd</code> → Delete (and copy) the current line</li>
<li><code>p</code> → Paste</li>
</ul>
<p>Recommended:</p>
<ul>
<li><code>hjkl</code> (highly recommended but not mandatory) → basic cursor move (←↓↑→). Hint: <code>j</code> looks like a down arrow.</li>
<li><code>:help <command></code> → Show help about <code><command></code>. You can use <code>:help</code> without a <code><command></code> to get general help.</li>
</ul>
</blockquote>
<p>Only 5 commands. That is all you need to get started. Once these command start to become natural (maybe after a day or so), you should move on to level 2.</p>
<p>But first, just a little remark about <em>Normal mode</em>. In standard editors, to copy you have to use the <code>Ctrl</code> key (<code>Ctrl-c</code> generally). In fact, when you press <code>Ctrl</code>, it is as if all of your keys change meaning. Using vim in normal mode is a bit like having the editor automatically press the <code>Ctrl</code> key for you.</p>
<p>A last word about notations:</p>
<ul>
<li>instead of writing <code>Ctrl-λ</code>, I’ll write <code><C-λ></code>.</li>
<li>commands starting with <code>:</code> end with <code><enter></code>. For example, when I write <code>:q</code>, I mean <code>:q<enter></code>.</li>
<li><code>:saveas <path/to/file></code> → save to <code><path/to/file></code></li>
<li><code>:x</code>, <code>ZZ</code> or <code>:wq</code> → save and quit (<code>:x</code> only save if necessary)</li>
<li><code>:q!</code> → quit without saving, also: <code>:qa!</code> to quit even if there are modified hidden buffers.</li>
<li><code>:bn</code> (resp. <code>:bp</code>) → show next (resp. previous) file (buffer)</li>
</ul>
</blockquote></li>
</ol>
<p>Take the time to learn all of these command. Once done, you should be able to do every thing you are able to do in other editors. You may still feel a bit awkward. But follow me to the next level and you’ll see why vim is worth the extra work.</p>
<p>Congratulation for reaching this far! Now we can start with the interesting stuff. At level 3, we’ll only talk about commands which are compatible with the old vi editor.</p>
<h3id="better">Better</h3>
<p>Let’s look at how vim could help you to repeat yourself:</p>
<oltype="1">
<li><code>.</code> → (dot) will repeat the last command,</li>
<li>N<command> → will repeat the command N times.</li>
</ol>
<p>Some examples, open a file and type:</p>
<blockquote>
<ul>
<li><code>2dd</code> → will delete 2 lines</li>
<li><code>3p</code> → will paste the text 3 times</li>
<li><code>.</code> → Just after the last command will write again the 100 “desu”.</li>
<li><code>3.</code> → Will write 3 “desu” (and not 300, how clever).</li>
</ul>
</blockquote>
<h3id="stronger">Stronger</h3>
<p>Knowing how to move efficiently with vim is <em>very</em> important. Don’t skip this section.</p>
<oltype="1">
<li>N<code>G</code> → Go to line N</li>
<li><code>gg</code> → shortcut for <code>1G</code> - go to the start of the file</li>
<li><code>G</code> → Go to last line</li>
<li><p>Word moves:</p>
<blockquote>
<oltype="1">
<li><code>w</code> → go to the start of the following word,</li>
<li><code>e</code> → go to the end of this word.</li>
</ol>
<p>By default, words are composed of letters and the underscore character. Let’s call a WORD a group of letter separated by blank characters. If you want to consider WORDS, then just use uppercase characters:</p>
<oltype="1">
<li><code>W</code> → go to the start of the following WORD,</li>
<li><code>E</code> → go to the end of this WORD.</li>
<li><code>0</code> → go to the beginning of this line</li>
<li><code>y</code> → yank from here</li>
<li><code>$</code> → up to the end of this line</li>
</ul>
<p>We also can do things like <code>ye</code>, yank from here to the end of the word. But also <code>y2/foo</code> yank up to the second occurrence of “foo”.</p>
<p>But what was true for <code>y</code> (yank), is also true for <code>d</code> (delete), <code>v</code> (visual select), <code>gU</code> (uppercase), <code>gu</code> (lowercase), etc…</p>
<h2id="th-level-vim-superpowers">4th Level – Vim Superpowers</h2>
<p>With all preceding commands you should be comfortable using vim. But now, here are the killer features. Some of these features were the reason I started to use vim.</p>
<h3id="move-on-current-line-0-g_-f-f-t-t">Move on current line: <code>0</code><code>^</code><code>$</code><code>g_</code><code>f</code><code>F</code><code>t</code><code>T</code><code>,</code><code>;</code></h3>
<blockquote>
<ul>
<li><code>0</code> → go to column 0</li>
<li><code>^</code> → go to first character on the line</li>
<li><code>$</code> → go to the last column</li>
<li><code>g_</code> → go to the last character on the line</li>
<li><code>fa</code> → go to next occurrence of the letter <code>a</code> on the line. <code>,</code> (resp. <code>;</code>) will find the next (resp. previous) occurrence.</li>
<li><code>t,</code> → go to just before the character <code>,</code>.</li>
<li><code>3fa</code> → find the 3rd occurrence of <code>a</code> on this line.</li>
<li><code>F</code> and <code>T</code> → like <code>f</code> and <code>t</code> but backward.
<p>A useful tip is: <code>dt"</code> → remove everything until the <code>"</code>.</p>
<h3id="zone-selection-actionaobject-or-actioniobject">Zone selection <code><action>a<object></code> or <code><action>i<object></code></h3>
<p>These command can only be used after an operator in visual mode. But they are very powerful. Their main pattern is:</p>
<p><code><action>a<object></code> and <code><action>i<object></code></p>
<p>Where action can be any action, for example, <code>d</code> (delete), <code>y</code> (yank), <code>v</code> (select in visual mode). The object can be: <code>w</code> a word, <code>W</code> a WORD (extended word), <code>s</code> a sentence, <code>p</code> a paragraph. But also, natural character such as <code>"</code>, <code>'</code>, <code>)</code>, <code>}</code>, <code>]</code>.</p>
<p>Suppose the cursor is on the first <code>o</code> of <code>(map (+) ("foo"))</code>.</p>
<blockquote>
<ul>
<li><code>vi"</code> → will select <code>foo</code>.</li>
<li><code>va"</code> → will select <code>"foo"</code>.</li>
<li><code>vi)</code> → will select <code>"foo"</code>.</li>
<li><code>va)</code> → will select <code>("foo")</code>.</li>
<li><code>v2i)</code> → will select <code>map (+) ("foo")</code></li>
<li><code>v2a)</code> → will select <code>(map (+) ("foo"))</code></li>
<h3id="macros-qa-do-something-q-a">Macros : <code>qa</code> do something <code>q</code>, <code>@a</code>, <code>@@</code></h3>
<p><code>qa</code> record your actions in the <em>register</em><code>a</code>. Then <code>@a</code> will replay the macro saved into the register <code>a</code> as if you typed it. <code>@@</code> is a shortcut to replay the last executed macro.</p>
<blockquote>
<p><em>Example</em></p>
<p>On a line containing only the number 1, type this:</p>
<ul>
<li><code>qaYp<C-a>q</code> →
<ul>
<li><code>qa</code> start recording.</li>
<li><code>Yp</code> duplicate this line.</li>
<li><code><C-a></code> increment the number.</li>
<li><code>q</code> stop recording.</li>
</ul></li>
<li><code>@a</code> → write 2 under the 1</li>
<li><code>@@</code> → write 3 under the 2</li>
<li>Now do <code>100@@</code> will create a list of increasing numbers until 103.</li>
<p>That was 90% of the commands I use every day. I suggest that you learn no more than one or two new commands per day. After two to three weeks you’ll start to feel the power of vim in your hands.</p>
<p>Learning Vim is more a matter of training than plain memorization. Fortunately vim comes with some very good tools and excellent documentation. Run vimtutor until you are familiar with most basic commands. Also, you should read this page carefully: <code>:help usr_02.txt</code>.</p>
<p>Then, you will learn about <code>!</code>, folds, registers, plugins and many other features. Learn vim like you’d learn piano and all should be fine.</p>
<blockquote>
<p>If you liked this article, there is a follow up: <ahref="../../../../Scratch/en/blog/Vim-as-IDE/"><em>Vim as IDE</em></a></p>