Common commands for VIM for Linux applications

Source: Internet
Author: User
Tags diff uppercase letter









Vim Select text, delete, copy, paste



The selection of text, for the editor, is a very basic thing, also often used, summarized as follows:



V starts at the current position of the cursor, where the cursor is selected, and then ends with a click of v.



V starts at the current line of the cursor, the line that the cursor passes through is selected, and then the end of the V is pressed.



CTRL + V starts at the cursor's current position, selects the rectangular area of the beginning and end of the cursor, and then presses CTRL + V to end.



GGVG selects all the text, where GG jumps to the beginning of the line, V selects the entire row, the end of G



When selected, you can edit them with edit commands, such as
D Delete



Y copy (default is copy to "register")



P Paste (default from "Register to remove contents paste")



"+y Copy to system Clipboard (i.e. Vim's + register)



"+p paste from the system clipboard



VIM Command Summary


1. Delete characters


To delete a character, simply move the cursor over the character and press "X".


2. Delete a row


Delete a whole line of content using the "dd" command. After deletion, the following line is moved up to fill the vacancy.


3. Remove line breaks


In Vim you can combine two lines into one line, meaning that the line breaks between the two lines are deleted: The command is "J".


4. Revocation


If you delete too much content by mistake. Obviously you can type it again, but the command "U" is easier, it can undo the last operation.


5. Redo


If you undo multiple times, you can also use Ctrl-r (redo) to reverse the undo action. In other words, it is an undo to undo. The Undo command has another form, the "U" command, which undoes all operations on one line at a time. The second time you use this command, the previous "U" action is undone. with "U" and ctrl-r you can retrieve any operation status.


6. Append


The "I" command can insert text before the current cursor.
The "a" command can insert text after the current cursor.
The "o" command can start another line below the current line and turn the current mode into insert mode.
The "o" command (note that the uppercase letter O) will be a row above the current line.


7. Using the command count


Let's say you want to move up 9 rows. This can be done with "kkkkkkkkk" or "9k". In fact, many commands can accept a number as the number of times the same command is executed repeatedly. For example, to add three exclamation marks at the end of a line, the command used was "a!!!". Another way is to use "3a!" Command. 3 indicates that the command will be repeated 3 times. Similarly, deleting 3 characters can be used with "3x". The specified number must precede the command it is about to decorate.


8. Exit


To exit vim, use the command "ZZ". This command saves the current file and exits vim.


9. Discard edits


Discard all changes and exit with the command ": q!". With ": e!" The command discards all modifications and re-loads the original contents of the file.


10. Move in Word


Use the "w" command to move the cursor forward one word's first character, such as "3w" to move the cursor forward 3 words. The "B" command moves the cursor back to the first character of the previous word.
The "E" command moves the cursor to the last character of the next word. Command "GE", which moves the cursor over the last character of the previous word. 、


11. Move to the beginning or end of the line


The "$" command moves the cursor to the end of the current line line. If you have a key on your keyboard, it works the same way. The "^" command moves the cursor over the first non-whitespace character of the current line. The "0" command always moves the cursor over the first character of the current line. The same is true for keys. The "$" command can also accept a count, such as "1$" moves the cursor to the end of the current line line, and "2$" moves to the end of the next line, and so on. The "0" command does not accept a count like this, and the command "^" is preceded by a count without any effect.


12. Move to the specified character


The command "FX" looks for the next character X (right direction) on the current line, and can be searched to the left with a command count "F" command. The "TX" command is the same as the "FX" command, except that it does not hover the cursor over the character being searched, but on a character before it. Tip: "T" means "to". The reverse direction version of the command is "Tx". These 4 commands can all be used ";" To repeat. The same command is repeated with "," but the direction is the opposite of the original command.


13. To match a parenthesis for the purpose of moving


Use the command "%" to jump to the parentheses that match the parentheses below the current cursor. If the current cursor is on (, it jumps forward to the ")" that matches it), it automatically jumps back to the matching "(" up) if it is currently on.


14. Move to the specified line


Using the "G" command to specify a command count, this command will position the cursor on the line specified by the command count. For example, "33G" will place the cursor on line 33rd. Without specifying a command count as a parameter, "G" positions the cursor on the last line. The "GG" command is a quick way to jump to the first line.
Another way to move to a line is to specify a command count before the command "%" such as "50%" will position the cursor in the middle of the file. "90%" jumps close to the end of the file.
The command "H", "M", "L", respectively, jumps the cursor to the first line, the middle row, and the End line section.


