08_Linux basics – vim-tmux – character encoding

Time:2022-11-24

@TOC

08_Linux basics – vim-tmux – character encoding


One. vim

Text editor – vim (==edit text==)

Windows: notepad, word, sublime, pycharm

Can edit music, video, pictures? Answer: No, == can only edit text ==

Linux:

vi (==character interface==), vim (==character interface==), gedit (==graphic ==)

==Unix will install vi==

vim is ==enhanced version== of vi (add other functions on the basis of vi)

The function of vim editor

vim editor == function ==:

==1. Writing scripts and programs==

==2. Modify the configuration file==

==3. Do some text processing==

vim-mode

vim has three most commonly used and basic modes: command mode, edit mode (insert mode), last line mode, visual mode, replacement mode

Vim has three most commonly used == basic == modes:

==Command mode==

==Edit Mode==

==Last Line Mode==

There are two other ==not commonly used== modes:

==View Mode==

==Replace Mode==

vim command mode

== Command mode: vim a / ESC ==

==Unable to edit==, only allows shortcut operations such as delete, copy, paste, undo, etc., when opening a file ==Default==Enable this mode

==yy copy==

==p paste==

==dd delete==

==u Undo==

==command mode== is equivalent to a ==intermediate==intermediate

Each mode ==exit== will ==return to command mode==, command mode can enter all modes

vim edit mode

Edit Mode ESC ==i==

— INSERT–

Insert mode, make ==text edit==

———————————————————————–

Operation key function

==i== Cursor enters insert mode at ==current== position

    I Cursor==jump to beginning of line==and enter insert mode

    a Cursor==back one space==and enter insert mode

    A Cursor==back to the end of the line==and enter insert mode

    ==o== Create a new line == under the line where the cursor is == and enter the insert mode

    ==O== Create a new line == on the line where the cursor is == and enter the insert mode

    s ==delete==cursor==character==and enter insert mode

    S == delete == where the cursor is == line == and enter insert mode
vim last line mode

Last line mode ESC ==:==

:

Enter commands, set the environment, save files, find and replace

—————————————————-

Operation key function

==:q== ==Quit== if no changes have been made to the file

    ==:q!====Discard Save==and ==Exit==

    ==:wq save==current file and ==quit==

    ==:wq! Force == current file to be saved and ==quit==

    ==:x save==current file and ==exit==

    ==:x! force == current file to be saved and ==exit==

    ==ZZ save==current file and ==exit==
vim view mode

view mode ESC ==v, V, ctrl+v==

— Visible lines —

Select specific block text, copy, modify, etc.

Small ==v==: select == the section specified by the row ==

Large ==V==: select == whole line ==

==Ctrl + v== : Select == Block == Insert and press I to exit mode and enter command mode (Esc), add all at this time

vim replace mode

replace mode ESC ==R==

— replace —

———————————–

123 hello456 789
xxx helloyyy zzz
xxx yyy zzz

practise

Class Exercise 1 Insert Mode

123 456
123 4
xyz 789
4
xyz 789

———————————————————

step:

==v== small v visual mode visual — “select a part

==y== press y to copy

==o== Press o to start a new line and enter insert mode

==P== Press esc again, then p, press p to paste

Class Exercise 2

123 TEST 456 789
xxx TEST yyy zzz

———————————————————

select block insert

ctrl + v: select block

Insert by big I

ESC exits mode and enters command mode, at this time add all

Vim commonly used shortcuts

vim common shortcuts

vim opens the file:

\# vim e
\# ==vim +5 e==

Indicates to open file e, and ==move the cursor to line 3==

\# ==vim +/aa e==

Open the file e, and ==highlight the characters aa==

How to exit the vim editor

vim save and exit:

Operation key function

