My Love on Vim

My life with Vim

I started learning Vim during Aug, 2011, the original motivation was trying to "get rid of my mouse while I am coding", I had no teacher but google, learnt it from the very hard way. From the beginning 2-3 months, honest speaking, I was less productive while using Vim comparing with IDEs, there were so many things I could do in second in IDE, for example: search/replace, formatting, re-factoring, folding, and so on, whereas I had to spent days in learning how to accomplish a simple task in Vim, that was really painful, but I never gave up! After the first two months of exploration, I was feeling much more comfortable in using Vim, and I believe I am going to use this king of text editor during rest of my life:).

Reasons why loving

There are millions of people love Vim during the past decades in the planet, there could be different reasons why people love Vim, I believe the ultimate reasons are:

  • Type less, do more!
  • Mouse-less!
  • Ultimate productivity!
  • Unlimited extensibility!
I just want express my understanding on the last fact: Vim can be extended through Vim script, ruby, python, lua, any language you like, unlimited possibility, as long as you are doing something that many people are doing as well, there would be plugins to help you; there isn't? You create one and benefits other people doing similar thing like you do!

Hacking with Vim technical talk

Navigate with < and > Or Click to View the presentation in a new Tab

Hack-with-Vim-photo

I did a technical talk to share my most favourite Vim tips to colleagues in Autodesk Singapore office, it was a short one, but with many ultimate and useful hacking tips with Vim, my intention was trying to excite people, let them open their mouth: "Wow! I never realized that Vim can do things like this!!", or "I did know Vim is powerful, I never expect it can be that powerful!!".

As I wish, I got some very positive feedback, and some familiar friends said they re-recognized me:)

My favourite Vim tips

The content below are the tips I demonstrated in my talk above, all of them are not my invention, but standing upon giant's shoulder! Since I will be using Vim in rest of my life, there is no doubt I about this point, and I wish I will contribute as much as I can to this god like text editor and its eco-system in the future!

Ultisnips + vim-snippets

Ultisnips boosts my coding speed like a rocket! It works with vim-snippets behind the scene to allow you to type very few characters but generates a usually used code snippet for you, with place-holders for you to easily customizing the snippet, this combination supports all mainstream languages as long as there are geeks speak it! See my screenshot below:

Vim-snippets

  • Ultisnips is written in python and is more preferable comparing to the relatively old snipmate, see discussion here.
  • If you couldn't find the language you want, for example Apple swift, dude, it is a good opportunity for you to contribute:)

Dragging lines

Dragging line(s) with automatically indentation fixing, this trick is accomplished by adding the following lines into vimrc:

  " Alt+j/k to move current line/selected lines up and down 
  nnoremap  :m+== 
  nnoremap  :m-2==
  inoremap  :m+==gi
  inoremap  :m-2==gi
  vnoremap  :m'>+gv=gv
  vnoremap  :m-2gv=gv

It works for single line:
Vim-drag-single-line

It works for multi lines:
Vim-drag-multi-lines

Macros

The JSON conversion I did during the technical session was accomplished by the following key combinations:

  1. gg #Make sure current cursor is at the buffer beginning.
  2. qa # Start recording macros "a"
  3. i # Insert
  4. "
  5. f= #Forward to the char "="
  6. i" # Insert double quote before "="
  7. ESC #Back to normal mode
  8. l #Move to "="
  9. r: #Replace "=" with ":"
  10. a" #Append a double quote after ":"
  11. A", # Append " and , at the end of line
  12. j0 #Move to the beginning of the next line
  13. 28@a #Repeat this operation for 28 times

Learn more: Macros - Vim Tips Wiki

Execute script inside Vim

Every vimer knows we can execute script (shell, perl, python, ruby, js, just everything) right inside Vim without switching to terminal, although many people are tmux funs, I still prefer do it in Vim's way, by having the following lines in my vimrc, I can instantly run the current script and view the result with nearly no latency:)

  autocmd FileType sh map   :w:!/bin/sh % 
  autocmd FileType ruby map   :w:!/usr/bin/env ruby % 
  autocmd FileType python map   :w:!/usr/bin/env python % 
  autocmd FileType javascript map   :w:!/usr/bin/env node % 

Match & Until

cst # Change surround tag, pretty useful when editing HTML

vit # View in current tag

dit # Delete content in current tag

f$ # Forward to the first occurrence of $ within current line

F$ # Backward to the first occurrence of $ within current line

dt$ # Delete content until matching the first occurrence of $ within current line

vt$ # View (Select) content until matching the first occurrence of $ within current line

Search & Replace

Vim is extremely powerful in dealing with searching and replacing, the trick I've done in the session is just tip of the iceberg, I typed :g/#/d to remove all comments in a Ruby file.

Vimium

This is a awesome Chrome extension, the home page is: https://chrome.google.com/webstore/detail/vimium/dbepggeogbaibhgnhhndojpepiihcmeb?hl=en, suffering the internet without mouse is absolutely feasible!

My vimrc

http://WayneYe.com/vimrc

My future on Vim

I have been using Vim everyday during the past 3 years, I never stopped learning it, and with no doubt, I will do so in rest of my life, really, I just can't breath without it!

There are absolutely a lot of improvement space for me, apart from continuously improve productivity, I have intention to do more things in vim: Email, highly customized snippets (refer: Ultisnips Screencast Episode 4, presentation, and more important, I want to contribute to Vim ecosystem!

Tags:

Categories:

Updated:

Leave a comment