15. Tell your current location


Use the CTRL-G command. "Set Number" displays the line numbers in front of each line. Instead, close the line number with the command ": Set Nonumber". ": Set ruler" Displays the current cursor position in the lower-right corner of the vim window.


16. Roll Screen


Ctrl-u The window that displays the text scrolls up half-screen. The ctrl-d command moves the window down half-screen. Scrolling one line at a time can use CTRL-E (scroll up) and ctrl-y (scroll down). To scroll forward a full screen using the command ctrl-f. In addition Ctrl-b is its reverse version. The "ZZ" command puts the current line in the center of the screen, and the "ZT" command places the current line at the top of the screen, and "ZB" places the current line at the bottom of the screen.


17. Simple Search


The "/string" command can be used to search for a string. To find the next position of the last-found string, use the "n" command. If you know the exact location you're looking for is the first occurrence of the target string, you can also place a command count before "n". "3n" will look for the 3rd occurrence of the target string.
"?" The command works the same as "/", except that the search is in the opposite direction. " N "command repeats the previous lookup, but with the original"/"or"? " The specified search direction is reversed.
If the lookup ignores case, use the command "set ignorecase" to return the exact match with the command "set Noignorecase".


18. Find the next word in the text


Position the cursor over the word and press the "" key. Vim will take the word where the current cursor is located and search it for the target string. The "#" command isthe reverse version of "". You can also add a command count before these two commands: "* *" finds the third occurrence of word under the current cursor.


19. Find entire word


If you use "/the" to find Vim will also match to "there". To find the "the" as a standalone word, use the following command: "/the>". ">" is a special notation that only matches the end of a word. Approximately, "\<" matches at the beginning of a word. This way, the "the" as a word can be used: "/".


20. Highlight Search Results


Turn this feature on with ": Set Hlsearch" To turn this feature off: ": Set Nohlsearch". If you just want to get rid of the current highlight, you can use the following command: ": Nohlsearch" (abbreviated to Noh).


21. Match the beginning and end of a line


The ^ character matches the beginning of a line. The $ character matches the end of a line.
So "/was$" matches only the word was at the end of a line, so "/^was" matches only the word was at the beginning of a line.


22. Match any single character


. This character can be matched to any character. For example, "C.M" can match any previous character is C, the latter character is m, regardless of the middle character.


23. Match Special characters


Put a backslash in front of the special character. If you look for "ter." ", with the command"/ter\. "


24. Using tags


When you use the "G" command to jump from one place to another, Vim remembers where you took the jump. This position is a marker in vim. Use the command command"It can make you jump back to the starting point。to jump back and forth between two points. The Ctrl-o command jumps to the position where you stopped the cursor earlier (hint: o means older). Ctrl-i jumps back to the updated position where the cursor was later stopped (hint: I was in front of O on the keyboard).
Note: Using Ctrl-i is the same as pressing the key.


25. Named tags


The command "Ma" marks the position name under the current cursor as "a". A total of 26 custom tags can be used from A to Z. To jump to a tag you have defined, use the command "marks "Marks is the name of the defined tag. The command 'a' causes you to jump to the beginning of the line where a is located. Command: "marks" to view the list of tokens
Command delm! Delete all tags.


26. Operator commands and displacements


The "DW" command deletes a word, and the "d4w" command deletes 4 word, and so on. Similar to have "d2e", "d$". This type of command has a fixed pattern: the operator command + displacement command. Type an operator command first. For example, "D" is a delete operator. Next is a displacement life. such as "W". This is where the command is scoped to any move cursor command.


27. Change the text


The operator command is "C" and changes the command. It behaves like the "D" command, but enters insert mode after the command executes. For example, "CW" Changes a word. Or, more precisely, it deletes a word and places you in insert mode.
The "CC" command can change the entire line. However, the original indentation is still maintained.
"C $" changes the current cursor to the end of the line content.
Shortcut command: X for DL (delete character under current cursor)
X represents DH (deletes the character to the left of the current cursor)
D stands for d$ (deleted to the end of the line)
C (Change to end-of-line content)
s represents CL (modifies one character)
S stands for cc (modifies a whole line)
The command "3DW" and "d3w" are all deleted from 3 word. The first command "3DW" can be seen as deleting a word operation 3 times, and the second command "d3w" is to delete 3 word at a time. This is one of the less obvious differences. In fact, you can put the command count in both places, for example, "3d2w" is to delete two word, repeat 3 times, a total of 6 word.