==:q== ==Quit== if no changes have been made to the file

    ==:q!== Abandon saving and ==quit==

    ==:wq== ==Save==current file and ==Exit==

    ==:wq!== ==Force save==current file and ==quit==

    ==:x== ==Save==current file and ==Exit==

    ==:x!== == force save == current file and == exit ==

    ==ZZ== ==Save==current file and ==Exit== (Note: command mode exit)

cursor movement

Cursor movement: up, down, left, and right arrow keys (k, j, h, l)

————————————————-

Operation type Operation key function

== cursor == direction movement == k, j, h, l == == up, down, left, right ==

\=\=\=\=\=\==\=\=\=\=\=\=\=\=\==\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=

==Flip page== Page Down or Ctrl+F ==Down==Flip a whole page

Page Up or Ctrl+B ==Up==Flip a whole page

\=\=\=\=\=\==\=\=\=\=\=\=\=\=\==\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=

==inline==quick jump Home key or “^”, number “0” jump to == the beginning of the line (Note: “^” point symbol) ==

End key or "$" key to jump to ==end of line==

\=\=\=\=\=\==\=\=\=\=\=\=\=\=\==\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=

==Between lines==Quick jump

1G or gg Jump to the ==first line== of the file

G Jump to the == end line of the file ==

\#G jump to == line # in the file ==

(Last line mode): 40 Jump to == line 40 ==

w == each word == first letter == jump

e = = == end letter of each word == = jump

vim-settings

vim-settings

(last line mode): set number == set line number ==

(last line mode): set 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: General==^beginning of line, $end of line==)

==x or d or Del delete==single character== at the cursor

==dd delete==current cursor==line==

==\#dd delete===\#line content==from the cursor

d==^====Delete==Current==Before the cursor to the beginning of the line==All characters

d==$== Delete all characters from the current == cursor to the end of the line ==

d==w====Delete a word==(word), with symbols such as spaces and colons

——————————————————

copy

==yy copy==current line==whole line==to the clipboard

==\#yy copy====#line== content from cursor

——————————————————

paste

Small==p== Paste== the content in ==buffer==== to the cursor position==after==

Big==P Paste==to cursor position==before==

file content lookup

file content lookup

Operation key function

==/==word == top down == Find the string “word” in the file

==?==word==Bottom up==Finds the string “word” in the file

Small ==n== == Locate the next == matching searched string

Large==N====Locate the previous==matched searched string

(Note: The difference between /word and ?word: == opposite direction ==)

Cancel the highlight, set the number of lines, cancel the setting of the number of lines

Unhighlight: noh
Set the number of lines: set nu
cancel: set nonu

2. vim-tmux and character encoding problems

file content replacement

File content replacement (last line mode)

command function

==:s /old/new== Replace == the first == character “old” string == found in == the current line == with “new”

==:s /old/new/g== Replace == all == string “old” == found in == current line == with “new”

==:#,# s==/old/new/==g== Replace == all == strings “old” with “new” in the range of ==line number “#,#”==

==:% s==/old/new/==g== Replaces == all == strings “old” with “new” global in ==whole file==

==:s== /old/new/==gc== Add the ==c command== at the end of the replacement command, and each replacement action ==prompt==user ==Confirm==Confirm

==:%s==/feng//==g== delete == all feng== of ==

