The principles and ways to improve the efficiency of vi/vim use quickly

Source: Internet
Author: User



Vi/vim is the editor that is provided by default for all Unix/linux operating systems. Due to its powerful functions and efficient operation, Vi/vim has become one of the many editing tools that unix/linux users and administrators must master and use skillfully. Especially in the absence of a graphical interface, it is inseparable from the Vi/vim. Vi/vim command is very many, the usage is extremely flexible, grasps has certain difficulty. In this paper, the author will combine his own experience, in seven ways to highlight the principles, approaches or commands can quickly improve the efficiency of Vi/vim file editing, so as to achieve a multiplier effect.


Introduction


Vi was originally implemented by Bill Joy in the 1976 code. Vim (vi improved) is an improved VI, developed and released by Bram Moolenaar in 1991. Vi/vim is the editor that is provided by default for all Unix/linux operating systems. Due to its powerful functions and efficient operation, Vi/vim has become one of the many editing tools that unix/linux users and administrators must master and use skillfully. Especially in the absence of a graphical interface, it is inseparable from the Vi/vim. Vi/vim command is very many, the usage is extremely flexible, grasps has certain difficulty. There are many articles about how to use vi/vim in detail, so this article does not describe the many functions and commands of Vi/vim. In this paper, the author will combine his own experience, in seven ways to highlight the principles, approaches or commands can quickly improve the efficiency of Vi/vim file editing, so as to achieve a multiplier effect. The approach or command described in this article is only for Vi/vim of the system default configuration. About Vi/vim the various customization functions are not included in the scope of this article. ( Note: all of the commands mentioned in this article were tested on Red Hat Enterprise Linux Server release 6.1.) )


Selection of the Vi/vim version


"Workers must first sharpen their tools". In the Vi/vim version of the choice, the principle is " can not use the Vim of Vi, can use the latest version of the old version should not be kept." Vim provides much more functionality and features than Vi, such as syntax highlighting and coloring functions. For the use of effectiveness and efficiency, editing the same document, using Vim better, in the case of the version, the newer version will often fix some of the shortcomings and shortcomings of the old edition. This requires us to use the latest version of Vim where possible.



Tips:



Under Linux, if you log in to the system as the root user, the Vim editor opened with the VI command will often only load the most basic functions, such as the function of syntax highlighting coloring is basically not. The trick to using Vim in the root user is to open the Vim editor with the VIM command.


Start and close Vi/vim


There are many ways to turn vi/vim on and off, either by starting the Vi/vim editor itself or by opening one or more files at the same time you start the Vi/vim editor, either by discarding the disk or by saving only part of the file. The following is a list of related commands:


Table 1. Basic commands for starting and closing Vi/vim
function command description
start VI open VI/VIM
VI <file> open vi/vim and load file <file>
 
exit ZQ unconditional exit  
: q! unconditionally exit  
ZZ disk and exit  
: Wq Save and exit
 
Save part of the file : M,NW <file> Save the contents of m rows to n rows to file <file>  
: m,nw >> <file> move m rows to n rows The content is added to the end of file <file>
 


Mastering and proficiency in these basic commands is the basic requirement for using Vi/vim. Only in this way, in the actual use of the process to achieve on-demand choice, flexible use, improve the use of VI/VIM efficiency. One point to note is that when using Vi/vim, sometimes you want to temporarily quit Vi/vim, go to the shell environment to do some operations, and so on, and then continue to return to the Vi/vim state. For this often-encountered reality demand, many people in most cases will save exit Vi/vim, enter the shell to perform the action, and then reopen the file you just edited. In fact, this is a very inefficient method, because again open need to reposition the place just edited, trouble not to say, Vi/vim editor state is completely different. In fact, there are two ways to implement a temporary exit Vi/vim, enter the shell environment and then return to the requirements:



Method One: Use Ctrl-z and FG for both command combinations.



This solution mainly utilizes the operating mechanism of Linux/unix. The specific principle is: The Ctrl-z command puts the current vi/vim process into the background, then the shell environment can be used for you, the FG command will be located in the background of the Vi/vim process in the foreground execution, so we re-enter the Vi/vim interface and revert to the original editing state.



Method Two: Use the line command: Sh.



Enter in normal mode of Vi/vim : Sh to enter the Linux/unix shell environment. Enter the exit command when you want to return to the Vi/vim editing environment.



The implementation mechanism of these two methods is not necessarily, but the same effect, are very fast and effective.


Move cursor


