Simple but not simple text editor Vim

If Centos does not have vim installed, you can execute the command in the networked state:

sudo yum install - y vim

install vim 

Table of contents

The basic concept of vim

Basic operation of vim

vim normal mode command set 

Vim last line mode command set 

Simple vim configuration 


The basic concept of vim

The three common modes of vim are command mode, insert mode and last line mode. The functions of each mode are as follows:

Normal/Normal/Command mode (Normal mode)

Control the movement of the screen cursor, delete characters, words or lines, move and copy a section and enter Insert mode, or go to last line mode

Insert mode

Only in Insert mode, text input can be done, press "ESC" key to return to the command line mode. This mode is the most frequent editing mode we will use later.

last line mode

Save or exit the file, you can also perform file replacement, find character strings, list line numbers and other operations. In command mode, shift+: to enter this mode. To view all your modes: open vim, enter the bottom line mode directly: help vim-modes

Basic operation of vim

Enter vim, enter vim and the file name at the system prompt symbol, and enter the vim full-screen editing screen: 

 After entering vim, it is in [normal mode], you have to switch to [insert mode] to be able to enter text. 

Switch from [Normal Mode] to [Insert Mode]

  • input a
  • input i
  • input o

Switch from [Insert Mode] to [Normal Mode] 

"ESC

Currently in [Insert Mode], you can only input text all the time. If you find that you have entered a wrong word and want to use the cursor keys to move back and delete the word, you can press the "ESC" key to enter [Normal Mode] and then delete Word. Of course, it can also be deleted directly.

[Normal Mode] switches to [Last Row Mode]

 Enter ":" (key shift + ;)

Exit vim and save the file

In [Normal Mode], press the ":" colon key to enter "Last line mode"

:w (save current file)

: wq (enter "wq", save and exit vim)

: q! (Enter q!, force exit vim without saving)

vim normal mode command set 

Although Vim is a text-based editor, it provides a wealth of editing functions. For those who are accustomed to using the graphical interface, it will be difficult to adapt at first, but after proficiency, you will find that editing with Vim is actually faster.

insert mode

Press "i" to switch to the insert mode "insert mode", and press "i" to enter the insert mode to start inputting the file from the current position of the cursor;

After pressing "a" to enter the insert mode, the text will be entered from the position next to the current cursor position;

After pressing "o" to enter the insert mode, a new line is inserted, and text is entered from the beginning of the line.

move cursor 

  • Vim can directly use the cursor on the keyboard to move up, down, left, and right, but regular vim uses lowercase English letters "h", "j", "k", and "l" to control the cursor to move left, down, up, and right by one grid
  • Press [gg]: go to the beginning of the text
  • Press "G": move to the end of the text (shift + g)
  • Press "$": Move to the "end of line" of the line where the cursor is located (shift + 4)
  • Press "^": Move to the "beginning" of the line where the cursor is located (shift + 6)
  • Press "%": switch between matching brackets (shift + 5)
  • Press "w": the cursor jumps to the beginning of the next word (next word or next sentence)
  • Press "e": the cursor jumps to the end of the next word (next word or next sentence)
  • Press "b": the cursor returns to the beginning of the previous word (next word or sentence)
  • Press "#l": the cursor moves to the #th position of the line, such as: 5l, 56l
  • Press "ctrl" + "b": move the screen to "back" by one page
  • Press "ctrl" + "f": the screen moves "forward" by one page
  • Press "ctrl" + "u": the screen moves half a page "backward"
  • Press "ctrl" + "d": the screen moves half a page "forward"

delete text 

  • "x": Each time you press, delete a character at the cursor position
  • "#x": "6x" means to delete the 6 characters behind " (including yourself) where the cursor is located
  • "X": uppercase X, each time you press it, delete a character "in front" of the cursor position
  • "#X": "20X" means to delete the "front" 20 characters of the cursor position
  • "dd": delete the line where the cursor is located
  • "#dd": Delete # line from the line where the cursor is located

