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

Operation introduction of linux vim

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article introduces the relevant knowledge of "introduction to the operation of linux vim". In the course of the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

V1. The default character encoding is utf8, open the file encoded by gb2312, the Chinese is all garbled, how to restore the normal display?

Enter edit + + enc=cp936

V2. How do I execute commands in a vim system?

Enter! cmd

V3. Use vim to directly open and edit the. * gz file without decompressing, reading, editing, and then compressing.

Compressed files are supported through plugin

Vim ls-lrR.txt.gz

V4. How to support syntax highlighting in Vim?

Once and for all, add the following statement to ~ / .vimrc

Syntax on

Temporary method, enter the command: syntax on

V5. How Vim can quickly switch between structured documents, such as C language {} blocks.

Enter between {}, just enter%

V6. How can I get Vim to write down my editing status so that I can get back to work quickly next time?

Input: mksession file_name_your_session

V7. How to get Vim to tell you the number of bytes, words, and current location information of the edited file?

Enter g CTRL_G

V8. Enter help, open the help window, how to close quickly?

Enter CTRL-W, then enter CTRL-Q

Or enter CTRL-W Q

V9. I want to edit the log every day. I don't want to enter the date and time. What should I do?

Now we want to have a date like this: Mon Sep 19 23:19:03 CST 2005, simple, defined

A shortcut, F2, to do such a mapping in vim

: map gg:read! date

V10. How to use Vim to change the encoding of a text file?

For example, if you want to change a document that was originally encoded by utf-8 to cp936, you can use the following instructions

: set fileencoding=cp936

Then save the file: W

V11. How do I remember my configuration options (such as default encoding, file type, etc.) for vim?

Please write your configuration commands to the ~ / .vimrc configuration script file

V12. How do I view all the include file paths of the currently edited Candlespace + source files?

Input: checkpath

V13. How do I save the global configuration selection (available to all users)?

First enter: version to view the value of the system vimrc configuration file

Then edit the system vimrc configuration file and add the commands you want vim to execute

V14. How to quickly copy a line of text in Buffer?

Enter yy to record to the buffer first, move the cursor to the place you want to paste, and press p

V15. How to use macro commands to generate a sequential list of numbers?

1. Create the first list entry, make sure it starts with a number.

2. Qa-start recording into buffer 'a'

3. Y-yank the entry

4. P-put a copy of the entry below the first one

5. CTRL-A-increment the number

6. Q-stop recording

7. @ a-repeat the yank, put and increment times

V16. How to Fold the text?

Automatic Fold, assuming your file is a C source file, enter

: set foldmethod=indent

: set foldenable

Manual Fold, select the line you want to fold, in Normal mode

Enter the command zf

When the Fold is created, move the cursor to the line where Fold is located

Enter zo to open the appropriate Fold

Zc, close the corresponding Fold

ZM, close all Fold in the file

ZR, open all Fold in the file

Za, automatically turn on or off the corresponding Fold

V17. How do I view the number of words and characters in the current file?

Enter g CTRL-G, and all bytes are counted as UTF-8.

V18. How to quickly search for the currently selected text in Visual mode?

In order not to change the user's habits, we still use the keys * and #

Vnoremap * y / "

Vnoremap # y? "

Note: the instruction inserts the contents of the named register

V19. How do I jump back and forth between different blocks / paragraphs in the source code?

Enter {, advance to the previous paragraph

Enter} to proceed to the next paragraph

V20. How to convert TAB in text to fixed-length spaces?

Input: set expandtab

Input: set tabstop=4

Input: retab

V21. How does the data in the paste clipboard in vim keep it in its original format without indentation?

When paste, most of the input text will contain newline characters, as well as some special characters, vim will automatically indent the text, and

And automatic line wrapping, etc., in order to avoid the influence of these intelligent options, you can turn on the paste switch.

V22. A few interesting features

Gg to the beginning of the file

Gd to variable declaration

Gg=G full text indent automatically, = G single line indent

K (shift+k) man

Crl+] function prototype at crl+t return (ctags)