There are many commands for cursor movement in Vi/vim, which is one of the most confusing and poorly ordered places for many people. In fact, many of the commands in Vi/vim are designed for different operating units, and different commands correspond to different operating units. Thus, when using the command to operate, the first thing to be clear is what kind of operation unit, that is, to manipulate a character, a sentence, a paragraph, or to operate a line, a screen, a page. Commands are different for different units. As long as the unit is chosen properly, the order is natural and the operation is naturally and quickly efficient; otherwise, it can only be time consuming and laborious. This can also be said to be the best embodiment of Vi/vim superior to other places of the editor, but also vi/vim some people love to hate one place. After the operation unit is determined, the number of operations is the number of times that the command is executed repeatedly. To correctly and efficiently use Vi/vim's various operations, we must grasp this principle: the first fixed unit re-quantification . The scope of the operand is calculated as: Operation range = number of operations * operating unit. For example: The 5h command moves left 5 characters, and the 8w command shifts 8 words to the right.



Note: Some operating units, such as files, cannot be added. Please refer to the Vi/vim User manual for specific instructions.



There are many operating units in the Vi/vim, in order from small to large (the corresponding action command in parentheses): Characters (H, l) → Words (w, W, B, B, e) → lines (j, K, 0, ^, $,: n) → sentences (()) → paragraph ({,}) → screen (h, M, L) → Page (ctrl-f , Ctrl-b, ctrl-u, ctrl-d) → file (G, GG,: 0,: $).



The specific commands are explained as follows:


Table 2. Basic commands for moving the cursor
Operating Unit Command Description
Character H move one character to the left
L Move Right One character
Words W/W move to the beginning of the next word
B/b move to the beginning of the previous word
e/e move to the end of the word where the cursor is located
The word w, b, and E commands are strings separated by whitespace characters (spaces, tab), such as the string "Str1-str2 STR3-STR4", which is two words for the W, B, e commands, and four words for W, B, and E commands.
Yes J Move down one line
K Move up one line
0 move to the beginning of the current line
^ Move to the first non-null character of the current line
$ move to the end of the current line
: N Moves to nth row
0 is the number 0 (zero)
Sentence ) moves to the end of the current sentence
(move to the beginning of the current sentence
Paragraph Move the end of the current paragraph
{Move to the beginning of the current paragraph
Screen H moves to the first line of the screen
M moves to the middle line of the screen
L move to the last line of the screen
Page CTRL-F Scroll forward one page
Ctrl-b Scroll Backward one page
Ctrl-u rolling forward Half page
ctrl-d Scroll Backward Half page
File G move to end of file
GG move to the beginning of the file
: 0 Move to the first line of the file
: $ move to last line of file
0 is the number 0 (zero)


In addition to these basic units, there is a %(jump to the matching parentheses),'. (Jump to the recently modified location and position the edit point),'. (Jump to the recently modified position without positioning the edit point) These three commands are also very important, and flexible use in vi/vim can greatly improve efficiency. % in addition to the cursor movement, it can also be used to detect the source of the various parentheses matching situation.


Text editing


As with the cursor movement, the commands for editing in Vi/vim are also more, but the operating units are much less than the moving cursor. In order from small to large (the corresponding action command in parentheses): characters (x, C, S, R, I, a) → Words (CW, CW, CB, CB, DW, DW, DB, db) → lines (dd, D0, d$, I, A, O, O) → Sentences ((,)) → paragraphs ({,}). Some of these operating units can be added to the number of operations. The scope of the operand is calculated as: Operation range = number of operations * operating unit. For example: d3w command Delete three words, 10dd command Delete 10 lines.



The specific commands are explained as follows:


Table 3. Basic commands for text editing
Operating Unit Command Description
Character X Delete the character at the cursor position
C Change the current character and enter insert mode
s replaces the character at the cursor position and enters insert mode
R replaces the character at the cursor position but does not enter insert mode
I enter insert mode before the character in the current position
A enters insert mode after a character in the current position
Words CW/CW Delete the current word from the beginning of the cursor and into insert mode
CB/CB Delete the current word from the position of the cursor to the beginning of the word and into insert mode
DW/DW Delete the current word from the beginning of the cursor but not into insert mode
db/db Delete the current Word from the cursor position to the beginning of the word but not into insert mode
CW, CB, DW, DB command operation words are separated by white space characters (spaces, Tab) strings, such as the string "Str1-str2 Str3-str4", for the cw,cb,dw,db command is two words, and for CW, CB, DW, DB command is four words.
Yes DD Delete When moving forward
D0 delete content from the current cursor to the end of the line
d$ Delete content from the current cursor to the end of the line
I Enter insert mode at the beginning of the current line
A enters insert mode at the end of the current line
O Enter insert mode with another line below the current line
O Enter insert mode on another line above the current line
  • 0 in the D0 command is a number zero
  • O is a lowercase english letter [? U]
  • O uppercase English letters [? U]
Sentence d) Delete the contents of the current sentence from the cursor position to the end of the sentence
D (delete the contents of the current sentence from the cursor position to the beginning of the sentence
Paragraph D} Delete the current paragraph from the cursor position to the end of the paragraph content
d{Delete the contents of the current paragraph from the cursor position to the beginning of the paragraph


In addition to the most basic text editing commands mentioned above, Vi/vim also provides many other editing commands or related combinations. Using these commands or related combinations tends to greatly improve the efficiency and speed of text editing. These commands are now listed under functions as a table:


Table 4. Efficient commands for text editing
function Command Description
Copy and paste YW copy the current Word from the beginning of the cursor section
yy copies all characters of the line that the cursor is in
P places the last deleted or copied text in the current character
P places the last deleted or copied text before the current character
Work with operands to quickly copy and edit text
Undo and Redo U undo Change
Ctrl-r Redo Changes
A very practical command.
Repeat operation . Repeat Last Action
  • . To a decimal point (dot)
  • Use with cursor move command;
  • Do the same thing in different places without having to re-enter the previous complex commands, sort of like MS Office's format Painter
Exchanging adjacent characters or rows XP swaps the character of the cursor position and the character on its right
DDP swaps the line at the cursor position and its next row
Uppercase and lowercase conversions ~ Convert the case of letters under the cursor backwards
Guw the word where the cursor is located to lowercase
Guw the word where the cursor is located to lowercase
Guw to capitalize the word where the cursor is located
Guu all characters in the line where the cursor is located lowercase
GUU all characters in the line where the cursor is located are capitalized
G~~ the line where the cursor is located all character case reversal conversions
Get External input : r!<cmd> the output of command <cmd> into the current cursor position
: R <file> File <file> read into the current cursor location
Sort : 1,$!sort to sort all content within a file
Add line number :%!NL Add line number before all non-empty lines
:%!nl-ba Add line number before all lines
Implemented using the Linux command nl.
Indent in >> Right indent (can be used with operands)
<< left indent (can be used with operands)
With the operation of the number of use, when editing the source code is very useful.
Auto-complete Ctrl-p Auto-complete It's very useful when writing code. For example, pressing CTRL-P after entering Prin will automatically help you enter the part of the printf function name, and the relevant alternate function is listed below the bottom.
Show current Edit file name CTRL-G display the currently edited file name and number of rows Information about the current edit file can be learned without exiting Vi/vim
Show character code inside GA Displays the inner code (including decimal code, hexadecimal code, and octal code) of the character of the cursor The inner code displayed is the inner code under the current encoding
Text Search and replace


The following three basic commands are the main vi/vim for text search:


Table 5. Basic commands for Text search
function Command Description
Search /forward Search in file
? Search backwards in a file
  • Regular expressions can be used
  • can be used with operands, such as 3/str to search forward the string str and move the cursor to the third found string
Search for the next N Search for the next
N Reverse Search Next
  • Regular expressions can be used
  • can be used with operands, such as command 3N to reverse search for a third matching string


In addition to these three basic commands, there are three very efficient and fast commands related to search lookups:


Table 6. Efficient command for Text search
function Command Description
Quick Search * Search forward in the file for the word that the current cursor is in
# Search backwards in the file for the word that the current cursor is in
Very fast Search commands
Show Search Command History q/Display the Search command History window
Q? Window showing history of search commands
You can choose to reuse a previously used search lookup command
Find Help Shift-k find the man help for the command or function where the cursor is located
  • You can quickly query the use of commands or functions without exiting Vi/vim;
  • Press the Q key to exit the man help


The replacement master is implemented with a search using the line command, with the following format:



: m,ns/str1/str2/g replaces the string str1 in rows m through n with the string str2.



Among the many commands for replacing with regular expressions, one command you usually need to master is how to quickly remove ^m characters. When you edit a file that is generated in the Windows operating system in the Linux/unix system, if you upload a carriage return with a newline character that is not handled correctly, the ^m characters will often appear with Vi/vim open. If the uploaded file is a shell script, the script will not run even if the correct execution permissions are given. This is also one of the most common issues where files are transmitted in Linux/unix and Windows. In fact, using the Replace command in Vi/vim:1, $s/^m//g can easily remove ^m characters quickly. It is important to note that ^m is entered using CTRL-V, ctrl-m.


Execute external command


In Vi/vim, you can also execute various external commands in normal mode, with the following command format:


Table 7. Execute external command format
function Command Description
Execute external command :!<cmd> Executing external commands <cmd> Enter this command in normal mode
Show command-line command history Q: Window showing command-line command history You can choose to reuse previously used command-line commands


where command Q: Displays the history of the used row commands, from which you can choose to reuse previously used commands. This is fast and easy for those complex commands that need to be reused.


Vi/vim settings


Vi/vim has many internal variables that can be set as needed. Variable types are often set differently, simply by setting a specific variable name, and complex, you need to specify and assign an explicit value to set the variable. In practical applications, please refer to the VI/VIM manual if necessary. Here are some of the setup commands that you often use and can improve your editing efficiency:


Table 8. Vi/vim Settings command
function Command Description
View the current value of the setting : Set All to view the current value of all options set in VI or Vim  
: Set <option>? View specific Options <option& Gt The current value of
 
Sets the line number to display or not : Set number displays line numbers  
: Set no number suppress line numbers display
shorthand for the command:
: Set nu 
: Set no nu
Set auto indent : Set autoindent sets auto indent  
: Set No autoindent Cancel Auto indent Setting the shorthand for the
command:
: Set AI
: Set no AI
Set indent width : Set SHIFTW idth=4 Set indent width to 4 command shorthand:
: Set sw=4
set case ignore or not : Set ignorecase set Ignore size   ;
: Set no ignorecase de-ignore size setting
shorthand for the command:
: Set IC
: Set no IC
set invisible character display : Set list displays invisible characters  
: Set Nolis T suppress invisible character setting
when invisible characters are displayed, the TAB key is displayed as ^i, and $ is displayed at the end of each line.


In normal mode, these settings commands only modify the settings of the current session, and these settings are lost when you exit or restart Vi/vim. To keep these settings, you must write these settings to the Vi/vim VIMRC file. For the Linux/unix operating system, each user's VIMRC file is located in the user's home directory, and the file name is. vimrc. The Vi/vim will read the VIMRC file in the user's home directory each time it is started and set the VI/VIM usage environment accordingly. Vi/vim also installs an example of the VIMRC file Vimrc_example.vim to the/usr/share/vim/vim<version> directory at the same time as the installation. You can copy this sample file to the current user's home directory and rename it to. VIMRC, as needed, and it is relatively easy to modify it on this basis. Other related settings such as custom and key mappings for Vi/vim are basically written to the user's VIMRC file, please refer to the Vi/vim manual for more detailed information. Users can also maintain a VIMRC file of their own, and copy this file to their own use of the environment, to maintain the vi/vim characteristics in different environments consistent with their own usage habits.





Conclusion


This paper mainly introduces the ways to improve the use efficiency of vi/vim and the related commands, and mastering these is the basic requirement of efficient use of vi/vim. There are many other commands and custom features that are not covered in this article about Vi/vim. We can combine their own use situation and demand, on the basis of this article to expand, and constantly improve their vi/vim use level.


Blogger Supplement


DT) has been removed to this line)