28. Replacing a single character


The "R" command is not an operator command. It waits for you to type the next character to replace that character under the current cursor. The "r" command prefix with one command count is to replace multiple word nonalphanumeric with the one that is about to be entered. To replace a character with a line break, use "R". It deletes a character and inserts a line break. Using command count here will only delete the specified number of characters: "4r" will replace 4 characters with a newline character.


29. Repeat Changes


"." The command repeats the last change you made. "." The command repeats all changes you make, except for the "U" command ctrl-r and the command that begins with a colon. "." It needs to be executed in normal mode, and it repeats the command, not the changed content.


30.Visual mode


Press "V" to enter visual mode. Move the cursor to cover the range of text you want to manipulate. The text that is selected at the same time is highlighted. Finally, type the operator command.


31. Moving text


When you delete text with a command such as "D" or "X", the deleted content is saved. You can also take it back with the P command. The "P" command is to put the returned content before the cursor, "P" is placed after the cursor. For an entire line of content that is deleted with "DD", "P" places it on the top row of the current row. "P" is the back row of the current row. You can also use command counts for command "P" and "P" commands. The effect is that the same content is retrieved for the specified number of times. This allows the "3p" after "DD" to place 3 copies of the deleted rows in the current position.
The command "XP" swaps the characters that are located in the cursor with the next character.


32. Copy text (within VIM editor)


The "Y" operator command copies the text into a register 3. You can then use the "P" command to retrieve it. Because "Y" is an operator command, you can use "yw" to copy a word. You can also use command counts. The following example uses the "y2w" command to copy two word, the "yy" command to copy a whole line, "Y" is also to copy the entire line of content, copy the current cursor to the end of the command is "y$".


33. Text objects


"DIW" removes the word (not including white space characters) where the current cursor is located "Daw" deletes the word (including white space characters) where the current cursor is located


34. Shortcut commands


X Delete the character under the current cursor (shortcut command for "DL")
X Delete the character before the current cursor (shortcut command for "DH")
D Delete the content from the current cursor to the end of the line (shortcut command for "d$")
DW deleted from the current cursor to the beginning of the next word
DB deleted from the current cursor to the beginning of the previous word
DIW Delete the word (excluding white space characters) where the current cursor is located
Daw Delete word (including white space characters) where the current cursor is located
DG Delete content from current line to end of file
DGG Delete the contents of the current line to the file header
If you use the "C" command instead of "D", these commands become change commands. Using "Y" is the Yank command, and so on.


35. Edit another file


With the command ": Edit Foo.txt", can also be abbreviated as ": E foo.txt".


36. List of files


You can specify that you want to edit multiple files when you start vim, using the command "Vim one.c two.c three.c". Vim will only display the first file after launch, after the completion of the editing of the file, you can use the order: ": Next" or ": N" to save the work and continue to edit the next file, the command: ": Wnext" or ": WN" can be combined with this process.


37. Display the file currently being edited


With the command ": args".


38. Move to another file


With the command ":p revious" ":p rev" back to the previous file, the merge save step is ": wprevious" ": Wprev". To move to the final file ": Last", to the first ":". There is no ": Wlast" or ": Wfirst" command. You can use a command count in front of the ": Next" and ":p revious" commands.


39. Edit another file list


You can redefine a list of files without restarting Vim. The command ": args five.c six.c seven.h" Defines the three files to be edited.


39. Automatic Filing


Command ": Set Autowrite", "Set AW". Automatically write the contents back to the file: If the file has been modified, in each: Next,: Rewind,: Last,: First,:p revious, stop,: suspend,: Tag,:!,: Make, Ctrl], and ctrl-^ command.
Command ": Set Autowriteall", "Set AWA". Similar to ' Autowrite ', but also applies to ": Edit", ": Enew", ": Quit", ": Qall", ": Exit", ": xit", ": Recover" and close the Vim window. Placing this option also means that Vim behaves as if it were to open ' autowrite '.


40. Switch to another file


To quickly switch between two files, use ctrl-^.


41. Document tagging