(Note: :s/…/… the first str in the current line

:s/…/…/g all str in the current line

        :#,#s/…/…/g line number#,# all str

        :%s/…/…/g all str in the whole document

        :s/…/…/gc all str confirmation prompts throughout the document

        :%s/…//g entire document all str deleted)

(Note: s – replace

:s - replace the current line

        \#,# - line number # to #

        % - the entire document

        g - all strings

        c - confirmation prompt

        :%s/…//g - all str deleted in the whole document)

==Cache: swp file==

==When writing a file 1.txt==

==First copy the 1.txt file and copy it into a .1.txt.swp file. This file is actually edited==

==When saving, .1.txt.swp will overwrite 1.txt==

——————————————————————————————-

related operations

Under one terminal # vim 2.txt, another terminal type # ls -al, the prompt is as follows

[[email protected] cp]# ==ls -al==
Total usage 88
-rw-r–r– 1 root root 4096 October 15 15:05 == 2.txt.swp==

\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\==\=\=\=\=\=\=\=\=\=\=\=\=\=\==\=\=\=\=\=\=\=\=\=\=\=\==\=\=\=\=\=\=\=\=\=\==\=\=\=\=\==\=\=\==\=\=\=\=\=\=\=\=\==\=\=\=\=\=\=\=\=\=\=\==\=\=\=

In another terminal # vim 2.txt, the prompt is as follows

Found swap file ".2.txt.swp"
         Owner: root Date: Thu Oct 15 15:05:45 2020
         Filename: ~root/cp/2.txt
         Modified: No
         Username: root Hostname: sanchuang-linux
        Process ID: 3129 (still running)
Opening file "2.txt"
           Date: Thu Oct 15 15:05:22 2020

(1) Another program may be editing the same file.  If this is the case,
 be careful not to end up with two different instances of the same
 file when making changes.  Quit, or continue with caution.
(2) An edit session for this file crashed.
 If so, use ":recover" or "vim -r 2.txt"
 Recover modified content (see ":help recovery").
 If you have done a restore, delete the swap file ".2.txt.swp"
 to avoid seeing this message in the future.

The swap file ".2.txt.swp" already exists!
Open as read-only ([O]), direct edit ((E)), resume ((R)), quit ((Q)), abort ((A)):

\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\==\=\=\=\=\=\=\=\=\=\=\=\=\=\==\=\=\=\=\=\=\=\=\=\=\=\==\=\=\=\=\=\=\=\=\=\==\=\=\=\=\==\=\=\==\=\=\=\=\=\=\=\=\==\=\=\=\=\=\=\=\=\=\=\==\=\=\=

:wq When writing, the prompt is as follows

"2.txt"                                                                                          
Warning: This file has changed since it was loaded! ! !
Are you sure you want to write (y/n)?

==* Note==: Enter the command mode, press the number key, and then enter the insert mode, the input content will be copied into multiple copies according to the size of the number pressed before

(Note: Step

vim 1.txt ——> press number 3 first ——> press i to enter insert mode —> abc ——> Esc ——> abcabcabc)

Open a new file or read in other file content && undo and save as

Open a new file ==:e== or read other file content ==:r==

command function

==:e== ~/install.log == open == new == file == make == edit ==

==:r== /etc/filesystems in ==current file== == read other file content == (Note: the additional content)

Undo and Save As ==:w==

==:w== /tmp/hello.txt Save the current file as hello.txt in the /tmp directory

== Simultaneous editing == Multiple files

\# ==vim 1.txt 2.txt==

==:n== edit previous

==:N== edit next

\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\==\=\=\=\=\=\=\=\=\=\=\=\=\=\==\=\=\=\=\=\=\=\=\=\=\=\==\=\=\=\=\=\=\=\=\=\==\=\=\=\=\==\=\=\==\=\=\=\=\=\=\=\=\==\=\=\=\=\=\=\=\=\=\=\==\=\=\=

example:

\# vim 2.txt

==:e 1.txt==

(Note: enter 1.txt for editing)

————————–

\# vim 2.txt

==:r 1.txt==

(Note: Read the contents of the 1.txt file. Insert the contents of the 1.txt file in the line below the current cursor)

\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\==\=\=\=\=\=\=\=\=\=\=\=\=\=\==\=\=\=\=\=\=\=\=\=\=\=\==\=\=\=\=\=\=\=\=\=\==\=\=\=\=\==\=\=\==\=\=\=\=\=\=\=\=\==\=\=\=\=\=\=\=\=\=\=\==\=\=\=

Undo and Save As ==:w==

command function

==:w== /tmp/hello.txt Save the current file as hello.txt in the /tmp directory

\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\==\=\=\=\=\=\=\=\=\=\=\=\=\=\==\=\=\=\=\=\=\=\=\=\=\=\==\=\=\=\=\=\=\=\=\=\==\=\=\=\=\==\=\=\==\=\=\=\=\=\=\=\=\==\=\=\=\=\=\=\=\=\=\=\==\=\=\=

example:

\# vim 1.txt

==:w /root/cp/test.txt==

(Note: Save the file 1.txt in the /root/cp/ directory, ==rename it to test.txt==)

Vim configuration (3 types)

——————————————————————————————–

The first

==vim settings==: (last line mode)

Set the number of rows at one time: (only valid during current editing)

:set number

Cancel

:set nonumber

——————————————————————————————–

the second

==Global Configuration Files==:

\# vim /etc/vimrc

\# cp /etc/vimrc{,.bak} (Note: The effect is equivalent to # cp /etc/vimrc /etc/vimrc.bak)

(* Note ↑: Backup operation. Back up before modifying the global configuration file. Roll back if there is an error)

set number (Note: ==set row number==)

set ==showcmd== (Note: ==display the current command ==)

set encoding=utf-8 (Note: == set encoding ==)

set tabstop=4 (Note: ==Set the number of spaces for the tab key==)

——————————————————————————————–

The third type: (only valid for the current user)

==Write ~/.vimrc==

[[email protected] ~]# vim ==.vimrc==

[[email protected] ~]# cat .vimrc

set number

set showcmd

set encoding=utf-8

set tabstop=4

tmux

Multi-screen editing in tmux shell
Install:
# yum install tmux  -y  
tmux
Ctrl+b up, down, left, right
Ctrl+b shift +"
Ctrl+b shift + %
Ctrl+b + c to create a new page
Ctrl+b + p Return to the previous page
Ctrl+b+n Return to the next page
Ctrl+b + x close the current small window     
ctrl+d off
Ctrl+b + b + ! delete

Ctrl+b + d temporary storage, save the current tmux state and exit tmux

tmux:

Installation: yum install tmux

tmux:

One: Knock tmux to enter the session

A green bar at the bottom of the window indicates that you have entered tmux

———————————————————————————————————-

Left and right split screen: first press ==ctrl + b==, then press shift+==%== number

Split screen up and down: first press ==ctrl + b==, then press shift+==”==

Move the cursor up, down, left, and right: ==ctrl + b==, then press ==up, down, left, and right== arrow keys

Delete a split: move the cursor to that split screen, then press ==ctrl +d==

Add a new window (add a new page): ==ctrl+b==, then press ==c==

Return to previous page: ==ctrl+b== then press ==p==

Return to next page: ==ctrl+b== then press ==n==

———————————————————————————————————-

Save the current tmux state, and exit tmux:

Use: ==ctrl+b== then press ==d==

———————————————————————————————————-

View current saved session state

[[email protected] ~]# ==tmux ls==

0: 2 windows (created Thu Oct 15 16:31:27 2020) [139×22]

1: 2 windows (created Thu Oct 15 16:45:40 2020) [139×22]

————————————————————————————————————-

Enter the first session state:

==tmux a -t 0==

————————————————————————————————————-

==Set tmux==, which is very similar to vim’s last line mode:

Press ==ctrl+b== , then press ==:== (there will be ==yellow== bars at the bottom of the window)

Set ==sync==: ==ctrl+b== then press ==:== then type ==set synchronize-panes on==

unsync==set synchronize-panes off==

The tmux terminal is closed, and the executed program will continue to execute (* the benefits of tmux)

The tmux terminal closes, not the tmux session closes. The tmux terminal is closed, and the previously executed programs will continue to execute.

Without using tmux, by default, the terminal is closed, and the program being executed is also closed. The test is as follows

\=\=\=\=\=\=\=\=\=\==\=\=\=\=\=\=\=\=\=\=\=\==\=\==\=\=\=\=\=\=\=\==\=\=\=\=\=\==\=\=\=\=\=\=\=\=\=\=\=\==\=\=\=\=\=\=\=\=

test:

== Writing scripts: ==

\# cat sleep.sh
echo “begin………..”
sleep 100
echo “end………….”

\=\=\=\=\==\=\=\=\=\=\=\=\=====\=\==\=\=

==run script==
\# sh sleep.sh
begin………..

\=\=\=\=\==\=\=\=\=\=\=\=\=====\=\==\=\=

Run the script: == (put it in the background) ==

\# sh sleep.sh ==&==

\=\=\=\=\==\=\=\=\=\=\=\=\=====\=\==\=\=

==Check if the process is running==

\# ==ps -ef |grep sleep==
root 13133 13112 0 17:12 pts/6 00:00:00 sh sleep.sh
root 13134 13133 0 17:12 pts/6 00:00:00 sleep 100
root 13190 13140 0 17:12 pts/16 00:00:00 grep –color=auto sleep

\=\=\=\=\==\=\=\=\=\=\=\=\=====\=\==\=\=

==Close the terminal and reopen the connection to see if the process is running. ==

Result: not running

……

\=\=\=\=\==\=\=\=\=\=\=\=\=====\=\==\=\=

==Run script under tmux==

……

==After tmux is closed, the session is still there, and the program will continue to run==

\# ps -ef |grep sleep
root 13278 13003 0 17:17 pts/13 00:00:00 sh sleep.sh
root 13279 13278 0 17:17 pts/13 00:00:00 sleep 100
root 13284 13140 0 17:17 pts/16 00:00:00 grep –color=auto sleep

\=\=\=\=\==\=\=\=\=\=\=\=\=====\=\==\=\=

coding

coding

coding:http://www.ruanyifeng.com/blo…

There are generally 2 ways to encode files: ==ascii and unicode==

UTF-8 is one of the implementations of Unicode

==Transcoding==:

\# ==iconv -f== utf-8 ==-t== gbk ==code_test.txt== >==code_test1.txt==

The ASCII code can represent up to 256 characters, which is not enough

unicode universal code, each character gives you a serial number

UTF-8 is one of the implementations of Unicode, which stipulates how to store it in the computer.

(UTF-8 is friendly to ascii code/English code)

==What encoding method is used to store, and what encoding method must be used to decode==

utf-8、utf-16

Domestic codes gbk, gb2131

\=\=\=\==\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\==\=\=\=\=\==\=\=\=\=\==\=\=\=\=\=\=\=

== System language setting environment: ==

\# ==locale==

LANG=zh_CN.UTF-8

LC_CTYPE=”zh_CN.UTF-8″

LC_NUMERIC=”zh_CN.UTF-8″

LC_TIME=”zh_CN.UTF-8″

LC_COLLATE=”zh_CN.UTF-8″

LC_MONETARY=”zh_CN.UTF-8″

LC_MESSAGES=”zh_CN.UTF-8″

LC_PAPER=”zh_CN.UTF-8″

LC_NAME=”zh_CN.UTF-8″

LC_ADDRESS=”zh_CN.UTF-8″

LC_TELEPHONE=”zh_CN.UTF-8″

LC_MEASUREMENT=”zh_CN.UTF-8″

LC_IDENTIFICATION=”zh_CN.UTF-8″

LC_ALL=

\=\=\=\==\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\==\=\=\=\=\==\=\=\=\=\==\=\=\=\=\=\=\=

==file code_test.txt Note: Check the file encoding type==

\# locale system language setting environment

\# file code_test1.txt to see the file encoding method

The system, file, and shell are unified, and there will be no garbled characters

\=\=\=\==\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\==\=\=\=\=\==\=\=\=\=\==\=\=\=\=\=\=\=

Class code operation:

\# mkdir vim_test
\# cd vim_test/
\# ls
\# rz -E (Note: upload the windows code_test.txt file to linux)
rz waiting to receive.
\# ls
code_test.txt
\# vim code_test.txt
\# ==file code_test.txt (Note: Check the file encoding type)==
code_test.txt: UTF-8 Unicode text
\# iconv -f utf-8 -t ==gbk== code_test.txt >code_test1.txt (Note: switch to national standard code)
\# ==file code_test1.txt==
code_test1.txt: ==ISO-8859 text (Note: national standard code)==
\# vim code_test1.txt (Note: Garbled characters are displayed)
\#

Why is there garbled characters? Answer: Open gbk encoding in utf-8 mode

What encoding method is used to store, and what encoding method must be used to decode

summary (xmind)

08_Linux basics - vim-tmux - character encoding

vim text editor
vi,vim
vim common mode
    command mode
        文件打开默认就是command mode
        ESC key arrives
    edit mode
        a,i,o,s (supporting close-up)
    last row mode
        :
    visual mode
        v
            Select the specified part of the text
        V
            Select the specified line of text
        ctrl+v
            Select a specified block of text
    replace pattern
        R
basic use
    command mode
        delete
            x 、d
            dd
            5dd
        copy
            yy
            3yy
        paste
            p 
        revoke
            u
        look up
            /
        cursor movement
            Up, down, left and right arrow keys (k, j, h, l)
            file header
                gg
            end of file
                G
            move to specific line
                5G
                :5
            beginning of line
                home button
                0
                ^
            end of line
                end key
                $
            page up
                page up, ctrl+b
            page down
                pagedowm, ctrl+f
    edit mode
        i
        I
        o
        O
        a
        A
        s
        S
    visual mode
        copyy
        Insert I
    last row mode
        exit and save
            q
            q!
            wq
            wq!
            x
            ZZcommand mode退出
        replace
            :s/old/new
            :s/old/new/g
            :%s/old/new
            :%s/old/new/g
            :3,5s/old/new/g
            :%s/old/new/gc
        set up
        file operation
            :e edit new file
            :r import file contents
            :w save file path
            vim  1.txt 2.txt
                :n edit next
                : N edit previous
Vim common shortcuts to use
    vim file1 file2 file3
    vim +10  file
    vim +/aa file
configuration of vim
    temporary configuration
        last row mode
            :set nu
    global configuration
        /etc/vimrc
    user configuration
        ~/.vimrc
tmux
    tmux:
Installation: yum install tmux
tmux: 
One: Knock tmux to enter the session
A green bar at the bottom of the window indicates that you have entered tmux

Left and right split screen: press ctrl + b first, then press shift + %
Split screen up and down: press ctrl + b first, then shift + "
Move the cursor up, down, left, and right: ctrl + b and then press the up, down, left, and right arrow keys
delete一个分割: cursor movement到那个分割屏幕,然后按ctrl +d
Add a new window (add a new page): ctrl+b then press c
Return to the previous page: ctrl+b then press p
Return to the next page: ctrl+b then press n

Save the current tmux state, and exit tmux:
Use: ctrl+b then d

View current saved session state
[[email protected] ~]# tmux ls
0: 2 windows (created Thu Oct 15 16:31:27 2020) [139x22]
1: 2 windows (created Thu Oct 15 16:45:40 2020) [139x22]

Enter the first session state:
tmux a -t 0

set uptmux,跟vim的last row mode很像:
Press ctrl+b, then press: (then there will be a yellow bar at the bottom of the window)
set up同步:  ctrl+b 然后按:  然后输入set synchronize-panes on
                        unsync set synchronize-panes off

The tmux terminal closes, not the tmux session closes. The program executed before that will continue to execute.
Without using tmux, by default, the terminal is closed, and the program being executed is also closed.
file encoding
    iconv -f utf-8 -t gbk file.txt > file2.txt
    View system locale
        locale
    ascii
    unicode
        utf-8
        utf-16
        gbk
        gb2132