[] The most complete history vim editor from entry to the grave, from amazing to proficient (continuous improvement)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/LEON1741/article/details/100061918

Linux to play vi people know, this is a legendary tool, all Unix-like systems will be built vi text editor, other editors will not necessarily exist. But vi cumbersome to use, function is relatively simple, so now we use more advanced version of its --vim editor. This article is to introduce a full range of vim, including theory, practice various aspects of each dimension, but also add some of my own experiences in the practice of summary, the key is to constantly improve and supplement, I hope this really as the title became a guide to everyone from entry into the grave, from amazing to master a good article, I hope you a lot of guidance, more exchanges!

1. What is the vim

As mentioned above, vim is developed from a vi text editor. Convenient programming code completion, and compilation errors jumps, etc. is particularly rich, is widely used in programmers, Emacs and Unix-like systems tied for the user favorite text editor.

vim design is a combination of command, among a variety of users to learn the text move / jump commands and other common mode of editing commands, and can be flexibly used in combination, it can be more than those without a schema editor efficient text editing. At the same time a lot of vim and shortcut keys and regular expressions similar, can aid memory, and vim for programmers is optimized.

2, vim advantage

Online casual look, the sky fans are praised vim strong, I do not say, direct excerpt from the book Jane [ https://www.jianshu.com/p/ca7fff98edfb original on] for your reference.

  1. High efficiency editing text.
    The role of the editor is mainly to deal with text, text processing speed of the points there. Manipulating text mainly input, cursor move, delete, and copy and paste, etc., the same cursor movement, vim may be faster than the average editor on many, vim copy and paste functions are also powerful enough to make you want to give vim again learning : D, I believe, to learn vim will significantly improve the efficiency of your code or editing text.

  2. A perfect combination of various editors and IDE.
    If you want to fight the vim c / c ++ / python java even the IDE, no problem, but I believe that no matter how you configure the set can not exceed JetBrain family. If you can put Android Studio or Pycharm shortcuts vim editor with the high efficiency of not it be even more powerful? This proved to be feasible, ideavim support for vim is still very good, and almost all of the editor or IDE have vim plugin, you do not have to worry about without your beloved notepad ++ on the go mac, do not worry about a career change from android ios in huge changes in the editing mode, learn vim, on different platforms, different editor or IDE inside a vim with anything.

  3. Remote editing
    Most linux systems have built-in vi editor, which is the reason why most people should learn vim instead of emacs, and I think if you develop should not escape some log remote access or modify some configuration parameters or something, this time vi skills will come in handy you, if you think i just need to know and: wq ok, then I really would not be able to argue, after all, there was a time I was doing, but I think you really put hjkl get cooked, by the way also fluent after moving the cursor should not think so.

  4. Scalable, private custom function
    vim Although already very strong, but some of the special needs and can not be perfect, if you have the ability, then you can learn to write their own scripts vimscript own, absolutely personal order. Of course, if too lazy to learn vimscript words, python is also possible, but the basic vimscript still have to grasp. If you're really lazy to die, you can go to google or github, chances are you think of the needs of others have written for you plug it waiting for you to discover.

  5. Quick start
    This is my opinion as a condition editor must have, like the Atom launch speed is very slow, notepad ++ to do pretty good, but unfortunately, only supports windows platform, vim startup speed is very fast, open to modify some configuration edit text files, and very efficient. In addition, it is said there is no quick start emacs vim, ha ha.

What are you waiting for, it's time to look at a good typing habits formed over the years, and a little closer look at vim, you will find that the previous code word how much room for improvement. Now I will continue to share some vim and operating skills of learning with you, want to learn together, to carry forward the spirit vimmer, so be your last vim editor.

3, vim installation

Very simple, direct a command to get!

sudo apt-get install vim

By default, the system will be installed in / usr / share / vim directory. After installing, use the "vim -version" command to view your installed version:

leon@Ubuntu:~$ vim --version
VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Jun 07 2019 15:35:43)
Included patches: 1-1689
Extra patches: 8.0.0056
Modified by pkg-vim-maintainers@lists.alioth.debian.org
Compiled by pkg-vim-maintainers@lists.alioth.debian.org