A tag named in uppercase letters. They are global tags that can be used in any file. For example, you are editing the FAB1. Java ", using the command" 50%MF "to set a tag named F in the middle of the file. Then, in the "Fab2.java" file, use the command "GnB" to set the tag named B on the last line. You can use the "F" command to jump to the half-middle of the file "Fab1.java". or edit another file, the ' B ' command will take you back to the last line of the file "Fab2.java".
To know what position a tag represents, you can use the name of the tag as the "Marks" command parameter ": Marks M" or keep up with several parameters ": Marks MJK"
You can use Ctrl-o and ctrl-i to jump to an earlier position and to a later position.


42. View Files


You can edit a file in read-only mode by viewing only the file and not writing to the file. With the command:
Vim-r file. If you want to make it mandatory to avoid modifying the file, you can use the command:
Vim-m file.


43. Change the file name


Save the existing file as a new file, using the command ": Sav (EAS) move.c". If you want to change the name of the file you are currently editing, but you do not want to save the file, you can use the command: ": F (ile) move.c".


44. Splitting a window


The simplest way to open a new window is to use the command: ": Split." The CTRL-W command can toggle the current active window.


45. Close the window


With the command: "Close". You can close the current window. In fact, any command that exits file editing ": Quit" and "ZZ" will close the window, but using ": Close" prevents you from turning off the last vim, so that you don't accidentally turn off vim as a whole.


46. Close all other Windows except the current window


With the command: ": Only", close all other windows except the current window. If any of these windows are modified, you will get an error message and the window will be left.


47. Separate a window for another file


Command ": Split two.c" can open the second window and start editing in a newly opened window as
parameter of the file. If you want to open a new window and start editing an empty buffer, use the command: ": New".


48. Vertical Segmentation


With the command ": Vsplit or:: Vsplit two.c". There is also a corresponding ": vnew" command that separates the window vertically and opens a new empty buffer in it.


49. Toggle Window


Ctrl-w h to the left window
Ctrl-w j to the window below
Ctrl-w k to the upper window
Ctrl-w l to the right window
Ctrl-w t to top window
Ctrl-w B to bottom window


50. Commands for all window operations


": Qall" discards all operations and exits, ": Wall" Save All, ": Wqall" to save all and exit.


51. Open a window for each file


Use the "-o" option to have vim open a window for each file:
"Vim-o one.txt two.txt three.txt".


52. Use Vimdiff to view different


"Vimdiff main.c~ main.c", Another way to enter diff mode is to operate in vim. Edit the file "Main.c", and then open another separator window to show the difference:
": Edit Main.c"
": Vertical Diffpatch Main.c.diff".


53. Page Sign


Command ": Tabe (DIT) thatfile" opens "Thatfile" in a window that occupies the entire vim display area. The command ": Tab split/new" results in a new tab that has a window. To switch between the different tabs with the "GT" command.



This is my summary of some of the basic usage, may be helpful to the novice, alone lele than the Lele, is it!



Description: The following black is the general function of VI and VIM, and the red is the unique function of VIM (vi improved). Vim is typically installed under UNIX and Linux.


Three different states


Command: Any input will be used as an edit command without appearing on the screen and any input will cause immediate reflection
Insert: Any input data is placed in the edit register, press ESC to jump back to command mode
Escape: An instruction preceded by ":" or "/" appears on the bottom line of the screen, and any input is treated as a special instruction.


Leave VI


: q! Leave VI and discard the content you just edited within the buffer.
: Wq writes the data in the buffer to disk and leaves VI.
: X with Wq.
(Note that--:x is file encryption, be sure to match with: X to save the exit)


Enter input mode


A (append) is joined by the cursor after the data.
A by the end of the bank to add information.
I (insert) is joined by the cursor before the data.
I shall add the information to the first of the bank.
O (Open) adds a new line below the line for the input data.
O add a new line to the row for input data.


Delete and modify


x Delete the character where the cursor is located.
X Delete the character before the cursor is located.
R replaces the character of the (replace) cursor with the character followed by this instruction. For example, RA replaces the character of the cursor with a.
R enters the replacement state until ESC is reached.
s deletes the character that contains the cursor and enters input mode until ESC.
S deletes the row data for the cursor and enters the input mode until ESC.


The movement of the cursor