g* Find all words that contain the current word (* Find only words that exactly match the current word)



Ctrl+e scroll up, the cursor position does not move



Ctrl+y Scroll down, the cursor position does not move



A: Insert at end of bank line
J: Can remove line breaks between the bank and the next line, that is, the next line is renewed to the end of the bank
~: The character of the cursor at the same place is swapped in case



: Ex Opens the Directory browser, can browse all the files in the current directory, and can choose



: F File to rename window




VI Open multiple Files
VI a B C
: N jumps to the next file, you can also specify the file to jump directly, such as: N C, you can jump directly to the C file
: e# back to the file you just edited



Vim Replication Tips



Http://stackoverflow.com/questions/2023015/vim-yanking-range-of-lines



81gy91g or 81ggy91gg Copy from 81 lines to 91 rows



Or



Ma



Mc



' ay ' c copy from label A to label C



Or



: 81,91 CO 100


Resources
    • Access DeveloperWorks technical topics AIX and UNIX, read Aix/unix/linux system management and related software applications, expand your aix/unix/linux skills.
    • View the article "Vim Practical Technology, part 1th: Practical Tips" to learn about Vim's five operating modes.
    • View the article "Dialogue UNIX: The new improved Vim editor" to learn about the differences between Vi and Vim and VIMR basic commands.
    • View the article "magical vim" to learn about Vim's powerful text processing capabilities and related commands or scripts.
    • See the article "Vi Tips and tricks: Impressive 10 cool Commands" to learn about Vim's tips and tricks.
    • View the article "Learning linux,101: Using VI to edit files" to understand the basic use of VI.
    • Find out more about our most popular articles and tutorials in the DeveloperWorks Linux zone for more reference materials for Linux developers, including beginners for Linux.
    • Check out all Linux tips and Linux tutorials on the developerWorks.
    • Stay tuned for DeveloperWorks technical activities and webcasts.


Article Source: Https://www.ibm.com/developerworks/cn/linux/l-cn-tip-vim



The principles and ways to improve the efficiency of vi/vim use quickly


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.