4, vim configuration

All configuration options vim all stored in its configuration file [] in .vimrc. It usually has two profiles, the global version of (global) and user version (personal).

  • Global configuration file in the directory where you installed the vim [/ etc / vim / vimrc, you can also access its link file / usr / share / vim / vimrc]. If you change the contents, will be effective for all users.
  • User profiles in your home directory [~ / .vimrc]. If you change the contents, you only take effect for the current user.

If you enable a configuration item for only a single edit can be in command mode, first enter a colon, and then enter the configuration. For example, set number can be written in this configuration .vimrc which can be entered in the command mode.

Configuration items are generally "open" and "closed" two settings. "Close" is in the "open" preceded by the prefix "no".

" 打开
set number

" 关闭
set nonumber

Discover a CI is open or closed, can be in command mode, enter this configuration, and followed by a question mark.

:set number?

The above command will return number or nonumber.

If you want to view the help, you can use the help command.

:help number

Here is my article in actual use, the summary of some of their experience:

4.1, vim show how to solve the problem of Chinese garbled

Very simple configuration file in the [global vim / etc / vim / vimrc, you can also access its link file / usr / share / vim / vimrc] in, at the end add the following lines of code:

set fileencodings=utf-8,gb2312,gb18030,gbk,ucs-bom,cp936,latin1
set termencoding=utf-8
set fileformats=unix
set encoding=prc

4.2, to be added

To be added ...

5, vim use

5.1, open the file using vim

hot key Functional Description
vim filename Open or create a new file, and the header portion of the first cursor row
vim -r filename Recover files crash when the last open vim
vim -R filename The specified file in read-only mode into the Vim editor
vim + filename Open the file and places the cursor in the last line of the header
vi + n filename Open the file, and the cursor of the n-th row header
vi +/pattern filename Jian play file, and a position of the cursor on the first matching pattern
vi -c command filename Before editing a file, runs the specified command

5.2, read the file using vim

hot key Functional Description
h or left arrow key (←) Cursor left one character described [see below]
j or down arrow key (↓) Move the cursor down one character [described below]
k or the up arrow key (↑) Cursor up one character described [see below]
l or the right arrow key (→) The cursor moves to the right one character [see below] Description
[Ctrl] + [f] Screen "down" to move one, the equivalent of [Page Down] keys (used)
[Ctrl] + [b] Screen "up" move one, the equivalent of [Page Up] button (common)
[Ctrl] + [d] Screen "down" to move half a page
[Ctrl] + [u] Screen "Up" to move half a page
+ Move the cursor to the next line of the non-space character
- Move the cursor to the line of non-space characters
n n represents a number, for example 20. Press the number and then press the space key, the cursor will move to the right n characters of the line
0 Or function key [Home] This is the number "0": the characters move to the front of the line at the (common)
$ Function key or the [End] moves to the last character of the surface of the line (common)
H Move the cursor to the first character of this top of the line screen
M The first character cursor is moved to the line of the center of the screen
L Move the cursor to the first character in the bottom of the screen that row
G The move to the last line of the file (common)
ng n is a number. Move to the n-th row of this file. 20G, for example, the line will move to the file 20 (can be used with: set nu)
gg Move to the first line of the file, equivalent to 1G ah! (Common)
n n is a number. Move the cursor down n row (common)

Note: If you put the right hand on the keyboard, you will find hjkl be arranged together, so you can use these four buttons to move the cursor. If you want to move a plurality of times, then, for example, downward movement of the line 30, may be used "30j" or "30 ↓" key combination, i.e. the number of times plus (digital) want to, the pressing action can be!

5.3, edit the file using vim

5.3.1, insert text

Entering commands from input mode to edit mode, press I, i, O, o, A, a key to complete the like, the use of different keys, a different position of the cursor is located, as shown in the following table.

