Linux Learning-Bird Brother-Chapter Nine-vim program editor


vi Is a text editor in command-line mode, vim is the advanced version vi , vim not only can display text in different colors, but also can be carried out, such as shell scripting, C language program editor, can be vim seen as a program editor,

One, vi and vim

Why study vim:

  • All UNIX-likesystems will have a built-in vitext editor
  • Many software editing interfaces will be actively calledvi
  • vimWith the ability to edit the program, it can actively identify the correctness of the grammar by font color, which is convenient for program design

Second, the use of vi

viThere are three modes, namely general command mode, edit mode and command line mode

  • General command mode
    Function: cursor movement, search and replace, delete character, delete entire column, copy entire column, paste entire column
    Description of function keys available in general command mode How to move the cursor
    [Page Down] Page down
    [Page Up] Page up
    0 or [Home] Move to the first character of this line
    $ Or [End] Move to the last character of this line
    G Move to the last line of this file
    gg Move to the first line of this file
    n n is a number, representing that the cursor moves down n lines
Description of function keys available in general command mode Find and replace
/word Find a wordstring named under the cursor
?word Find a wordstring named above the cursor
:n1,n2s/word1/word2/g n1 and n2 are numbers, find /word1this string between n1 and n2 lines , and replace the string withword2
:1,$s/word1/word2/g Find the word1string from the first line to the last line and replace it withword2
:1,$s/word1/word2/g Find the word1string from the first line to the last line , and replace it with word2, before prompting the characters to confirm to the user whether to replace
。。。 。。。
  • Edit mode:
    function: press any one of the letters [i, I, o, O, a, A, r, R] from the general command mode to enter the edit mode, and insert or replace to edit the content
Description of available function keys for switching from general command mode to edit mode Enter insert or replace edit mode
i与I iTo insert from the current cursor position, Ito insert at the first non-space of the current line
a与A aTo insert Afrom the next character at the current cursor, to insert from the last character of the line where the cursor is
o与O oTo insert a new line oat the current line under the cursor ; to insert a new line for the previous line at the current cursor
r与R Enter replace mode, rit will replace the one character under the cursor once; Rwould have been replaced by the text cursor until you press Escup
[Esc] Exit edit mode and return to normal command mode
  • Command line mode
    Function: Press [:, / ,? from general command mode ] To enter the command line mode, with other additional functions for reading / storing files.
    Let ’s learn about the vicommonly used key functions
Description of available function keys for switching from general command mode to command mode Command line mode save, exit and other commands
:w Write the edited data to the hard disk file
:w! If the file attribute is [read-only], the file is forced to be written. Whether it can be written is related to the file permissions of the file
:q drop outvi
wq Exit after saving, if :wq!yes, force save and exit

Three, vim extra features

1. Visual Block

How to manipulate data with columns as the operating unit?

Key meaning of visual blocks
v Character selection, will highlight the place where the cursor passes
V Line selection, the line passing by the cursor will be highlighted
[Ctrl]+v Visualize blocks, you can select data in a rectangular way
y Copy the highlights
d Delete the highlight
p Paste the block you just copied at the cursor position

2. Multi-file editing

vimSeveral files are connected at the same time to open at the same time. The related keys are:

Buttons for multi-file editing
:n Edit next file
:N Edit last file
:files List all files currently opened by this vim

3. Multi-window function


4. vimKeyword completion function

vimCompleting the functions, there are roughly the following:

Key combination Completed content
[ctrl]+x—>[ctrl]+n Use the [text of the document] that is currently being edited as a keyword to complete
[ctrl]+x--->[ctrl]+f Use [file name] in the current directory as a key word to complete
[ctrl]+x--->[ctrl]+o Add the extension as a grammatical supplement and fill in with the vimbuilt-in keywords

Note: The extension of .htmlor must be used .php, otherwise the vimcorrect syntax check function will not be called.

5. Vim environment settings and records: /.vimrc**, ** /. Viminfo

vimThe setting value of is generally placed in the /etc/vimrcfile, it is generally not recommended to modify

6. *** vim *** Commonly used commands

[External chain image transfer failed, the source site may have an anti-theft chain mechanism, it is recommended to save the image and upload it directly (img-tBqBzts9-1586392123529) (C: \ Users \ lee \ Desktop \ vim common command schematic.jpg)]

Three, other vim use matters needing attention

vimAlthough it is powerful, there are some places to pay attention to below

1. The problem of Chinese encoding

由于编码的问题,vim有时候会无法正常显示正常的中文。
vim的终端界面中使用的是UTF-8,由于编码的不同,中文内容便会显示一堆乱码,这时候需要考虑一下几点:

  • Linux系统默认支持的语系数据,与/etc/locale.conf有关
  • 终端(bash)的语系,与LANG、LC_ALL几个变量有关
  • 文件原本的编码
  • 打开终端的软件,例如GNOME下面的窗口界面

2.DOS与Linux的换行符

两者的字符定义不同,不建议在Windows系统中将文件编辑好之后再上传到Linux系统。当对文本文件进行复制时,需要使用unix2dosdos2unix转换行格式

3.语系编码转换

iconv命令可以将语系编码进行转换

四、重点回顾

  • Linux下面的配置文件多为文本文件,故使用vim即可进行设置编辑
  • vim可视为程序编辑器,可用以编辑shell脚本,配置文件等,避免打错字
  • vi有三种模式,包括命令模式,编辑与命令行模式,常见的按键有i[Esc]wq
  • vi的替换功能,例如:n1,n2s/old/new/g
  • 进入编辑模式几乎只要记住:i,o,R三个按键即可,尤其是新增一行的o与替换的R
  • vim的环境设置可以写入在~/.vimrc文件中
  • 可以使用iconv进行文件语系编码的转换
  • 使用dos2unixunix2dos可以变更每一行的行尾换行符
    R三个按键即可,尤其是新增一行的o与替换的R
  • vim的环境设置可以写入在~/.vimrc文件中
  • 可以使用iconv进行文件语系编码的转换
  • 使用dos2unixunix2dos可以变更每一行的行尾换行符
  • 对于文章内有对齐的区块,可使用[ctrl]-v进行复制,粘贴,删除的操作
发布了33 篇原创文章 · 获赞 4 · 访问量 3万+

Guess you like

Origin blog.csdn.net/leaeason/article/details/105402181