Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the basic ways to use vim

2025-04-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article, the editor introduces in detail "what are the basic methods of using vim", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "what are the basic uses of vim" can help you solve your doubts.

Vim (improved) editor is a powerful cross-platform text file editing tool, inherited from the Vi editor of Unix system, supports Linux/Mac OS X/Windows system, and can be used to establish and modify text files.

1. Introduction of vi and vim

(1) based on graphical interface, such as gedit, geany, etc. (2) based on command line, such as nano, vi, joe, etc.

Each text editor of the command line interface has its own keyboard shortcuts, for example, saving nano as CTRL+W,vim is: W; a text editor based on the command line is necessary. Linux's tty1~tty6 is a command line interface and cannot open a text editor with a graphical interface. Vi is a text editor installed by default for every Linux Distribution, and the default editor for many software is vi, so it is necessary to learn vi. Vim is the advanced version of vi, vi is a text editor, and vim should be said to be a program editor, because it can load different syntax highlighting according to file name, that is, color distinction, like ordinary IDE, for example, java files will follow the syntax highlighting of java.

Let's write a "hello world" java program to see the syntax highlighting of vim.

You can see that vim has a good syntax checking function because keywords are marked with different colors; vi has three modes:

(1) General mode: enter the default mode of vi, which can be copied, pasted, deleted, etc. (2) Editing mode: press'i' from the general mode to enter; (3) Command line mode: press ":",'?','/ 'from the general mode to enter.

Note: if you install vim and use the vim editor after entering vi, we see "alias vi=vim" after entering alias

2. Vi/vim button

Common buttons for "General Mode":

The common key meaning of "General Mode" h to the left J up K l to the right 20h to the left 20 characters 20j to the left 20k up 20k 20l to the right 20 characters Page Up turn the page down the Home cursor moves to the leftmost end of the current line the End cursor moves to the right of the current voyage n [space] cursor moves n characters N [enter] cursor down N line G cursor to the last Line nG cursor move to the nth line gg or 1G cursor move to the first line / word look down word?word look up wordn if it is / word use It means to continue looking down. If it is used after word, it means to continue searching up. N if it is used after / word, it means to continue searching upward; if it is used after word, it means to continue searching downwards : n old m old windows newshock g from lines n to m, replace old with CTRL+Z in lines n to last, replace old with CTRL+Z in newu similar to windows Previous step x delete a character X forward delete a character 3x delete 3 characters 3x delete 3 characters forward dd delete current line 3dd delete current line start 3 lines yy copy current line 3yy copy current line start 3 lines p paste to the next line of the current cursor P paste to the previous line of the current cursor CTRL+r redo the previous operation

Show details

Common buttons for Command Line Mode:

The common key meaning of "command line mode" is: W save: Q exit vim:wq save and exit: wq! (where permissions can be converted) force save and exit: Q! Directly exit without saving: W filename save as filename:n,m w filename save lines n to m as filename:set nu display line number: set nonu does not display line number:! Command temporarily leaves vim and executes command. After execution, enter vim:r filename to read the data of filename file into the current file: set all displays the current vim environment configuration.

Show details

III. The recovery mechanism of vim

When vim is editing a file, .file.swp exists at the same time, which is used to temporarily store and help restore the contents of the file. When vim closes the file normally, the .filename.swp file disappears.

We see the figure showing two reasons for this interface:

(1) multiple people edit this file at the same time: because Linux is a multi-user operating system, two people may log in and edit the file at the same time. If An enters the system and starts editing the 1.txt file, it will appear .1.txt.swp. When A has not finished editing but B also wants to edit it, because there is .1.txt.swp in the directory of this file, the interface shown above will appear. (2) the file is closed abnormally.

The last line in the figure illustrates several actions we can take at this time:

(1) o: open as read-only; (2) R: restore, that is, restore from the swp file, but the swp file will not be deleted when vim is closed and needs to be deleted manually; (3) d: delete the swp file; (4) Q: exit vim; IV, vim record and default configuration file

The / etc/vimrc file is a global vim configuration file.

1. If we use vim with our xiazdong account, there will be a .viminfo file in / home/xiazdong, which is used as a log of vim to record what files the user opened and what they did with vim. 2. If your xiazdong account wants to configure the default environment for vim, you can create .vimrc in / home/xiazdong and configure some features, such as displaying line numbers, syntax checking, etc.

The following configurations are generally used:

Set hlsearchset backspace=2set autoindentset nuset rulerset showmodeset bg=darksyntax on V. Visual Block function

The Visual Block feature can be used for block replication.

(1) CTRL+v: start copying blocks; (3) y: copy blocks; (4) p: paste blocks; (5) d: delete block VIM usage method of VIM VI, edit multiple files at the same time

The advantage of multi-file editing is the ability to yy some of the contents of one file, p to another file. Vim file1 file2 can edit two files at the same time, but only one file content appears on the screen at the same time, which needs to be switched.

(1): n: switch down; (2): n: switch up; (3): files: list all files currently edited; 7. Multi-window editing

As shown below: an interface can display the contents of multiple files.

(1): sp: open the current file; (2): sp filename: open the filename file; (3) CTRL+w+ ↓: switch the cursor to the next window; (4) CTRL+w+ ↑: switch the cursor to the previous window; (5): Q: close the window where the cursor is located; VIII.

There may be garbled problems, the solution: keep the terminal's character encoding consistent with the file's character encoding! Tty1~tty6 does not support the display of Chinese, so there must be garbled codes when displaying Chinese in tty1~tty6. Set the character encoding of the terminal.

Transcoding command: original encoding of iconviconv-f file-new coding of t file filename-o newfilename for example, a file with big5 encoding: 1.big5, a file to be converted to utf8: 2.utf8, then iconv-f big5-t utf8 1.big5-o 2.utf8 reads here, this article "what are the basic uses of vim" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it. If you want to know more about the articles, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report