How to use Vim efficiently
PHP中文网
PHP中文网 2017-05-16 16:42:38
0
18
1643

Newbies have just come into contact with Vim. I hope you guys can recommend some introductory practice tutorials. Now I just use Vim as a normal editor. Also, it seems a bit troublesome to press Esc to return to normal mode. You have to reach to the upper left corner of the keyboard. Hey, is there any easy way? First, thank you!

PHP中文网
PHP中文网

认证0级讲师

reply all(18)
为情所困

I have been using vim for almost two years and have read a lot of tutorials. Generally speaking, I still have to practice more by myself and add functions when I feel it is necessary. Here I will share the best tutorial I have ever seen. It was written by a friend from Tieba. It is very well written

Zero

Learn to touch type

Start with the simplest configuration file. Don’t consider configuring plug-ins before level 5. Don’t memorize commands by heart. Don’t directly use other people’s configuration files.
Basically, if you stay in insert mode for a long time, you will feel that vim is mediocre and not easy to use,

At this time, the editing efficiency may be lower than using the original general editor. When others ask what editor to use,
Most people will answer: vim with a nano, kate, kwrite, gedit, geany or other IDE

Develop the habit of staying in normal mode (normal is not called for nothing). Enter insert mode only when typing. After typing, immediately <ESC>
Learn the movement commands in normal mode. The standard process for inputting text at this time:

  1. normal mode movement command accurately positions the cursor (remember, the search command is also a movement command)
  2. Enter insert mode editing (don’t just use i when entering insert mode, get used to using I a A o O s C depending on the situation)
  3. <ESC> Return to normal mode

At this time, the editing effect will return to the same as the ordinary editor you used before, or even slightly improved

Learn what an operator is (commands d y c, etc.), what is motion (all commands that can move the cursor, h j k l w e f t / ?, etc.),
Learn the method of operator + motion, such as ct. (Delete the content between the current cursor and . (point) and enter insert mode to prepare for modification)
Learn basic Ex commands, :s and so on

At this time, the editing efficiency begins to improve significantly. When using other general editors, you will start to feel uncomfortable and inefficient

Learn text-objects and know the method of operator + text-objects to perform surgically precise positioning and modification,
Since you are mainly using c/c++, here are some examples that are useful in this case:

ci" (consisting of change operator and text-object i")

This command will find the next string enclosed in "" on the line where the current cursor is located, clear the content inside the quotes, and enter insert mode for easy modification
It is easier to use than to explain. You can use const char* hello = "Hello world ."; Code like this to test

yaB (consisting of yank operator and text-object aB)

This command will copy the code block where the current cursor is (the content enclosed by {}, including the curly braces themselves) to the default register.
It is convenient to use p to paste it elsewhere (of course, copy and paste are not encouraged when writing code), You can use any {} to test, and then find another place to p

gUiw (composed of make uppercase operator and text-object iw)

This command will change the word currently under the cursor to all uppercase letters
For example, change max_size to MAX_SIZE

At this time, I began to understand why vim is accurate and efficient, and it’s not because of the full keyboard without the need for a mouse

After getting used to operator + motion, I found that the . command is very useful. Pay more attention to using repeatable command combinations
Practice using bookmark positioning, q to record macros, using multiple registers, using args, and using buffer
Use other Ex commands

At this moment, I can’t remember how I got through the days before vim was not used

LU

Start map commands that are often used repeatedly, start writing vimscript, and start to know what kind of plug-ins you need
Let’s try to recommend a few:
pathogen or vundle, tagbar, CtrlP, Gundo, UltiSnips, surround, Syntastic, Conque, ack.vim, vim-commentary, fugitive (if you are a git user)
There are many more, but most of them are not directly helpful to c/c++, so I won’t mention them

At this time, I have no interest in other editors, except maybe emacs

Have a completely personalized vimrc, basically enter the realm of transformation, and become a legend in the world
People often observe you editing, and you are inspired to become ambitious and want to learn vim. After trying it for a few hours, you give up because you feel that the hotkeys are not "humane".
But occasionally he will mention to people that he has seen a person like you in the world

仅有的幸福

I have been using vim for a year. I am about to write a vim cheat sheet a few days ago, so I will write it here in advance.

First vim != gvim

1.How to edit

Press the ESC key at any time to enter normal mode. In normal mode, you can press: to enter commands.

Press i or a in normal mode to enter insert mode, then you can edit text.

2. Simple editing

  • Move the cursor: You can directly use the arrow keys to move the cursor, a better way is to use vim hjkl
  • Save the file, in normal mode: w. Exit editing: q, force save: w! Force exit: :q!
  • Undo operation: u or press u in normal mode

3. Faster editing

Now you can write in vim.

We need faster editing

1. Quickly position the cursor:

  • ^ Jump to the beginning of the line (similar to the function key Home)
  • $ Jump to the end of the line (similar to the function key End)
  • :n jump to line n
  • :$ Jump to the last line

