[LINUX] Vim

Overview

vim is one of the editors. Type vim in the terminal to launch Vim. .. The initial state of startup is normal mode. Entering ʻi in normal mode switches to input mode. You can return to normal mode by pressing the ʻesc key. To exit Vim, press : to switch to command line mode and then press q to enter. You can finish with this. Enter : q! If you want to ignore the changes and exit. If you add !, You can ignore the change and exit. If you use Vim to rewrite and save the file, you can save and exit by pressing wq after: . If it is : w, it will be saved and Vim will not end. If you want to change the file name and save it by overwriting, you can change and save it by following : w with the file name you want to change.

You can activate the visual mode by pressing v. You can see while selecting.

If you want to move from the current file to another file, you can move it by following : e with the file name.

Mobile commands

In normal mode, enter gg to move to the top. On the contrary, if you want to move to the bottom, you can move immediately by entering G.

If you want to move in units of screens, you can move to one screen with control + f. On the contrary, if you want to move to one screen, you can move it with control + b.

If you want to move word by word, you can use w to move forward word by word and conversely use b to go back word by word.

Line-by-line movement You can move to the beginning of the line by entering ^. Conversely, you can move to the end of the line by entering $.

Unique move You can move it by entering f +the character of the part you want to move. For example, typingfr will take you to the next r. If you have multiple rs and want to go to the next r, you can enter ;to move to the nextr. If you continue to enter ;, you can move to the next r`.

{} Move You can move to the corresponding } by typing % in {. You can go back again by typing it again. The same is true for ().

Visual mode

v Character unit selection shift + v = V Row unit selection control + v rectangle selection Press gg to go back to the beginning and press shift + v to switch to line-by-line selection, and G to select all.

cut

In normal mode, you can cut by x single character cut dd in line units, and if you want to cut multiple lines, you can cut by the number of lines you want to cut + dd. For example, if you want to cut 3 lines at once, it will be 3dd. To paste what you want to cut, type p and you can paste it. The one cut just before will be pasted.

If you want to copy instead of cut, you can copy by typing yy. It will be a copy for each line. If you want to copy multiple lines, specify the number of lines as well as cut. For example, if you want to copy 5 lines at once, it will be 5yy.

Search

After entering / in normal mode, enter the word you want to search for and press enter to move to the position of that word. The searched word may match more than once. To move to the matched word below, you can use n. Conversely, if you want to move up, you can move it with N.

Search for the word where the cursor is

* Moves from the position where the cursor is now to the perfectly matching word above. # It will move from the position where the cursor is now to the word that matches perfectly below.

Replacement

This is the method when you want to replace the character in the line where the cursor is now. Switch to command mode with :. Express the replacement with s. Abbreviation for substitute. Then enter / and the word or character you want to replace in the line. After that, enter / and the changed character or word. In this case, only the first matching character will be replaced. If you want to replace the first and subsequent matching characters in the line at once, enter / and g afterwards.

ex) Replaces the first match with e in the: s / e / E line with E. : s / e / E / g Replace all e in the line with E.

If you want to replace the entire file, enter % after : and before s. This % means the entire file. ex):% s / e / E / g Replaces all e in the entire file with E.

I want to replace this when there are multiple matches in the entire file. Suppose you don't want to replace this. In that case, enter g followed by c. ex):%s/e/E/gc

replace with E (y/n/a/q/l/^E/^Y)?

It will be displayed in the terminal as above and the cursor will be on the matched character. At that time, enter the options. Then, it is processed according to the selection and the cursor moves to the next matching character. It is an option, but you can make various choices such as y if you want to replace it and n if you do not replace it.

Cancel operation

ʻU is an abbreviation for undo, and it will be restored when you enter it. You can use it when you make a mistake. If you accidentally press ʻu, but want to change the state after processing before pressing ʻu, you can change the state with control + r`.

Split window

It can be used when multiple files are displayed at the same time.

: sp Enter when you want to divide the screen into upper and lower parts. control + w + w Enter when moving between screens. : vs Enter when you want to divide the screen into left and right. close Used when you want to close the screen.

tab

: tabnew You can open a new tab. : tabe + file name If you know the file to open in advance, you can specify the file and open a new tab by following : tabe with the file name. You can move between tabs with gt in normal mode. : tabclose You can close the corresponding tab. vim -p file name File name If you want to start multiple files in tabs before starting vim, add -p and specify the file you want to open, vim will start in multiple tabs.

: tabdo Enter when you want to replace in all tabs. ex):tabdo %s/e/E/g The replacement is done for all tabs.

Repeat the last operation

You can repeat the previous operation by pressing . in normal mode. ex) Enter 2dd to delete two lines, then enter . to delete two lines again.

Indent

It is used to align the indentation. You can align the indentation by entering =. You can align the entire indent by selecting all with gg + shift + v + G and then entering=.

Complementary function

Use it when you want to use that function name or that variable name again. Enter the first letter or dark up to the second letter, then type control + n to display the previously used word for selection. If only one is applicable, the selection screen will not be displayed and it will be completed automatically. This is useful when you want to re-enter a long function or variable name.

Rectangle selection

Select a rectangle with control + v and enter shift + i = ʻI. You can enter characters across multiple lines. ʻEsc It will be reflected in the selected part by inputting twice. This is convenient when you want to enter all at once or delete all at once.

Text object

It means to erase the contents of the cit tag and enter the input mode. It means to erase the whole including the contents of the cat tag and enter the input mode. c change delete && insert d Delete

ʻIʻa Whether the contents are whole ʻI is the contents inner ʻa is the whole all

t`` " ) ] Specifies the objects on the line where the cursor is. You can process them.

Convenient settings

: set number Displays the number of lines. : set nonumber The number of lines is hidden. : Syntax on Color-coded. : Syntax off It hides the color coding. : set tabstop = number You can specify the number of characters in the tab. : set ignorecase You can make it case insensitive. Searches usually only catch lowercase letters. By using this, it will match uppercase letters as well. It will be distinguished by : set noignorecase. The search will only catch lowercase letters.

Recommended Posts