tl;dr: Want to learn vim (the best text editor known to human kind) the fastest way possible. I suggest you a way. Start by learning the minimal to survive, then integrate slowly all tricks.
Vim the Six Billion Dollar editor
Better, Stronger, Faster.
Learn vim and it will be your last text editor. There isn’t any better text editor I know. Hard to learn, but incredible to use.
I suggest you to learn it in 4 steps:
By the end of this journey, you’ll become a vim superstar.
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 music 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.
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 Normal mode. Let’s get in Insert mode. Type on the letter
You should feel a bit better. You can type letters like in a standard notepad. To get back in Normal mode just tap the
You know how to switch between Insert and Normal mode. And now, the list of command you can use in Normal mode to survive:
i→ Insert mode. Type
ESCto return to Normal mode.
x→ Delete the char under the cursor
:wq→ Save and Quit (
dd→ Delete (and copy) current line
hjkl(highly recommended but not mandatory) → basic cursor move (←↓↑→). Hint:
jlook like a down arrow.
:help <command>→ Show help about
<command>, you can start using
:helpwithout anything else.
Only 5 commands. This is very few to start. Once these command start to become natural (may be after a complete day), you should go on level 2.
But before, just a little remark on Normal mode. In standard editors, to copy you have to use the
Ctrl key (
Ctrl-c generally). In fact, when you press
Ctrl, it is a bit like if all your key change meaning. With vim in Normal mode, it is a bit like if your
Ctrl key is always pushed down.
A last word about notations:
Ctrl-λ, I’ll write
:will must end by
<enter>. For example, when I write
You know the commands required for survival. It’s time to learn a few more commands. I suggest:
Insert mode variations:
a→ insert after the cursor
o→ insert a new line after the current one
O→ insert a new line before the current one
cw→ replace from the cursor to the end the word
0→ go to first column
^→ go to first non-blank character of the line
$→ go to the end of line
g_→ go to the last non-blank character of line
/pattern→ search for
P→ paste before, remember
pis paste after current position.
yy→ copy current line, easier but equivalent to
Load/Save/Quit/Change File (Buffer)
:e <path/to/file>→ open
:saveas <path/to/file>→ save to
:wq→ save and quit
:q!→ quit without saving, also
:qa!to even if there are some modified hidden buffers.
:bp) → show next (resp. previous) file (buffer)
Take the time to integrate all of these command. Once done, you should be able to do every thing you are able to do on other editors. But until now, it is a bit awkward. But follow me to the next level and you’ll see why.
Congratulation reaching this far! We can start the interesting stuff. At level 3, we’ll only talk about command which are compatible with the old vi.
Let’s look at how vim could help you to repeat yourself:
.→ (dot) will repeat the last command,
Some examples, open a file and type:
2dd→ will delete 2 lines
3p→ will paste the text 3 times
100idesu [ESC]→ will write “desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu “
.→ Just after the last command will write again the 100 “desu “.
3.→ Will write 3 “desu” (and not 300, how clever).
Knowing how to move efficiently with vim is very important. Don’t skip this section.
G→ Go to line N
gg→ shortcut for
1G, go to the start of the file
G→ Go to last line
w→ go to the start of the following word,
e→ go to the end of this word.
By default, word are composed of letter 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 uppercases:
W→ go to the start of the following WORD,
E→ go to the end of this WORD.
Now let’s talk about very efficient moves:
%: Go to corresponding
#) : go to next (resp. previous) occurrence of the word under the cursor
Believe me, the last three commands are gold.
Remember about the importance of vi moves? Here is the reason. Most commands can be used using the following general format:
<start position><command><end position>
For example :
0→ go to the beginning of this line
y→ yank from here
$→ up to the end of this line
We also can do things like
ye, yank from here to the end of the word. But also
y2/foo yank up to the second occurrence of “foo”.
But what was true for
y (yank), is also true for
v (visual select),
gu (lowercase), etc…
With all preceding commands you should be comfortable to use vim. But now, here are the killer features. Some of these features were the reason I started to use vim.
0→ go to column 0
^→ go to first character on the line
$→ go to the last character on the line
fa→ go to next occurrence of the letter
aon the line.
;) will seek for the next (resp. previous) occurrence.
t,→ go just before the character
3fa→ search the 3rd occurrence of
aon this line.
A useful tip is:
dt" → remove everything until the
These command can only be used after an operator of in visual mode. But they are very powerful. Their main pattern is:
Where action can be any action, for example,
v (select in visual mode). And object can be:
w a word,
W a WORD (extended word),
s a sentence,
p a paragraph. But also, natural character such as
Suppose the cursor is on the first
(map (+) ("foo")).
vi"→ will select
va"→ will select
vi)→ will select
va)→ will select
v2i)→ will select
map (+) ("foo")
v2a)→ will select
(map (+) ("foo"))
Rectangular blocks are very useful to comment many lines of code. Typically:
^→ go to start of the line
<C-v>→ Start block selection
<C-d>→ move down (could also be
I-- [ESC]→ write
--to comment each line
Not on windows you might have to use
<C-q> instead of
<C-v> if your clipboard is not empty.
In Insert mode, just type the start of a word, then type
qa record your actions in the register
@a will replay the macro saved into the register
a as if you typed it.
@@ is a shortcut to replay the last executed macro.
On a line containing only the number 1, type this:
Ypduplicate this line.
<C-a>increment the number.
@a→ write 2 under the 1
@@→ write 3 under the 2
- Now do
100@@will create a list of increasing numbers until 103.
We saw an example with
<C-v>. There is also
V. Once the selection made, you can:
J→ join all lines together.
>) → indent to the left (resp. to the right).
=→ auto indent
Add something at the end of all visually selected lines:
$go to the end of line
A, write text,
Here are the main commands, but you should look at
:split→ create a split (
:vsplitcreate a vertical split)
<C-w><dir>: where dir is any of
hjklor ←↓↑→ to change split.
<C-w>|) : maximise size of split (resp. vertical split)
<C-w>-) : Grow (resp. shrink) split
That was 90% of commands I use every day. I suggest you to learn no more than one or two new command per day. After two to three weeks you’ll start to feel the power of vim in your hands.
Learning Vim is more a matter of training than plain memorization. Fortunately vim comes with some very good tools and an excellent documentation. Run vimtutor until you are familiar with most basic commands. Also, you should read carefully this page:
Then, you will learn about
!, folds, registers, the plugins and many other features. Learn vim like you’d learn piano and all should be fine.
Do you wish you could view a Java Class File using Vim?
First of all you will need a Java Decompiler to decompile the Class File. I would suggest the JAD decompiler by Pavel Kouznetsov
It's a command line decompiler and absolutely free.
Next create a vimscript file called jad.vim:
augr class au! au bufreadpost,filereadpost *.class %!d:jad.exe -noctor -ff -i -p % au bufreadpost,filereadpost *.class set readonly au bufreadpost,filereadpost *.class set ft=java au bufreadpost,filereadpost *.class normal gg=G au bufreadpost,filereadpost *.class set nomodified augr END
Note: Keep the Jad.exe in a directory without white spaces. The -p options directs JAD to send the output to standard output instead of a .jad file. Other options are described on the JAD site.
Next add the following line in the vimrc file.
Next time you do vim abc.class, you have the source code for abc.class.
NOTE: I have written the script so as to open the class file read only, So that you don't accidentally modify it. You can also extend this script to unjar a jar file and then view each file in the JAR file.
I modified this a little bit to use the improved file runtime organisation of vim60, I don't know if other will like it better but here it is: I added to filetype.vim:
augroup filetypedetect au! BufRead,BufNewFile *.jad setfiletype java au! BufRead,BufNewFile *.class setfiletype class augroup END
and created in ftplugin a class.vim with
%!jad.exe -noctor -ff -i -p % set readonly set ft=java normal gg=G set nomodified
If you don't want to 'hit ENTER' after every decompilation, you can add 'silent' to some of the lines:
au bufreadpost,filereadpost *.class silent %!jad -noctor -ff -i -p %
and maybe even:
au bufreadpost,filereadpost *.class silent normal gg=G
I can now select any class file, right click, and select the "Edit with Vim" option to view the source.
I'm just wondering, how do I change this to if I do the same to a jar file, and it will show me the contents of the jar file, as in a jar -tf, and if I select any of the entries, I can view the source?
I tried opening the jar file using WinZip, and if I view any of the entries in it using vim, I'll get something like this: JavaClassFileReadException: can't open input file on `C:\Documents' JavaClassFileReadException: can't open input file on `and' JavaClassFileReadException: can't open input file on `Settings\gwunwai\Local' JavaClassFileReadException: can't open input file on `Settings\Temp\Version.class'
I think that's because WinZip extracts the zip entries into my temp directory, which contains white spaces.
Is there some way around this?
you can change the command to %!jad.exe -noctor -ff -i -p "%" (enclosed the % with quotes). It worked for me