0 move to the top of the line
$ move to the end of the line.
E moves to the last letter of the next word
W moves to the first letter of the next word.
b moves to the first letter of the previous word.
^ Moves to the first character of the line.
H moves to the first line of the window.
M moves to the middle row of the window.
L move to the last line of the window.
G moves to the last line of the file.
+ Move to the first character in the next column.
-Moves to the first character in the previous column.
: N moves to the nth column of the file.
N+ moves to the nth column after the cursor position.
N-Moves to the nth column before the cursor is located.
CTRL+G Displays the line number, file name, line number of the last line in the file, and the row number of the cursor that is the percentage of the total line number.


The movement of Windows


ctrl+f window scroll down one page.
ctrl+b window to roll up a page.
Ctrl+d window scroll down half page.
Ctrl+u window to roll up half a page.
The Ctrl+e window scrolls down one line.
Ctrl+y windows to roll up a row.


Cut, copy, delete


Operator + Scope = command
Operator
D Cut
Y copy.
P Paste, with D and Y and use. The data of the last D or Y can be placed under the row of the cursor position.
c Modify, similar to delete with Insert group and. Delete a word group, sentence and other information, and insert new data.
Scope
E is the position of the cursor to the last character of the string.
W is the first character from the position of the cursor to the next string.
b The first character from the position of the cursor to the previous string.
$ by the position of the cursor to the last character of the row.
0 The first character of the row from where the cursor is located.


The whole operation was made


DD deletes the entire row.
D deletes all the characters after the cursor in the behavior unit.
CC modifies the contents of the entire line.
yy copies the row where the cursor is located to the memory buffer.


Cancel a previous action (Undo)


U restores the result before the last instruction.
U restores all changes to the row of the cursor.
You can undo the instruction multiple times, undoing one operation at a time until the start of the operation.
Ctrl+r can revert to the pre-undo content, and can be recovered multiple times by multiple.


Find and replace


/String to search for the string after the cursor.
The string looks for the string before it is moved to the cursor.
N continue searching for the next identical string.
N continue looking for the next identical string.
% lookup "(", ")", "{", "}" for the pairing.
s search for a range of rows.
G Search data for the entire edit buffer.
: 1, $s/old/new/g change all the "old" in the file to "new".
: 10,20s/^//Insert 5 blanks at the front of line 10th through 20th.
(VIM)
/string after the input query content can be saved to the buffer, can be used to ↑↓ the previous content selection.
Also: Move the cursor below the selected word to press *, you can select this word as the query character, you can avoid the trouble of entering a long string of characters.


Case-sensitive replacement


First Use press V to open the selection function, and then use the ↑↓←→ key to select the character to be replaced, if lowercase to uppercase, then press U;
If you select a word, you can press V, press W, and finally press u/u, so you can change the size of the word Fu without deleting and re-typing.


Connection of data


The connection of the sentence. Connect the row below the cursor to the cursor after the row.
setting of the environment
: Set all to set the list of environment variables
: The current value of the SET environment variable
: Set Nu Sets the line number of the data.
: Set Nonu cancels line number setting.
: Set AI automatically shrinks.
: Set NOAI cancels auto-indent.
: Set ruler will display the current cursor position in the lower right corner of the screen, and change with the light movement, occupy the screen space is small, use is more convenient, recommended use.
: Set Hlsearch will highlight all matching content when using the Find function.
: Set Nohlsearch to turn off this feature.
: Set Incsearch enables VIM to position the matching point in the input string while the cursor is positioned to display it.
: Set Nowrapscan turns off the Find auto loopback function, which is to find the end of the file, end the lookup; The default state is auto loopback


Ex instruction read/write data


: 10,20w Test writes the data from line 10th to line 20th to the test file.
: 10,20w>>test adds the data from line 10th to line 20th after the test file.
: R test reads the data from the test file into the end of the edit buffer.
: e [filename] edits a new file.
: e! [FileName] Discards the currently modified file and edits the new file.
: SH enters the shell environment, exits with exit and returns to the editor.



!cmd: After running command cmd, return to the editor.


Delete, copy and move


: 10,20d Delete data from line 10th to line 20th.
: 10d Delete data from line 10th.
:%d deletes the entire edit buffer.
: 10,20co30 copies the data from line 10th to line 20th after line 30th.
: 10,20mo30 the data from line 10th to line 20th after line 30th.



Common commands for VIM for Linux applications


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.