hot key Functional Description
i Then inserted in the input text of the current cursor position, the cursor text corresponding to the right
I In the first row of the cursor line is then inserted into the text input, the first line is the first non-blank character in the line, corresponds to the beginning of the line the cursor moves to the execute command i
O Insert a new row below the cursor line. Cursor in the first empty row, waiting to enter text
O Insert a new row in the row above the cursor. Cursor in the first row blank line, waiting to enter text
a Then insert text input after the current cursor position
A End of the line cursor in the text row into a subsequent input, move the cursor to the end of the line corresponds to then execute a command

5.3.2 Find text

hot key Functional Description
/abc Abc search string from the cursor position forward
/^abc 查找以 abc 为行首的行
/abc$ 查找以 abc 为行尾的行
?abc 从光标所在为主向后查找字符串 abc
n 向同一方向重复上次的查找指令
N 向相反方向重复上次的查找指定

说明:
1、如果在文件中并没有找到所要查找的字符串,则在文件底部会出现 “Pattern not found” 提示。
2、在查找过程中需要注意的是,要查找的字符串是严格区分大小写的,如查找 “shenchao” 和 “ShenChao” 会得到不同的结果。
3、如果在字符串中出现特殊符号,则需要加上转义字符 “”。常见的特殊符号有 \、*、?、$ 等。如果出现这些字符,例如,要查找字符串 “10$”,则需要在命令模式中输入 “/10$”。

5.3.3、替换文本

快捷键 功能描述
r 替换光标所在位置的字符
R 从光标所在位置开始替换字符,其输入内容会覆盖掉后面等长的文本内容,按“Esc”可以结束
:n1,n2s/a1/a2/g 将文件中 n1 到 n2 行中所有 a1 都用 a2 替换
:g/a1/a2/g 将文件中所有的 a1 都用 a2 替换

例如,要将某文件中所有的 “root” 替换为 “liudehua”,则有两种输入命令,分别为:

:1, $s/root/liudehua/g
或
:%s/root/liudehua/g

上述命令是在编辑模式下操作的,表示的是从第一行到最后一行,即全文查找 “root”,然后替换成 “liudehua”。

如果刚才的命令变成【:10,20 s/root/liudehua/g】,则只替换从第 10 行到第 20 行的 “root”。

5.3.4、删除文本

快捷键 功能描述
x 删除光标所在位置的字符
dd 删除光标所在行
ndd 删除当前行(包括此行)后 n 行文本
dG 删除光标所在行一直到文件末尾的所有内容
D 删除光标位置到行尾的内容
:a1,a2d 函数从 a1 行到 a2 行的文本内容

注意,被删除的内容并没有真正删除,都放在了剪贴板中。将光标移动到指定位置处,按下 “p” 键,就可以将刚才删除的内容又粘贴到此处。

5.3.5、复制文本

快捷键 功能描述
p 将剪贴板中的内容复制到光标后
P 将剪贴板中的内容复制到光标前
yy 将光标所在行复制到剪贴板,此命令前可以加数字 n,可复制多行
yw 将光标位置的单词复制到剪贴板

5.4、使用vim保存文件

快捷键 功能描述
:wq 保存并退出 Vim 编辑器
:wq! 保存并强制退出 Vim 编辑器
:q 不保存就退出 Vim 编辑器
:q! 不保存,且强制退出 Vim 编辑器
:w 保存但是不退出 Vim 编辑器
:w! 强制保存文本
:w filename 另存到 filename 文件
x! 保存文本,并退出 Vim 编辑器,更通用的一个 vim 命令
ZZ 直接退出 Vim 编辑器

在这里插入图片描述
在这里插入图片描述

结束语

vim是一个非常强大的工具,强大到你不敢想象。我也只是一个刚入门的初学者,这里只是收集和总结了我自己已经了解到的一些信息和技巧,还有很多其他的部分我还没有接触到,欢迎大家留言交流,多多补充!

Guess you like

Origin blog.csdn.net/LEON1741/article/details/100061918