2. Quick editing:
In normal mode:

  • dd delete a line
  • yy copy a line
  • p paste
  • r Single character replacement

3. More sophisticated paste and copy:

  • m,nd - quickly delete rows m to n (delete)
  • m,ny - copy line m to n (yank)
  • m,nmt - cut line m to line n to line t (move)
  • m,ncot - Copy line m to line n to line t (copy to)

4. Search
In normal mode, press / to search down, press ? to search up
5. Alignment
gg=G quickly aligns the entire file
6. Label operation

  • :tabnew mytab Create a new tab mytab
  • :tabn jump to next tab
  • :tabp Jump to the previous tab
  • Jump to next and previous tab in normal mode with gt or gT

7. Cutting window operation

  • :split cuts a window horizontally
  • :vsplit cuts a window vertically
  • Use Ctrl-w+arrow keys to switch between cutting windows.

4. Let vim understand you better

Have you noticed that we have not mentioned vimrc and plug-ins above.

The time has come.

For example, we like to display line numbers when coding: in command mode: set nu or :set number

About vimrc, there are many things worth studying and discussing. You can refer to my vimrc, I have made detailed Chinese comments

5. About plug-ins and themes

vim has three well-known color schemes, solarized, molokai and badwolf

About vim plug-ins, there are only a few words that need to be said:

  • Be sure to use vundel to manage plugins.
  • Don’t modify other people’s plug-ins, or fork and modify them yourself, and then bundle your plug-ins.
  • You can study how to use git submodule to manage your plug-ins.

Okay, let’s go eat. . That’s it.

伊谢尔伦

Nothing but familiarity.

" Arrow keys are evil
map <up>    <nop>
map <down>  <nop>
map <left>  <nop>
map <right> <nop>

Also, Ctrl-[ you can also exit the editing mode.

Peter_Zhu

Hi! I provide a great method:
Play vim games!

If you are using Ubuntu system, please enter:

  1. sudo apt-get install vimtutor
  2. vimtutor

Or via a cool web vim game:

vim adventurer
http://vim-adventures.com/

为情所困

hey~! I really don’t know how to use vim, only my hands know. (Practice makes perfect)

If you don’t like using esc, you can use <c-c> or <c-[> with the same effect.

Read more vim tips and learn more about various vim plug-ins.

Then again, it’s just a text editor, don’t put the cart before the horse.

Separation line

By the way, I recommend a vim plug-in collection called Janus, which defines itself as Janus: Vim Distribution. It provides a complete, necessary basic vim settings that most people need Plug-ins, and solutions to continue customizing vim. It’s useless to say more, RTFM.

習慣沉默

Using vim mainly involves defining various functions yourself and using various plug-ins. The main thing is to configure vimrc. You can refer to other people's vimrc and search for vimrc on github, such as https://github.com/search?q=vimrc&ref... to see how others configure it and change it to your own. .

esc can be replaced by jj or jk.

inoremap jk <ESC> 
为情所困

After reading the built-in tutorial (there is a Chinese version, download link, you need to go through the firewall to download), but you don’t need to memorize it by rote, just remember what functions it has.

When you encounter a place that seems to be able to use these functions, start to check the tutorials, and then slowly you will check less and less, less and less, and slowly you will know it.

For example:

Hello World foobar

If you want to delete foobar, then you can use ffdwThis combined command (f is a query in one line, and the f after it is the first letter of foober. This is the first part of the command. Move the cursor to the word foobar, and d is Delete, w is to select a word, and the combination is to delete a word), which is very fast and is a combination of simple commands.

Then if you want to deal with more complex scenes, such as

foobar
foobar
foobar
foobar

Only delete the previous foo and leave the bar. Although vim provides processing functions at this time, we don’t need to worry about it. We use the macro recording function (which can save your operations and you can simply repeat them). By combining the commands, you can do a lot of complicated work

For example, in the above example (I don’t have Vim at the moment, I may be wrong from memory),

qa (将命令录制到a这个剪贴板上{vim有好多个剪贴板,比如abcdefg……})
3x (重复三遍删除操作,将foo删掉,其实也可以用f命令来搭配,对复杂的情况更适用)
0 (移动到行首)
j (移动到下一行)
q (停止录制)
3@a (重复三遍刚才录制的命令,3就是将命令重复3遍,@就是运行,a就是刚刚保存的那个剪贴板)

This is solved.

Vim is not difficult at all, because 90% of the situations only require a flexible combination of simple commands.

The status of the remaining 10% is also easy to know by checking the tutorial. If you ask someone who can use Vim to dictate commands, and compare all the commands, you will find that they only know a very small part.

我想大声告诉你

The people above have said it in detail, but for the esc key, it is recommended to change the key to Caps Lock, which will be much easier to press.

It is said that when RMS wrote Emacs, the keyboard looked like this
Obviously, the distance between Esc and Control should be very comfortable. If you have money, buy a piece of HHKB.

阿神

伊谢尔伦

http://vim-adventures.com/
A small game written based on vim operations. The game also practices vim operations.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template