copy 

  • "yw": Copy the characters from the cursor position to the end of the word into the buffer.
  • "#yw": copy # words to the buffer
  • "yy": Copy the line where the cursor is located to the buffer.
  • "#yy": "6yy" means to copy 6 lines of text "counting down" from the line where the cursor is located.
  • "p": Paste the characters in the buffer to the position of the cursor.

Note: All copy commands related to "y" must cooperate with "p" to complete the copy and paste functions.

replace 

  • "r": Replace the character where the cursor is located.
  • "R": Replace the character where the cursor is, until the "ESC" key is pressed.

undo last action 

  • "u": If you execute a command by mistake, you can press "u" immediately to return to the previous operation. Press "u" multiple times to perform multiple replies. (Ctrl+z under windows)
  • "ctrl + r": undo redo (Ctrl+y under windows)

Change

  • "cw": Change the word where the cursor is to the end of the word
  • "c#w": "c3w" means change 3 characters and jump to the specified line
  • "ctrl" + "g" lists the line number of the line where the cursor is located.
  • "#G": "5G", means to move the cursor to the beginning of the fifth line of the article.

Vim last line mode command set 

Before using the end-line mode, press the "ESC" key to make sure you are in the normal mode, and then press ":" (shift + :) colon to enter the end-line mode.

list line numbers

"set nu": After entering "set nu", the line number will be listed in front of each line in the file.

jump to a line in a file 

"#": "#" means a number, enter a number after the colon, and then press the Enter key to jump to the line. For example, enter the number 16 and press Enter, it will jump to the 16th line of the article .

find character

  • "/Keyword": Press the "/" key first, and then enter the character you want to find. If the keyword you searched for the first time is not what you want, you can keep pressing "n" to find the desired keyword.
  • "?Keyword": Press the "?" key first, and then input the character you want to find. If the keyword you searched for the first time is not what you want, you can keep pressing "n" to search forward until you reach the desired keyword.
  •     : Ignore case when set ic lookup

/ and? The difference between searching: / After finding, the cursor is positioned on the second line, ? The cursor is positioned on the first line after it is found.

save document

"w": Enter the letter "w" in the colon to save the file

leave vim

  • "q": Press "q" to quit. If you can't leave vim, you can force to leave vim with a "!" after "q".
  • "wq": It is generally recommended to use it together with "w" when leaving, so that the file can be saved when exiting.

vim split screen (open multiple files)

Enter vs + filename in the bottom line mode, if the filename does not exist, the file will be created automatically. 

Where the cursor is, which file is being edited, switch the cursor (switch between different interfaces): Ctrl+ww (press and hold Ctrl, press the w key twice quickly)

Simple vim configuration 

The location of the configuration file

Under the directory /etc/, there is a file named vimrc, which is a public vim configuration file in the system and is valid for all users. And in each user's home directory, you can create your own private configuration file, named: ".vimrc". For example, under the /root directory, there is usually a .vimrc file, and if it does not exist, it will be created. Switch users to become yourself and execute su, enter your main working directory, execute cd ~, open the .vimrc file in your own directory, and execute vim .vimrc

  

 

Create one without a .vimrc 

 

Write the things to be configured, save and exit, and the functions you configured will be available when vim is edited. 

Common Configuration Options 

  • Set syntax highlighting: syntax on
  • Show line number: set nu
  • Set the number of spaces for indentation to 4: set shiftwidth=4
  • Set on/set off Whether to show relative color help according to the syntax. When modifying related configuration files or shell script files in Vim, the corresponding colors will be displayed by default to help troubleshoot. You can unset this setting if you feel the colors are interfering
  • set ruler/set noruler Set whether to display the status bar in the lower right corner. The default is ruler display.
  • set showmode/set noshowmode Set whether to display a status bar such as "_ INSERT- " in the lower left corner. The default is showmode display.

Guess you like

Origin blog.csdn.net/m0_55752775/article/details/128925908