Ctl+p automatic completion (edit status)

X encrypted save (to enter password)

Instructions for use:

In the following example, xxx indicates that you enter xxx and enter in command mode

In the following example: xxx means to enter xxx and enter in extended mode

The command in parentheses indicates the relevant command.

Commands entered in edit mode or visual mode are noted separately.

1. Find

/ xxx (? xxx) means to search the entire document for a string that matches xxx, / to look down,? It means to look up. Among them, xxx can be a regular expression, so let's not say much about it.

Generally speaking, it is case-sensitive, and if you want to be case-insensitive, you have to type

: set ignorecase

After finding it, type n to find the next match, and enter N to look in the opposite direction.

* (#) when the cursor hovers over a word, enter this command to find a match

The next (top) word. Similarly, enter n to find the next match, and enter N inverse

To look for.

G * (g #) this command is similar to the previous command, except that it does not exactly match the word where the cursor is located.

Is to match all strings that contain the word.

Gd this command finds the word that matches the word in which the cursor is located, and hovers the cursor over the non-

Where the word appears for the first time in the commentary paragraph.

This command looks for anti-parentheses that match where the cursor is located, including () [] {}

F (F) x this command means to look for the first x character on the right (left) side of the cursor.

When found:

Enter; means to keep looking.

Input to indicate a search in the opposite direction

two。 Move the cursor quickly

In vi, moving the cursor and editing are two different things, and because of the distinction, it is very convenient to calibrate the light.

Bits and editors. So it's useful to move the cursor a little faster.

W (e) move the cursor to the next word.

B move the cursor to the previous word.

Move the cursor to the beginning of the line.

Move the cursor to the character at the beginning of the line.

Move the cursor to the end of the line.

H move the cursor to the first line of the screen.

M moves the cursor to the middle line of the screen.

Move the cursor to the end of the screen.

Gg moves the cursor to the first line of the document.

G move the cursor to the end of the document.

Cmurf (that is, ctrl key and f key are pressed together) this command is page down.

This order is called page up. C ctrl is pressed together with b key.

"this command is quite useful in that it moves the cursor to the previous mark, such as gd, *, etc.

After finding a word, enter this command to return to the last stop.

'. This command works pretty well, moving the cursor to the last modified line.

`. This command is so powerful that it moves the cursor to the last modification point.

3. Copy, delete and paste

In vi, y means copy, d means delete, and p means paste. Where copy and delete is a command with cursor movement

Combined, you can understand it by looking at a few examples.

Yw means to copy the content from the current cursor to the end of the word in which the cursor is located.

Dw means to delete the content from the current cursor to the end of the word in which the cursor is located.

Y 0 means to copy the contents from the current cursor to the beginning of the line where the cursor is located.

D 0 deletes the content from the current cursor to the beginning of the line where the cursor is located.

Y $indicates that the content is copied from the current cursor to the end of the line where the cursor is located.

D $deletes the content from the current cursor to the end of the line where the cursor is located.

Yfa means to copy from the current cursor to the first a character after the cursor.

Dfa means to delete the content from the current cursor to the first a character after the cursor.

Specifically:

Yy indicates the line where the copy cursor is located.

Dd indicates to delete the line where the cursor is located.

D means to delete the content from the current cursor to the end of the line where the cursor is located.

The complex use of copying, deleting, and pasting is related to registers and can be queried by yourself.

4. Numbers and commands

In vi, combining a number with a command often indicates a repetition of the command, or a line if it appears at the beginning of the extended mode

Number is located. Such as:

5fx indicates the fifth x character after the lookup cursor.

5w (e) move the cursor to the next five words.

5yy means to copy 5 lines below the cursor.

5dd means to delete 5 lines below the cursor.

Y2fa means to copy from the current cursor to the second a character after the cursor.

12pr 24y means to copy the contents between lines 12 and 24.

12 ~ # means copy the contents between line 12 and the line of the cursor.

24y means to copy the contents between the line of the cursor and line 24. Delete similar.

5. Fast input character

In vi, you are not required to enter every character, there are many ways to enter some characters quickly.

Students who use linux/unix must have an experience of typing the first few characters when typing a command under the command line and then pressing

The TAB system will automatically complete the remaining characters and print them if there are multiple matches. That's the famous order.

Completion (in fact, there is also a file name completion function in windows). There are many string completion commands in vi, which are very convenient.

In edit mode, enter a few characters and then enter this command, then vi starts to search up (down).

Start with the matching words and complete them. Keep typing this command and look it up in a loop. This command

Matches will be made in all files opened in the vim program.

C-x-l in edit mode, this command quickly completes the entire line, but only in this window

Match in the document.

C-x-f in edit mode, this command completes the file name. Such as entering:

Enter this command after / usr/local/tom, and it will automatically match:

/ usr/local/tomcat/

Abbr is an abbreviation. This is a macro operation, and one abbreviation can be used instead of another in edit mode

A string. For example, people who write java files often enter System.out.println, which is very

It's troublesome, so abbreviations should be used to reduce typing. You can do this:

: abbr sprt System.out.println

Later, after entering sprt, and then enter other non-alphabetic symbols, it will automatically expand to System.

Out.println

6. Replace

Substitution is a strength of vi because regular expressions can be used to match strings. Here are a few examples.

: s/aa/bb/g replaces aa with bb in all strings containing aa that appear on the line of the cursor

: s /\ / bb/g replaces all aa on the line of the cursor with bb, replacing only the word aa

:% s/aa/bb/g replaces aa with bb in all strings containing aa that appear in the document

: 12 aa 23s an aa BBG replace aa with bb in all strings containing bb that appear from lines 12 to 23

: 12Metro 23s / ^ / # / add # characters to the beginning of lines from 12 to 23

:% s = * $= remove any extra spaces at the end of all lines

: G / ^\ sblank planner d removes all blank lines that do not contain characters (spaces).

7. Multi-file editing

It is convenient to open many files for editing in a vim program.

: sp (: vsp) filename vim splits a horizontal (vertical) window and opens a new file in that window.

Starting with vim6.0, the file name can be the name of a directory, so that vim will

Open the directory and display a list of files. Press enter on the file name and type in this window.

Open the file, if you enter O, open the file in a new window, enter? You can see it.

To help information.

: e File name vim will open a new file in the original window. If the old file has been edited, it will be asked to save.

What if c-w-w vim splits several windows? Enter this command to cycle through the cursor

To the various windows.

: ls this command to see how many files this vim program has opened, at the bottom of the screen

The following data is displayed:

1 a "usevim.html" line 162

2 # "xxxxxx.html" line 0

Where:

1 represents the serial number of the open file, which is very useful.

% a represents the file code,% represents the file currently edited

# indicates the last edited file

"usevim.html" represents the file name.

Line 162 indicates the position of the cursor.

: B serial number (code) this command opens the file with the specified serial number (code) in this window, where the serial number (code)

It is seen with the: ls command.

: set diff this command is used to compare two files, you can use the

: vsp filename

Command to open another file, and then enter this command in each file window, you can see

It's working.

8. Macro replacement

Vi can not only replace text with abbr, but also macro definition of commands. Some orders are hard to lose.

So I define them on -, which is very convenient. These configurations can be pre-written to ~ / .vimrc

($VIM/_vimrc under windows), you don't have to write the preceding colon when you write it in.

: nmap: nohls removes the highlight of the searched string

Transfer the cursor to a different window in nmap w command mode

: run in imap input mode

: nmap:% s = * $= = remove all extra spaces at the end of the line.

: imap ditto

In: java: (note, why do you say java here? because the following definitions do not work on other file formats, the following

Will talk about how to achieve this)

Nmap: comp javac:mak-d. %

This command compiles the java file with javac and automatically locates the cursor to the error point. But it needs to be decided.

A javac.vim file is under $VIM/compiler, but there are only two lines in javac.vim:

Setlocal makeprg=javac

Setlocal errorformat=%A%f:%l:\% mdepartment%-Z% p ^,%-C%.%

: nmap: comp ant:mak

This command compiles the java file with ant and automatically locates the cursor to the error point. In general, install

You already have the compiler/ant.vim file after vim, so this command can be used directly. But I need it.

There are build.xml files in the current directory, and of course ant must be installed.

: nmap: cl this command is used to view all compilation errors.

: imap

: nmap: cc this command is used to view current compilation errors.

: imap

: nmap: cn this command is used to jump to the next error location.

: imap

: nmap: cp this command is used to jump to the previous error location.

: imap

: nmap: JavaBrowser

This command is used to split a new window on the left side of the window, which contains the resource tree of java, including

The class, class member variables and member methods that appear in this file are just like JCreator.

Enter it in this window? You will see help. Hey, hey, it's easy to use, but you need ctags support.

: imap

9. TAB

TAB is a tab, and take it out to make a section because it is really useful.

Enter this command and the cursor line moves one tab to the right.

Enter this command and move the cursor 5 lines back to the right one tab.

This command moves 12 to 14 rows of data one tab to the right.

This command moves 12 to 14 rows of data two tab to the right.

So how do you define the size of tab? Some people are willing to use 8 spaces, some use 4, and some use 2.

Some people want tab to be completely replaced by spaces, while others want tab to be tab. It doesn't matter. Vim can.

Help you. The following settings are generally written into the configuration file first to avoid constant typing.

Set shiftwidth=4 is set to indent 4 spaces automatically, of course, it should be indented first.

Set sts=4 sets softtabstop to 4. 0. After typing tab, I jumped 4 frames.

The actual tab of set tabstop=4 is 4 spaces instead of the default 8.

Set expandtab after typing tab, vim populates the tab with the appropriate spaces.

10. Autocmd

This command is very powerful and can be used to apply different configurations to different file formats; you can

Automatically add a copyright notice when creating a new file, etc. These commands are generally defined in configuration files such as ~ / .vimrc

Inside. Because he is very powerful, so I can not give a very specific explanation, can only give a few examples, please see help for details.

: autocmd! Delete all previous automatic commands.

Autocmd FileType java source / .vim/files/java.vim

Autocmd FileType java source / .vim/files/jcommenter.vim

The above two commands let me apply the two configuration files mentioned later when I open the java file.

Autocmd BufNewFile * .java 0r ~ / .vim/files/skeletons/java.skel

The above command allows me to automatically add the contents of the java file when I create a new java.skel file.

Autocmd BufNewFile * .java normal gnp

The above command allows me to automatically run the gnp command when I create a new java file, which makes some specialization

Processing, such as replacing _ _ date__ in the new java file with today's date.

11. Common script

In vim.sf.net you can find many scripts (script), which often have unexpected effects.

The ones I often use are:

Jcommenter.vim automatically adds javadoc-style comments.

JBrowser.vim class resource browsing. C, C++ and so on can use Tlist

There are many useful things, for example, checkstyle.vim can test your programming style, and jad.vim can directly

Decompile .class files and so on.

twelve。 Common configuration

You often need some personalized configuration in the ~ / .vimrc configuration file. For example, some of the macro definitions written above, some

Autocmd definition and so on. For example:

Set suffixes=.bak,~,.o,.h,.info,.swp,.aux,.bbl,.blg,.dvi,.lof,.log,.lot,.ps,.toc

In this way, when you open a file in vim, it will ignore the above file when pressing tab to complete the file name.

Set nu displays line number

Set ai setting automatic indentation

Map Y Y $makes Y and D the same, otherwise Y means the same as yy.

13. Other

There are also many interesting orders, recorded here so as not to be forgotten.

. Repeat the last editing command.

: G / ^ / exec "s / ^ /" .strpart (line ("."). ", 0,4) insert the line number at the beginning of the line

: runtime! Syntax/2html.vim converts txt to html, which will be converted according to your color configuration.

This is the end of the introduction to the operation of linux vim. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Servers

Wechat

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

12
Report