1. vim#
Text editor - vim (==edit text==)
Windows: Notepad, Word, Sublime, PyCharm
Can edit music, video, images? Answer: No, ==can only edit text==
Linux:
vi (==character interface==), vim (==character interface==), gedit (==graphical==)
==Unix installs vi==
vim is an ==enhanced version== of vi (adding other features on top of vi)
Purpose of vim editor#
The purpose of vim editor ==is==:
==1. Write scripts and programs==
==2. Modify configuration files==
==3. Perform some text processing==
vim modes#
vim has three most commonly used basic modes: command mode, edit mode (insert mode), last line mode, visual mode, replace mode
vim has three most commonly used ==basic== modes:
==Command mode==
==Edit mode==
==Last line mode==
There are also two other ==less commonly used== modes:
==Visual mode==
==Replace mode==
vim command mode#
==Command mode: vim a / ESC==
==Cannot edit==, only allows quick operations like delete, copy, paste, undo, etc., this mode is entered by default when opening a file
==yy copy==
==p paste==
==dd delete==
==u undo==
==Command mode== acts as an ==intermediary==
Exiting each mode will ==return to command mode==, command mode can enter all modes
vim edit mode#
Edit mode ESC ==i==
-- Insert --
Insert mode, for ==text editing==
-----------------------------------------------------------------------
Operation key Function
==i== Cursor enters insert mode at the ==current== position
I Cursor ==jumps to the beginning of the line== and enters insert mode
a Cursor ==moves back one space== and enters insert mode
A Cursor ==moves to the end of the line== and enters insert mode
==o== Starts a new line below the cursor's current ==line== and enters insert mode
==O== Starts a new line above the cursor's current ==line== and enters insert mode
s ==Deletes== the character at the cursor's ==position== and enters insert mode
S ==Deletes== the current ==line== where the cursor is and enters insert mode
vim last line mode#
Last line mode ESC ==:==
:
Enter commands, set environment, save files, search and replace
----------------------------------------------------
Operation key Function
==== Exit ==if no changes have been made to the file==
==!== ==Discard changes== and ==exit==
== Save== the current file and ==exit==
==! Force save== the current file and ==exit==
== Save== the current file and ==exit==
==! Force save== the current file and ==exit==
==ZZ Save== the current file and ==exit==
vim visual mode#
Visual mode ESC ==v, V, ctrl+v==
-- Visual Line --
Select specific blocks of text for copying, modifying, etc.
Small ==v==: Select ==part of the line==
Large ==V==: Select ==the entire line==
==Ctrl + v== : Select ==block== Insert with I Exit mode to command mode (Esc), at this point everything is added
vim replace mode#
Replace mode ESC ==R==
-- Replace --
-----------------------------------
123 hello456 789
xxx helloyyy zzz
xxx yyy zzz
Exercises#
Class Exercise 1 Insert Mode
123 456
123 4
xyz 789
4
xyz 789---------------------------------------------------------
Steps:
==v== Small v Visual mode ---》 Select a portion
==y== Press y to copy
==o== Press o to start a new line, enter insert mode
==P== Press esc, then p, press p to paste
Class Exercise 2
123 TEST 456 789
xxx TEST yyy zzz---------------------------------------------------------
Select block to insert
ctrl + v: Select block
Insert with large I
ESC Exit mode to command mode, at this point everything is added
Common vim Shortcuts#
Common vim shortcuts
Open file with vim:
# vim e
# ==vim +5 e==Indicates opening file e and ==moving the cursor to line 3==
# ==vim +/aa e==
Open file e and ==highlight the aa character==
Methods to Exit vim Editor#
Save and exit vim:
Operation key Function
==== Exit ==if no changes have been made to the file==
==!== Discard changes and ==exit==
==== ==Save== the current file and ==exit==
==!== ==Force save== the current file and ==exit==
==== ==Save== the current file and ==exit==
==!== ==Force save== the current file and ==exit==
==ZZ== ==Save== the current file and ==exit== (Note: exit from command mode)
Cursor Movement#
Cursor movement: Up, down, left, right arrow keys (k, j, h, l)
-------------------------------------------------
Operation type Operation key Function
==Cursor== direction move ==k, j, h, l== ==Up, Down, Left, Right==
==============================================================
==Page up/down== Page Down or Ctrl+F Scroll ==down== a whole page
Page Up or Ctrl+B Scroll ==up== a whole page
==============================================================
==Quick jump within line== Home key or “^”, number “0” Jump to ==beginning of line== (Note: “^” caret symbol)==
End key or “$” key Jump to ==end of line==
==============================================================
==Quick jump between lines==
1G or gg Jump to the ==first line== of the file
G Jump to the ==last line== of the file
#G Jump to the ==#th line== of the file
(last line mode):40 Jump to ==line 40==
w Jump to the ==first letter== of each word
e Jump to the ==last letter== of each word
vim Settings#
vim settings
(last line mode) number ==Set line numbers==
(last line mode) nonumber ==Cancel setting==
--------------------------------------
:set number 1 aa bb cc 2 dd ee ff 1 aa bb cc 2 dd ee ff 3 aa bb cc
Delete (Cut), Copy, Paste#
Operation type
Operation key Function
--------------------------------------
Delete (Note: Generally ==^ beginning of line, $ end of line==)
==x or d or Del Delete== the ==single character== at the cursor
==dd Delete== the current line where the cursor is
==#dd Delete== the ==# lines== of content starting from the cursor
d==^== ==Delete== all characters from the cursor to the beginning of the line
d==$== Delete all characters from the cursor to the end of the line
d==w== ==Delete one word== (word), separated by spaces, colons, etc.
------------------------------------------------------
Copy
==yy Copy== the entire content of the current line to the clipboard
==#yy Copy== the content of ==# lines== starting from the cursor
------------------------------------------------------
Paste
Small ==p== Paste== the content from the ==buffer== to the cursor position ==after==
Large ==P Paste== to the cursor position ==before==
File Content Search#
File content search
Operation key Function
==/==word ==Search for the string "word"== from top to bottom in the file
==?==word ==Search for the string "word"== from bottom to top in the file
Small ==n== ==Locate the next== matching searched string
Large ==N== ==Locate the previous== matching searched string
(Note: /word and ?word differ in that they have ==opposite directions==)
Cancel Highlighting, Set Line Numbers, Cancel Line Number Setting#
Cancel highlighting: noh
Set line numbers: set nu
Cancel: set nonu
2. vim-tmux and Character Encoding Issues#
File Content Replacement
File content replacement (last line mode)
Command Function
==
/old/new== Replace the ==first== occurrence of the string "old" found in the ==current line== with "new"==
/old/new/g== Replace all occurrences of the string "old" found in the ==current line== with "new"==:#,# s==/old/new/==g== Replace all occurrences of the string "old" with "new" in the ==line number range "#,#"==
==:% s==/old/new/==g== Replace all occurrences of the string "old" with "new" in the ==entire file== global
==
== /old/new/==gc== By adding the ==c command== at the end of the replacement command, it will prompt the user for ==confirmation== for each replacement action==:%s==/feng//==g== Delete all occurrences of "feng"
(Note:
/…/… Current line first str
/…/…/g Current line all str :#,#s/…/…/g Line number #,# all str
:%s/…/…/g Entire document all str
/…/…/gc Entire document all str confirmation prompt :%s/…//g Entire document all str delete)
(Note: s —— Replace
—— Current line replace #,# —— Line number # to #
% —— Entire document
g —— All strings
c —— Confirmation prompt
:%s/…//g —— Entire document all str delete)
==Cache: swp file==
==When writing a file 1.txt==
==First, a copy of 1.txt is made as .1.txt.swp, the actual editing is done on this file==
==When saving, .1.txt.swp will overwrite 1.txt==
-------------------------------------------------------------------------------------------
Related Operations
In one terminal # vim 2.txt, in another terminal type # ls -al, the prompt is as follows
[root@sanchuang-linux cp]# ==ls -al==
Total usage 88
-rw-r--r--. 1 root root 4096 Oct 15 15:05 ==.2.txt.swp================================\