<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.9.3">Jekyll</generator><link href="https://gemhowie.github.io/blog/feed.xml" rel="self" type="application/atom+xml" /><link href="https://gemhowie.github.io/blog/" rel="alternate" type="text/html" /><updated>2023-11-12T15:23:18+08:00</updated><id>https://gemhowie.github.io/blog/feed.xml</id><title type="html">豪伊</title><subtitle>howie的个人博客</subtitle><author><name>gemhowie</name></author><entry><title type="html">Ubuntu 18.04 LTS 配置</title><link href="https://gemhowie.github.io/blog/2018/04/27/ubuntu-18.04-LTS-config/" rel="alternate" type="text/html" title="Ubuntu 18.04 LTS 配置" /><published>2018-04-27T00:00:00+08:00</published><updated>2018-04-27T00:00:00+08:00</updated><id>https://gemhowie.github.io/blog/2018/04/27/ubuntu-18.04-LTS-config</id><content type="html" xml:base="https://gemhowie.github.io/blog/2018/04/27/ubuntu-18.04-LTS-config/">&lt;p&gt;安装完 Ubuntu 18.04 LTS 后进行一些必要的配置，方便使用。&lt;/p&gt;

&lt;h1 id=&quot;将你的硬件时钟设置为本地时区&quot;&gt;将你的硬件时钟设置为本地时区&lt;/h1&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;timedatectl set-local-rtc 1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;更改源为阿里云&quot;&gt;更改源为阿里云&lt;/h1&gt;

&lt;p&gt;在 应用程序 &amp;gt; 软件和更新 中更改源为阿里云 mirrors.aliyun.com&lt;/p&gt;

&lt;h1 id=&quot;安装aptitude&quot;&gt;安装aptitude&lt;/h1&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt-get &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;aptitude
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;安装vim&quot;&gt;安装vim&lt;/h1&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt-get &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;vim-gnome
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;vim配置&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;vim ~/.vimrc
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;添加&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;
&quot; Maintainer:
&quot;       Amir Salihefendic — @amix3k
&quot;
&quot; Awesome_version:
&quot;       Get this config, nice color schemes and lots of plugins!
&quot;
&quot;       Install the awesome version from:
&quot;
&quot;           https://github.com/amix/vimrc
&quot;
&quot; Sections:
&quot;    -&amp;gt; General
&quot;    -&amp;gt; VIM user interface
&quot;    -&amp;gt; Colors and Fonts
&quot;    -&amp;gt; Files and backups
&quot;    -&amp;gt; Text, tab and indent related
&quot;    -&amp;gt; Visual mode related
&quot;    -&amp;gt; Moving around, tabs and buffers
&quot;    -&amp;gt; Status line
&quot;    -&amp;gt; Editing mappings
&quot;    -&amp;gt; vimgrep searching and cope displaying
&quot;    -&amp;gt; Spell checking
&quot;    -&amp;gt; Misc
&quot;    -&amp;gt; Helper functions
&quot;
&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;


&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;
&quot; =&amp;gt; Plugins
&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;
&quot;call plug#begin('~/.config/nvim/plugged')
call plug#begin('~/AppData/Local/nvim/plugged')
Plug 'nlknguyen/papercolor-theme'
Plug 'tpope/vim-unimpaired'
Plug 'tpope/vim-surround'
Plug 'tpope/vim-fugitive'
Plug 'rickhowe/diffchar.vim'
Plug 'preservim/nerdtree'
Plug 'preservim/nerdcommenter'
Plug 'vim-scripts/matchit.zip'
Plug 'mbbill/undotree'
Plug 'townk/vim-autoclose'
Plug 'terryma/vim-expand-region'
Plug 'kana/vim-textobj-user'
&quot;Plug 'vim-scripts/YankRing.vim'
Plug 'maxbrunsfeld/vim-yankstack'
Plug 'easymotion/vim-easymotion'
Plug 'yggdroot/leaderf', { 'do': ':leaderfinstallcextension' }
Plug 'neoclide/coc.nvim', {'branch': 'release'}
call plug#end()


&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;
&quot; =&amp;gt; general
&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;
&quot; 关闭兼容模式。开启兼容模式会影响大部分vim功能，所以需要首先关掉兼容模式
&quot; not compatible with the old-fashion vi mode
set nocompatible

&quot; 命令行历史条目数。详细见:help 'history'，其中描述片段：a history of ':' commands, and a history of previous search patterns is remembered.
&quot; sets how many lines of history vim has to remember
set history=500

&quot; 开启文件类型检测
&quot; enable filetype detection
filetype on
&quot; 开启基于文件类型的缩进
&quot; enable filetype-specific indenting
filetype plugin on
&quot; 开启基于文件类型的插件
&quot; enable filetype-specific plugins
filetype indent on

&quot; 当文件被修改时重新加载文件，终端没效果待确认???
&quot; set to auto read when a file is changed from the outside
set autoread
au focusgained,bufenter * checktime

&quot; 设置leader键
&quot; with a map leader it's possible to do extra key combinations
&quot; like &amp;lt;leader&amp;gt;w saves the current file
let mapleader = &quot;,&quot;

&quot; fast saving
nmap &amp;lt;leader&amp;gt;w :w!&amp;lt;cr&amp;gt;

&quot; :W sudo saves the file
&quot; (useful for handling the permission-denied error)
command! W execute 'w !sudo tee % &amp;gt; /dev/null' &amp;lt;bar&amp;gt; edit!

&quot; 显示行号
&quot; show line numbers
set number

&quot; 显示相对行号
&quot; set relativenumber

&quot; vim和系统共用剪切板
set clipboard=unnamed


&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;
&quot; =&amp;gt; VIM user interface
&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;
&quot; 光标距离屏幕顶部和底部7行时就开始滚动屏幕
&quot; Set 7 lines to the cursor - when moving vertically using j/k
set so=7

&quot; Avoid garbled characters in Chinese language windows OS
let $LANG='en'
set langmenu=en
source $VIMRUNTIME/delmenu.vim
source $VIMRUNTIME/menu.vim

&quot; 启用增强模式的命令行补全
&quot; Turn on the Wild menu
set wildmenu
&quot; 在命令行中输入命令时，按下wildchar键（默认为Tab）将自动补全命令和参数：此时将在命令行的上方显示可能的匹配项；继续按下wildchar键，可以遍历所有的匹配项；也可以使用方向键或者CTRL-P/CTRL-N键，在匹配列表中进行移动；最后点击回车键，选择需要的匹配项。
&quot; start wild expansion in the command line using &amp;lt;TAB&amp;gt;
set wildchar=&amp;lt;TAB&amp;gt;

&quot; &amp;lt;Tab&amp;gt;自动补全，文件名或目录补全的时候，忽略以下编译产生的文件中存在的字符串选项
&quot; Ignore compiled files
&quot;set wildignore=*.o,*~,*.pyc
set wildignore=*.o,*~,*.class,*.pyc,*.elc,*.a,*.lib
if has(&quot;win16&quot;) || has(&quot;win32&quot;)
    set wildignore+=.git\*,.hg\*,.svn\*
else
    set wildignore+=*/.git/*,*/.hg/*,*/.svn/*,*/.DS_Store
endif

&quot; 设置标尺，总是在右下角显示当前光标所在位置的信息
&quot;Always show current position
set ruler

&quot; Height of the command bar
set cmdheight=1

&quot; A buffer becomes hidden when it is abandoned
set hid

&quot; 允许退格键在插入模式中删除任意字符。:set backspace=2 相当于 :set backspace=indent,eol,start
&quot; Configure backspace so it acts as it should act
set backspace=eol,start,indent
set whichwrap+=&amp;lt;,&amp;gt;,h,l

&quot; 搜索忽略大小写
&quot; Ignore case when searching
set ignorecase

&quot; 智能开启大小写敏感搜索，想要大小写不敏感，搜索模式要全部小写，反之，搜索模式有一个大写字母，自动开启大小写敏感
&quot; When searching try to be smart about cases
set smartcase

&quot; 高亮显示搜索结果
&quot; Highlight search results
set hlsearch

&quot; 递增的搜索，/或?搜索时，输入字符的时候就立刻高亮显示当前所有匹配结果
&quot; Makes search act like search in modern browsers
set incsearch

&quot; Don't redraw while executing macros (good performance config)
set lazyredraw

&quot; For regular expressions turn magic on
set magic

&quot; 光标遇到圆括号、方括号、大括号时，自动高亮对应的另一个圆括号、方括号和大括号。
&quot; Show matching brackets when text indicator is over them
set showmatch
&quot; How many tenths of a second to blink when matching brackets
set mat=2

&quot; 关闭发生错误时的哔声
&quot; No annoying sound on errors
set noerrorbells
&quot; 关闭使用刷新屏幕来代替哔声，当设置了visualbell，会刷新屏幕来代替哔声
set novisualbell
&quot; 清空visualbell用来刷新屏幕的代码
set t_vb=
&quot; 超时时间，默认1000，单位ms。mapping 或者 key sequence 等待下一个字符的超时时间
&quot;set tm=500

&quot; Properly disable sound on errors on MacVim
if has(&quot;gui_macvim&quot;)
    autocmd GUIEnter * set vb t_vb=
endif


&quot; Add a bit extra margin to the left
set foldcolumn=1


&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;
&quot; =&amp;gt; Colors and Fonts
&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;
&quot; 打开语法高亮。自动识别代码，使用多种颜色显示。
&quot; Enable syntax highlighting
syntax enable

&quot;&quot; Enable 256 colors palette in Gnome Terminal
&quot;if $COLORTERM == 'gnome-terminal'
&quot;    set t_Co=256
&quot;endif
&quot;
&quot;try
&quot;&quot;    colorscheme desert
&quot;    colorscheme molokai
&quot;catch
&quot;endtry
&quot;
&quot;set background=dark
&quot;
&quot;&quot; Set extra options when running in GUI mode
&quot;if has(&quot;gui_running&quot;)
&quot;    set guioptions-=T
&quot;    set guioptions-=e
&quot;    set t_Co=256
&quot;    set guitablabel=%M\ %t
&quot;endif

&quot; Set utf8 as standard encoding and en_US as the standard language
&quot;set encoding=utf8
set encoding=utf-8
set termencoding=utf-8
set fileencoding=utf-8
set fileencodings=ucs-bom,utf-8,big5,gb2312,latin1

&quot; Use Unix as the standard file type
set ffs=unix,dos,mac

&quot; 主题和字体
if has(&quot;gui_running&quot;) &quot; GUI color and font settings
  set background=dark
  set t_Co=256          &quot; 256 color mode
  &quot; NO menu,toolbar ...
  set guioptions-=m
  set guioptions-=T
  set guioptions-=l
  set guioptions-=L
  set guioptions-=r
  set guioptions-=R

  if has(&quot;win32&quot;)
    &quot;start gvim maximized
    if has(&quot;autocmd&quot;)
      au GUIEnter * simalt ~x
    endif
  endif
  colorscheme molokai
else
  &quot; terminal color settings
  &quot;colorscheme evening
  &quot;colorscheme monokai

  set background=light
  colorscheme PaperColor
  &quot;colorscheme gruvbox
endif

&quot; 81个字符高亮显示
&quot; 


&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;
&quot; =&amp;gt; Files, backups and undo
&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;
&quot; 关闭自动备份，备份的文件名通常是原文件名后面加一个~
&quot; Turn backup off, since most stuff is in SVN, git etc. anyway...
set nobackup
set nowb
set noswapfile

&quot; semicolon 分号，读音为 send 米 call 冷(len三声)
&quot; https://vim.fandom.com/wiki/Single_tags_file_for_a_source_tree
&quot; set autochdir 自动切换工作目录。这主要用在一个 Vim 会话之中打开多个文件的情况，默认的工作目录是打开的第一个文件的目录。该配置可以将工作目录自动切换到，正在编辑的文件的目录。
&quot; vim当前工作目录和文件所在目录是有区别的，可以通过设置set autochdir来自动切换vim当前工作目录
&quot; 设置vim搜索ctags生成的tag文件时的行为，./ 告诉vim从当前文件所在目录开始寻找，而不是vim的工作目录
set tags=./tags;


&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;
&quot; =&amp;gt; Text, tab and indent related
&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;
&quot; 使用空格代替 Tab
&quot; Use spaces instead of tabs
set expandtab

&quot; Tab 转为多少个空格。
set softtabstop=4

&quot; 智能的tab，具体功能待确认???
&quot; Be smart when using tabs ;)
set smarttab

&quot; 在文本上按下&amp;gt;&amp;gt;（增加一级缩进）、&amp;lt;&amp;lt;（取消一级缩进）或者==（自动缩进）时，每一级的字符数。
&quot; 1 tab == 4 spaces
set shiftwidth=4
set tabstop=4

&quot; Linebreak on 500 characters
set lbr
set tw=500

&quot; 自动缩进，按下回车键后，下一行的缩进会自动跟上一行的缩进保持一致。
&quot; autoindent
set ai
&quot; smartindent
set si
&quot; wrap lines
set wrap


&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;
&quot; =&amp;gt; Visual mode related
&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;
&quot; Visual mode pressing * or # searches for the current selection
&quot; Super useful! From an idea by Michael Naumann
vnoremap &amp;lt;silent&amp;gt; * :&amp;lt;C-u&amp;gt;call VisualSelection('', '')&amp;lt;CR&amp;gt;/&amp;lt;C-R&amp;gt;=@/&amp;lt;CR&amp;gt;&amp;lt;CR&amp;gt;
vnoremap &amp;lt;silent&amp;gt; # :&amp;lt;C-u&amp;gt;call VisualSelection('', '')&amp;lt;CR&amp;gt;?&amp;lt;C-R&amp;gt;=@/&amp;lt;CR&amp;gt;&amp;lt;CR&amp;gt;

&quot; 在visual mode下让 *(#) 也能正常搜索
&quot; vnoremap * y/&amp;lt;C-r&amp;gt;0&amp;lt;CR&amp;gt;
&quot; vnoremap # y?&amp;lt;C-r&amp;gt;0&amp;lt;CR&amp;gt;

&quot; 支持使用鼠标
set mouse=a


&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;
&quot; =&amp;gt; Moving around, tabs, windows and buffers
&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;
&quot; Map &amp;lt;Space&amp;gt; to / (search) and Ctrl-&amp;lt;Space&amp;gt; to ? (backwards search)
&quot;map &amp;lt;space&amp;gt; /
&quot;map &amp;lt;C-space&amp;gt; ?

&quot; Disable highlight when &amp;lt;leader&amp;gt;&amp;lt;cr&amp;gt; is pressed
map &amp;lt;silent&amp;gt; &amp;lt;leader&amp;gt;&amp;lt;cr&amp;gt; :noh&amp;lt;cr&amp;gt;

&quot; Smart way to move between windows
map &amp;lt;C-j&amp;gt; &amp;lt;C-W&amp;gt;j
map &amp;lt;C-k&amp;gt; &amp;lt;C-W&amp;gt;k
map &amp;lt;C-h&amp;gt; &amp;lt;C-W&amp;gt;h
map &amp;lt;C-l&amp;gt; &amp;lt;C-W&amp;gt;l

&quot; 切换到当前文件所在目录
nmap &amp;lt;leader&amp;gt;cd :cd %:p:h&amp;lt;cr&amp;gt;

&quot; 窗口打开关
nmap &amp;lt;leader&amp;gt;xs :write&amp;lt;CR&amp;gt;
nmap &amp;lt;leader&amp;gt;sd :vs&amp;lt;CR&amp;gt;
nmap &amp;lt;leader&amp;gt;s3 :vs&amp;lt;CR&amp;gt;
nmap &amp;lt;leader&amp;gt;sa :sp&amp;lt;CR&amp;gt;
nmap &amp;lt;leader&amp;gt;s2 :sp&amp;lt;CR&amp;gt;
nmap &amp;lt;leader&amp;gt;s1 &amp;lt;C-W&amp;gt;o
nmap &amp;lt;leader&amp;gt;sq :close&amp;lt;CR&amp;gt;
nmap &amp;lt;leader&amp;gt;s0 :close&amp;lt;CR&amp;gt;
nmap &amp;lt;leader&amp;gt;x3 :vs&amp;lt;CR&amp;gt;
nmap &amp;lt;leader&amp;gt;x2 :sp&amp;lt;CR&amp;gt;
nmap &amp;lt;leader&amp;gt;x1 &amp;lt;C-W&amp;gt;o
nmap &amp;lt;leader&amp;gt;x0 :close&amp;lt;CR&amp;gt;

&quot; 窗口移动
noremap &amp;lt;leader&amp;gt;wh &amp;lt;C-W&amp;gt;h
noremap &amp;lt;leader&amp;gt;wj &amp;lt;C-W&amp;gt;j
noremap &amp;lt;leader&amp;gt;wk &amp;lt;C-W&amp;gt;k
noremap &amp;lt;leader&amp;gt;wl &amp;lt;C-W&amp;gt;l
noremap &amp;lt;leader&amp;gt;ww &amp;lt;C-W&amp;gt;w
noremap &amp;lt;leader&amp;gt;wc &amp;lt;C-W&amp;gt;c
noremap &amp;lt;leader&amp;gt;wo &amp;lt;C-W&amp;gt;o

&quot; Do :help cope if you are unsure what cope is. It's super useful!
&quot; 打开Quickfix List，通常是make之后，显示所有编译的错误，方便进入修改???
nmap &amp;lt;leader&amp;gt;co :botright copen&amp;lt;cr&amp;gt;

&quot; Bash like keys for the command line
cnoremap &amp;lt;C-A&amp;gt;      &amp;lt;Home&amp;gt;
cnoremap &amp;lt;C-E&amp;gt;      &amp;lt;End&amp;gt;
cnoremap &amp;lt;C-K&amp;gt;      &amp;lt;C-U&amp;gt;
&quot; cnoremap &amp;lt;C-F&amp;gt;      &amp;lt;Right&amp;gt;
&quot; cnoremap &amp;lt;C-B&amp;gt;      &amp;lt;Left&amp;gt;
&quot; &amp;lt;C-F&amp;gt; 原有功能，列出之前输入过的命令，可以移动选择回车执行，比较有用，左右移动用的比较少，暂时不做映射
&quot; &amp;lt;C-A&amp;gt; 原来似乎没有作用
&quot; &amp;lt;C-E&amp;gt; 和&amp;lt;End&amp;gt;的功能一样，也是跳到行尾
&quot; &amp;lt;C-K&amp;gt; 是将按键的 字符表示 打印出来，Linux的&amp;lt;C-K&amp;gt;是删除光标之后的所有字符，但是vim命令行好像没有对应的功能，所以替换成&amp;lt;C-U&amp;gt;
&quot; &amp;lt;C-U&amp;gt; 是删除光标前的所有字符，和Linux命令一样

&quot; Close the current buffer
map &amp;lt;leader&amp;gt;bd :Bclose&amp;lt;cr&amp;gt;:tabclose&amp;lt;cr&amp;gt;gT

&quot; Close all the buffers
map &amp;lt;leader&amp;gt;ba :bufdo bd&amp;lt;cr&amp;gt;

map &amp;lt;leader&amp;gt;l :bnext&amp;lt;cr&amp;gt;
map &amp;lt;leader&amp;gt;h :bprevious&amp;lt;cr&amp;gt;

&quot; Useful mappings for managing tabs
&quot; 打开一个新的标签页
map &amp;lt;leader&amp;gt;tn :tabnew&amp;lt;cr&amp;gt;
&quot; 关闭所有其他标签页，只保留当前标签页
map &amp;lt;leader&amp;gt;to :tabonly&amp;lt;cr&amp;gt;
&quot; 关闭当前标签页
map &amp;lt;leader&amp;gt;tc :tabclose&amp;lt;cr&amp;gt;
&quot; 把标签页向右移动
map &amp;lt;leader&amp;gt;tm :tabmove&amp;lt;cr&amp;gt;
&quot; 移动到下一个标签页
map &amp;lt;leader&amp;gt;t&amp;lt;leader&amp;gt; :tabnext&amp;lt;cr&amp;gt;

&quot; Let 'tl' toggle between this and the last accessed tab
let g:lasttab = 1
nmap &amp;lt;Leader&amp;gt;tl :exe &quot;tabn &quot;.g:lasttab&amp;lt;CR&amp;gt;
au TabLeave * let g:lasttab = tabpagenr()


&quot; Opens a new tab with the current buffer's path
&quot; Super useful when editing files in the same directory
map &amp;lt;leader&amp;gt;te :tabedit &amp;lt;C-r&amp;gt;=expand(&quot;%:p:h&quot;)&amp;lt;cr&amp;gt;/

&quot; Switch CWD to the directory of the open buffer
map &amp;lt;leader&amp;gt;cd :cd %:p:h&amp;lt;cr&amp;gt;:pwd&amp;lt;cr&amp;gt;

&quot; Specify the behavior when switching between buffers
try
  set switchbuf=useopen,usetab,newtab
  set stal=2
catch
endtry

&quot; Return to last edit position when opening files (You want this!)
au BufReadPost * if line(&quot;'\&quot;&quot;) &amp;gt; 1 &amp;amp;&amp;amp; line(&quot;'\&quot;&quot;) &amp;lt;= line(&quot;$&quot;) | exe &quot;normal! g'\&quot;&quot; | endif


&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;
&quot; =&amp;gt; Status line
&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;
&quot; Always show the status line
&quot;set laststatus=2

&quot; Format the status line
&quot;set statusline=\ %{HasPaste()}%F%m%r%h\ %w\ \ CWD:\ %r%{getcwd()}%h\ \ \ Line:\ %l\ \ Column:\ %c

&quot; 状态栏设置
&quot; status line {
&quot; 是否显示状态栏。0 表示不显示，1 表示只在多窗口时显示，2 表示显示。
set laststatus=2
&quot;set statusline=%-10.3n  &quot;buffer number
set statusline=\ [%n]\   &quot;buffer number
&quot; 仅显示文件名，想要显示完整路径，可改为%F
set statusline+=%t       &quot;tail of the filename
&quot; 文件编码
set statusline+=[%{strlen(&amp;amp;fenc)?&amp;amp;fenc:'none'}, &quot;file encoding
&quot; 文件格式 unix dos
set statusline+=%{&amp;amp;ff}] &quot;file format
&quot; 帮助缓冲标记
set statusline+=%h      &quot;help file flag
&quot; 当前缓冲被修改标记
set statusline+=%m      &quot;modified flag
&quot; 当前缓冲只读标记
set statusline+=%r      &quot;read only flag
&quot; 文件类型
set statusline+=%y      &quot;filetype
&quot; ，左右分隔符，将剩下的显示项分割到右边
set statusline+=%=      &quot;left/right separator
&quot; 光标所在的列数
set statusline+=%c      &quot;cursor column
&quot; 空格分隔符
set statusline+=\       &quot;space separator
&quot; 当前光标所在行号/总行数
set statusline+=[%l/%L]
&quot; 当前光标所在文件位置的百分比
set statusline+=\ %P    &quot;percent through file


&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;
&quot; =&amp;gt; Editing mappings
&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;
&quot; Remap VIM 0 to first non-blank character
map 0 ^

&quot; Move a line of text using ALT+[jk] or Command+[jk] on mac
nmap &amp;lt;M-j&amp;gt; mz:m+&amp;lt;cr&amp;gt;`z
nmap &amp;lt;M-k&amp;gt; mz:m-2&amp;lt;cr&amp;gt;`z
vmap &amp;lt;M-j&amp;gt; :m'&amp;gt;+&amp;lt;cr&amp;gt;`&amp;lt;my`&amp;gt;mzgv`yo`z
vmap &amp;lt;M-k&amp;gt; :m'&amp;lt;-2&amp;lt;cr&amp;gt;`&amp;gt;my`&amp;lt;mzgv`yo`z

if has(&quot;mac&quot;) || has(&quot;macunix&quot;)
  nmap &amp;lt;D-j&amp;gt; &amp;lt;M-j&amp;gt;
  nmap &amp;lt;D-k&amp;gt; &amp;lt;M-k&amp;gt;
  vmap &amp;lt;D-j&amp;gt; &amp;lt;M-j&amp;gt;
  vmap &amp;lt;D-k&amp;gt; &amp;lt;M-k&amp;gt;
endif

&quot; Delete trailing white space on save, useful for some filetypes ;)
fun! CleanExtraSpaces()
    let save_cursor = getpos(&quot;.&quot;)
    let old_query = getreg('/')
    silent! %s/\s\+$//e
    call setpos('.', save_cursor)
    call setreg('/', old_query)
endfun

if has(&quot;autocmd&quot;)
    autocmd BufWritePre *.txt,*.js,*.py,*.wiki,*.sh,*.coffee :call CleanExtraSpaces()
endif

inoremap kj &amp;lt;ESC&amp;gt;

&quot; 把行尾多余的空格删除
nmap &amp;lt;leader&amp;gt;tr :%s/ \+$//g&amp;lt;CR&amp;gt;

&quot; 挂起vim进程，同ctrl+z，fg还原
nmap &amp;lt;leader&amp;gt;xc :suspend&amp;lt;CR&amp;gt;


&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;
&quot; =&amp;gt; Spell checking
&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;
&quot; Pressing ,ss will toggle and untoggle spell checking
&quot;map &amp;lt;leader&amp;gt;ss :setlocal spell!&amp;lt;cr&amp;gt;

&quot; Shortcuts using &amp;lt;leader&amp;gt;
&quot;map &amp;lt;leader&amp;gt;sn ]s
&quot;map &amp;lt;leader&amp;gt;sp [s
&quot;map &amp;lt;leader&amp;gt;sa zg
&quot;map &amp;lt;leader&amp;gt;s? z=


&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;
&quot; =&amp;gt; Misc
&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;
&quot; Remove the Windows ^M - when the encodings gets messed up
noremap &amp;lt;Leader&amp;gt;m mmHmt:%s/&amp;lt;C-V&amp;gt;&amp;lt;cr&amp;gt;//ge&amp;lt;cr&amp;gt;'tzt'm

&quot; Quickly open a buffer for scribble
&quot;map &amp;lt;leader&amp;gt;q :e ~/buffer&amp;lt;cr&amp;gt;

&quot; Quickly open a markdown buffer for scribble
&quot;map &amp;lt;leader&amp;gt;x :e ~/buffer.md&amp;lt;cr&amp;gt;

&quot; 粘贴模式开关，解决粘贴乱码问题???
&quot; Toggle paste mode on and off
map &amp;lt;leader&amp;gt;pp :setlocal paste!&amp;lt;cr&amp;gt;


&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;
&quot; =&amp;gt; Helper functions
&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;
&quot; Returns true if paste mode is enabled
function! HasPaste()
    if &amp;amp;paste
        return 'PASTE MODE  '
    endif
    return ''
endfunction

&quot; Don't close window, when deleting a buffer
command! Bclose call &amp;lt;SID&amp;gt;BufcloseCloseIt()
function! &amp;lt;SID&amp;gt;BufcloseCloseIt()
    let l:currentBufNum = bufnr(&quot;%&quot;)
    let l:alternateBufNum = bufnr(&quot;#&quot;)

    if buflisted(l:alternateBufNum)
        buffer #
    else
        bnext
    endif

    if bufnr(&quot;%&quot;) == l:currentBufNum
        new
    endif

    if buflisted(l:currentBufNum)
        execute(&quot;bdelete! &quot;.l:currentBufNum)
    endif
endfunction

function! CmdLine(str)
    call feedkeys(&quot;:&quot; . a:str)
endfunction

function! VisualSelection(direction, extra_filter) range
    let l:saved_reg = @&quot;
    execute &quot;normal! vgvy&quot;

    let l:pattern = escape(@&quot;, &quot;\\/.*'$^~[]&quot;)
    let l:pattern = substitute(l:pattern, &quot;\n$&quot;, &quot;&quot;, &quot;&quot;)

    if a:direction == 'gv'
        call CmdLine(&quot;Ack '&quot; . l:pattern . &quot;' &quot; )
    elseif a:direction == 'replace'
        call CmdLine(&quot;%s&quot; . '/'. l:pattern . '/')
    endif

    let @/ = l:pattern
    let @&quot; = l:saved_reg
endfunction


&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;
&quot; =&amp;gt; coc.nvim
&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;
&quot; Set internal encoding of vim, not needed on neovim, since coc.nvim using some
&quot; unicode characters in the file autoload/float.vim
set encoding=utf-8

&quot; TextEdit might fail if hidden is not set.
set hidden

&quot; Some servers have issues with backup files, see #649.
set nobackup
set nowritebackup

&quot; Give more space for displaying messages.
set cmdheight=2

&quot; Having longer updatetime (default is 4000 ms = 4 s) leads to noticeable
&quot; delays and poor user experience.
set updatetime=300

&quot; Don't pass messages to |ins-completion-menu|.
set shortmess+=c

&quot; Always show the signcolumn, otherwise it would shift the text each time
&quot; diagnostics appear/become resolved.
if has(&quot;patch-8.1.1564&quot;)
  &quot; Recently vim can merge signcolumn and number column into one
  set signcolumn=number
else
  set signcolumn=yes
endif

&quot; Use tab for trigger completion with characters ahead and navigate.
&quot; NOTE: Use command ':verbose imap &amp;lt;tab&amp;gt;' to make sure tab is not mapped by
&quot; other plugin before putting this into your config.
inoremap &amp;lt;silent&amp;gt;&amp;lt;expr&amp;gt; &amp;lt;TAB&amp;gt;
      \ pumvisible() ? &quot;\&amp;lt;C-n&amp;gt;&quot; :
      \ &amp;lt;SID&amp;gt;check_back_space() ? &quot;\&amp;lt;TAB&amp;gt;&quot; :
      \ coc#refresh()
inoremap &amp;lt;expr&amp;gt;&amp;lt;S-TAB&amp;gt; pumvisible() ? &quot;\&amp;lt;C-p&amp;gt;&quot; : &quot;\&amp;lt;C-h&amp;gt;&quot;

function! s:check_back_space() abort
  let col = col('.') - 1
  return !col || getline('.')[col - 1]  =~# '\s'
endfunction

&quot; Use &amp;lt;c-space&amp;gt; to trigger completion.
if has('nvim')
  inoremap &amp;lt;silent&amp;gt;&amp;lt;expr&amp;gt; &amp;lt;c-space&amp;gt; coc#refresh()
else
  inoremap &amp;lt;silent&amp;gt;&amp;lt;expr&amp;gt; &amp;lt;c-@&amp;gt; coc#refresh()
endif

&quot; Make &amp;lt;CR&amp;gt; auto-select the first completion item and notify coc.nvim to
&quot; format on enter, &amp;lt;cr&amp;gt; could be remapped by other vim plugin
inoremap &amp;lt;silent&amp;gt;&amp;lt;expr&amp;gt; &amp;lt;cr&amp;gt; pumvisible() ? coc#_select_confirm()
                              \: &quot;\&amp;lt;C-g&amp;gt;u\&amp;lt;CR&amp;gt;\&amp;lt;c-r&amp;gt;=coc#on_enter()\&amp;lt;CR&amp;gt;&quot;

&quot; Use `[g` and `]g` to navigate diagnostics
&quot; Use `:CocDiagnostics` to get all diagnostics of current buffer in location list.
nmap &amp;lt;silent&amp;gt; [g &amp;lt;Plug&amp;gt;(coc-diagnostic-prev)
nmap &amp;lt;silent&amp;gt; ]g &amp;lt;Plug&amp;gt;(coc-diagnostic-next)

&quot; GoTo code navigation.
nmap &amp;lt;silent&amp;gt; gd &amp;lt;Plug&amp;gt;(coc-definition)
nmap &amp;lt;silent&amp;gt; gy &amp;lt;Plug&amp;gt;(coc-type-definition)
nmap &amp;lt;silent&amp;gt; gi &amp;lt;Plug&amp;gt;(coc-implementation)
nmap &amp;lt;silent&amp;gt; gr &amp;lt;Plug&amp;gt;(coc-references)

&quot; Use K to show documentation in preview window.
nnoremap &amp;lt;silent&amp;gt; K :call &amp;lt;SID&amp;gt;show_documentation()&amp;lt;CR&amp;gt;

function! s:show_documentation()
  if (index(['vim','help'], &amp;amp;filetype) &amp;gt;= 0)
    execute 'h '.expand('&amp;lt;cword&amp;gt;')
  elseif (coc#rpc#ready())
    call CocActionAsync('doHover')
  else
    execute '!' . &amp;amp;keywordprg . &quot; &quot; . expand('&amp;lt;cword&amp;gt;')
  endif
endfunction

&quot; Highlight the symbol and its references when holding the cursor.
autocmd CursorHold * silent call CocActionAsync('highlight')

&quot; Symbol renaming.
nmap &amp;lt;leader&amp;gt;rn &amp;lt;Plug&amp;gt;(coc-rename)

&quot; Formatting selected code.
xmap &amp;lt;leader&amp;gt;f  &amp;lt;Plug&amp;gt;(coc-format-selected)
nmap &amp;lt;leader&amp;gt;f  &amp;lt;Plug&amp;gt;(coc-format-selected)

augroup mygroup
  autocmd!
  &quot; Setup formatexpr specified filetype(s).
  autocmd FileType typescript,json setl formatexpr=CocAction('formatSelected')
  &quot; Update signature help on jump placeholder.
  autocmd User CocJumpPlaceholder call CocActionAsync('showSignatureHelp')
augroup end

&quot; Applying codeAction to the selected region.
&quot; Example: `&amp;lt;leader&amp;gt;aap` for current paragraph
xmap &amp;lt;leader&amp;gt;a  &amp;lt;Plug&amp;gt;(coc-codeaction-selected)
nmap &amp;lt;leader&amp;gt;a  &amp;lt;Plug&amp;gt;(coc-codeaction-selected)

&quot; Remap keys for applying codeAction to the current buffer.
nmap &amp;lt;leader&amp;gt;ac  &amp;lt;Plug&amp;gt;(coc-codeaction)
&quot; Apply AutoFix to problem on the current line.
nmap &amp;lt;leader&amp;gt;qf  &amp;lt;Plug&amp;gt;(coc-fix-current)

&quot; Map function and class text objects
&quot; NOTE: Requires 'textDocument.documentSymbol' support from the language server.
xmap if &amp;lt;Plug&amp;gt;(coc-funcobj-i)
omap if &amp;lt;Plug&amp;gt;(coc-funcobj-i)
xmap af &amp;lt;Plug&amp;gt;(coc-funcobj-a)
omap af &amp;lt;Plug&amp;gt;(coc-funcobj-a)
xmap ic &amp;lt;Plug&amp;gt;(coc-classobj-i)
omap ic &amp;lt;Plug&amp;gt;(coc-classobj-i)
xmap ac &amp;lt;Plug&amp;gt;(coc-classobj-a)
omap ac &amp;lt;Plug&amp;gt;(coc-classobj-a)

&quot; Remap &amp;lt;C-f&amp;gt; and &amp;lt;C-b&amp;gt; for scroll float windows/popups.
if has('nvim-0.4.0') || has('patch-8.2.0750')
  nnoremap &amp;lt;silent&amp;gt;&amp;lt;nowait&amp;gt;&amp;lt;expr&amp;gt; &amp;lt;C-f&amp;gt; coc#float#has_scroll() ? coc#float#scroll(1) : &quot;\&amp;lt;C-f&amp;gt;&quot;
  nnoremap &amp;lt;silent&amp;gt;&amp;lt;nowait&amp;gt;&amp;lt;expr&amp;gt; &amp;lt;C-b&amp;gt; coc#float#has_scroll() ? coc#float#scroll(0) : &quot;\&amp;lt;C-b&amp;gt;&quot;
  inoremap &amp;lt;silent&amp;gt;&amp;lt;nowait&amp;gt;&amp;lt;expr&amp;gt; &amp;lt;C-f&amp;gt; coc#float#has_scroll() ? &quot;\&amp;lt;c-r&amp;gt;=coc#float#scroll(1)\&amp;lt;cr&amp;gt;&quot; : &quot;\&amp;lt;Right&amp;gt;&quot;
  inoremap &amp;lt;silent&amp;gt;&amp;lt;nowait&amp;gt;&amp;lt;expr&amp;gt; &amp;lt;C-b&amp;gt; coc#float#has_scroll() ? &quot;\&amp;lt;c-r&amp;gt;=coc#float#scroll(0)\&amp;lt;cr&amp;gt;&quot; : &quot;\&amp;lt;Left&amp;gt;&quot;
  vnoremap &amp;lt;silent&amp;gt;&amp;lt;nowait&amp;gt;&amp;lt;expr&amp;gt; &amp;lt;C-f&amp;gt; coc#float#has_scroll() ? coc#float#scroll(1) : &quot;\&amp;lt;C-f&amp;gt;&quot;
  vnoremap &amp;lt;silent&amp;gt;&amp;lt;nowait&amp;gt;&amp;lt;expr&amp;gt; &amp;lt;C-b&amp;gt; coc#float#has_scroll() ? coc#float#scroll(0) : &quot;\&amp;lt;C-b&amp;gt;&quot;
endif

&quot; Use CTRL-S for selections ranges.
&quot; Requires 'textDocument/selectionRange' support of language server.
nmap &amp;lt;silent&amp;gt; &amp;lt;C-s&amp;gt; &amp;lt;Plug&amp;gt;(coc-range-select)
xmap &amp;lt;silent&amp;gt; &amp;lt;C-s&amp;gt; &amp;lt;Plug&amp;gt;(coc-range-select)

&quot; Add `:Format` command to format current buffer.
command! -nargs=0 Format :call CocAction('format')

&quot; Add `:Fold` command to fold current buffer.
command! -nargs=? Fold :call     CocAction('fold', &amp;lt;f-args&amp;gt;)

&quot; Add `:OR` command for organize imports of the current buffer.
command! -nargs=0 OR   :call     CocAction('runCommand', 'editor.action.organizeImport')

&quot; Add (Neo)Vim's native statusline support.
&quot; NOTE: Please see `:h coc-status` for integrations with external plugins that
&quot; provide custom statusline: lightline.vim, vim-airline.
set statusline^=%{coc#status()}%{get(b:,'coc_current_function','')}

&quot; Mappings for CoCList
&quot; Show all diagnostics.
nnoremap &amp;lt;silent&amp;gt;&amp;lt;nowait&amp;gt; &amp;lt;space&amp;gt;a  :&amp;lt;C-u&amp;gt;CocList diagnostics&amp;lt;cr&amp;gt;
&quot; Manage extensions.
nnoremap &amp;lt;silent&amp;gt;&amp;lt;nowait&amp;gt; &amp;lt;space&amp;gt;e  :&amp;lt;C-u&amp;gt;CocList extensions&amp;lt;cr&amp;gt;
&quot; Show commands.
nnoremap &amp;lt;silent&amp;gt;&amp;lt;nowait&amp;gt; &amp;lt;space&amp;gt;c  :&amp;lt;C-u&amp;gt;CocList commands&amp;lt;cr&amp;gt;
&quot; Find symbol of current document.
nnoremap &amp;lt;silent&amp;gt;&amp;lt;nowait&amp;gt; &amp;lt;space&amp;gt;o  :&amp;lt;C-u&amp;gt;CocList outline&amp;lt;cr&amp;gt;
&quot; Search workspace symbols.
nnoremap &amp;lt;silent&amp;gt;&amp;lt;nowait&amp;gt; &amp;lt;space&amp;gt;s  :&amp;lt;C-u&amp;gt;CocList -I symbols&amp;lt;cr&amp;gt;
&quot; Do default action for next item.
nnoremap &amp;lt;silent&amp;gt;&amp;lt;nowait&amp;gt; &amp;lt;space&amp;gt;j  :&amp;lt;C-u&amp;gt;CocNext&amp;lt;CR&amp;gt;
&quot; Do default action for previous item.
nnoremap &amp;lt;silent&amp;gt;&amp;lt;nowait&amp;gt; &amp;lt;space&amp;gt;k  :&amp;lt;C-u&amp;gt;CocPrev&amp;lt;CR&amp;gt;
&quot; Resume latest coc list.
nnoremap &amp;lt;silent&amp;gt;&amp;lt;nowait&amp;gt; &amp;lt;space&amp;gt;p  :&amp;lt;C-u&amp;gt;CocListResume&amp;lt;CR&amp;gt;


&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;
&quot; =&amp;gt; Nerdtree
&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;
&quot; 自动开启Nerdtree
&quot;autocmd vimenter * NERDTree
&quot; 设定 NERDTree 视窗大小
&quot;let g:NERDTreeWinSize = 25
&quot; 开启/关闭nerdtree快捷键
nnoremap &amp;lt;leader&amp;gt;nt :NERDTreeToggle&amp;lt;CR&amp;gt;
&quot; 开启Nerdtree时自动显示Bookmarks
&quot;let NERDTreeShowBookmarks=1
&quot; 打开vim时如果没有文件自动打开NERDTree
autocmd vimenter * if !argc()|NERDTree|endif
&quot; 当NERDTree为剩下的唯一窗口时自动关闭
autocmd bufenter * if (winnr(&quot;$&quot;) == 1 &amp;amp;&amp;amp; exists(&quot;b:NERDTree&quot;) &amp;amp;&amp;amp; b:NERDTree.isTabTree()) | q | endif
&quot; 设置树的显示图标
let g:NERDTreeDirArrowExpandable = '▸'
let g:NERDTreeDirArrowCollapsible = '▾'
&quot; 不显示隐藏文件
let g:NERDTreeHidden=0
&quot; 过滤: 所有指定文件和文件夹不显示
let NERDTreeIgnore = ['\.pyc$', '\.swp', '\.swo', '\.vscode', '__pycache__']
&quot; 是否显示行号
&quot;let g:NERDTreeShowLineNumbers=1
&quot; Making it prettier
let NERDTreeMinimalUI = 1
let NERDTreeDirArrows = 1


&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;
&quot; =&amp;gt; LeaderF
&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;
&quot; let g:Lf_ShortcutF = '&amp;lt;c-p&amp;gt;'
nnoremap &amp;lt;leader&amp;gt;kk :LeaderfFile&amp;lt;CR&amp;gt;
nnoremap &amp;lt;leader&amp;gt;xf :LeaderfFile&amp;lt;CR&amp;gt;
&quot; 包含以下目录的标记为根目录
let g:Lf_RootMarkers = ['.git', '.svn', '.hg', '.project', '.root']
&quot; 先找当前文件的所有祖先目录里最近的根目录, 如果找到就使用这个根目录为查找起点(A的含义) 
&quot; 如果没找到任何根目录, 看看当前打开的文件是否在VIM的工作目录(working directory)下, 或者任何其子目录下。如果是, 就是用工作目录作为查找起点(F的含义)
let g:Lf_WorkingDirectoryMode = 'AF'

&quot; 索引文件工具
&quot; 如果LeaderF发现根目录是一个git或者hg的仓库(repository), 使用git或者hg作为索引文件的工具。这个是默认选项, 可以不写
let g:Lf_UseVersionControlTool=1
&quot; 如果Lf_UseVersionControlTool=0或者根目录不是仓库，会检查是否配置外部工具
&quot;let g:Lf_DefaultExternalTool='rg'
&quot; 如果以上都没有，则使用python脚本索引文件

if has('nvim')
    let s:cachedir = expand(stdpath('cache'))
    let s:configdir = expand(stdpath('config'))
else
    &quot;vim will share same folder with nvim
    if has('win32')
        let s:cachedir = expand('~/AppData/Local/Temp/cache/nvim')
        let s:configdir = expand('~/AppData/Local/nvim')
    else
        let s:cachedir = expand('~/.cache/nvim')
        let s:configdir = expand('~/.config/nvim')
    endif
endif
&quot; 设置1则显示隐藏文件，默认值0
&quot;g:Lf_ShowHidden = 0
&quot; 设置成1, 预览(preview)会在弹出(popup)窗口里显示, 而不会在原来的文件所在的窗口里显示. 默认值是0.
let g:Lf_PreviewInPopup = 1
&quot; 设置窗口高度
let g:Lf_WindowHeight = 0.30
&quot; 设置缓存路径
let g:Lf_CacheDirectory = s:cachedir
&quot; 设置状态栏配色方案
let g:Lf_StlColorscheme = 'powerline'
&quot; 设置哪几个功能自动显示preview
let g:Lf_PreviewResult = {
        \ 'File': 0,
        \ 'Buffer': 0,
        \ 'Mru': 0,
        \ 'Tag': 0,
        \ 'BufTag': 1,
        \ 'Function': 1,
        \ 'Line': 1,
        \ 'Colorscheme': 0,
        \ 'Rg': 0,
        \ 'Gtags': 0
        \}
&quot; 设置为1, 结果从下到上显示, 跟fzf/CtrlP一致, 默认是0, 从上倒下显示
&quot;let g:Lf_ReverseOrder = 0


&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;
&quot; =&amp;gt; Undotree
&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;
nnoremap &amp;lt;leader&amp;gt;ut :UndotreeToggle&amp;lt;CR&amp;gt;


&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;
&quot; =&amp;gt; vim-expand-region
&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;
map &amp;lt;leader&amp;gt;xx &amp;lt;Plug&amp;gt;(expand_region_expand)
map &amp;lt;leader&amp;gt;zz &amp;lt;Plug&amp;gt;(expand_region_shrink)
vmap v &amp;lt;Plug&amp;gt;(expand_region_expand)
vmap z &amp;lt;Plug&amp;gt;(expand_region_shrink)


&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;
&quot; =&amp;gt; vertical diffsplit
&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;
set diffopt+=vertical
&quot; better diff algorithm from xdiff
&quot; code checked into vim repo in c93262b2e
&quot; @see https://vimways.org/2018/the-power-of-diff/
&quot; @see https://raw.githubusercontent.com/ashfinal/vimrc-config/master/.vimrc
if has(&quot;patch-8.1.360&quot;) || has(&quot;nvim-0.3.5&quot;)
  set diffopt+=algorithm:histogram
  set diffopt+=indent-heuristic
endif


&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;
&quot; =&amp;gt; diffchar
&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;
&quot; [b ]b [e ]e 和 vim-unimpaired 冲突，加载插件的顺序放到 vim-unimpaired 之后，防止[b ]b [e ]e被覆盖
nmap &amp;lt;silent&amp;gt; [w &amp;lt;Plug&amp;gt;JumpDiffCharPrevStart
nmap &amp;lt;silent&amp;gt; ]w &amp;lt;Plug&amp;gt;JumpDiffCharNextStart
&quot;nmap &amp;lt;silent&amp;gt; [e &amp;lt;Plug&amp;gt;JumpDiffCharPrevEnd
&quot;nmap &amp;lt;silent&amp;gt; ]e &amp;lt;Plug&amp;gt;JumpDiffCharNextEnd
nmap &amp;lt;silent&amp;gt; &amp;lt;Leader&amp;gt;do &amp;lt;Plug&amp;gt;GetDiffCharPair
nmap &amp;lt;silent&amp;gt; &amp;lt;Leader&amp;gt;dp &amp;lt;Plug&amp;gt;PutDiffCharPair


&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;
&quot; =&amp;gt; vim-unimpaired
&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;
&quot; 补充官方丢失的[]相关命令，部分快捷键如下
&quot; ]b 和[b 循环遍历缓冲区。
&quot; ]e 和[e 交换上下两行。
&quot; ]f 和[f 循环遍历同一目录中的文件，并打开为当前缓冲区。
&quot; ]l 和[l 遍历位置列表。
&quot; ]q 和[q 遍历快速修复列表。
&quot; ]t 和[t 遍历标签列表。


&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;
&quot; =&amp;gt; vim-textobj-user
&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;
&quot; 和 coc classobj funcobj 冲突，暂时先屏蔽
&quot;call textobj#user#plugin('file', {
      &quot;\ 'file': {
      &quot;\ 'pattern': '\f\+', 'select': ['af', 'if']
      &quot;\ }
      &quot;\ })
&quot; C:\Users\Administrator\AppData\Local\nvim\init.vim
&quot; /root/init.vim /home/wh/demo.txt

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;安装git&quot;&gt;安装git&lt;/h1&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt-get &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;git
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;设置name，email，push&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;git config &lt;span class=&quot;nt&quot;&gt;--global&lt;/span&gt; user.name &amp;lt;name&amp;gt;
git config &lt;span class=&quot;nt&quot;&gt;--global&lt;/span&gt; user.email &amp;lt;email&amp;gt;
git config &lt;span class=&quot;nt&quot;&gt;--global&lt;/span&gt; push.default simple
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;使用ssh，生成公钥后在github上添加SSH keys&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ssh-keygen &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; rsa &lt;span class=&quot;nt&quot;&gt;-C&lt;/span&gt; &amp;lt;email&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;安装gnome-tweak-tool&quot;&gt;安装gnome-tweak-tool&lt;/h1&gt;

&lt;p&gt;Ubuntu 18.04 LTS 中 gnome 为默认桌面，安装优化工具可以进行高级设置&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt-get &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;gnome-tweak-tool
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;安装zsh--oh-my-zsh&quot;&gt;安装zsh &amp;amp; oh-my-zsh&lt;/h1&gt;

&lt;p&gt;安装zsh&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt-get &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;zsh
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;更改当前用户默认的shell为zsh&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;chsh &lt;span class=&quot;nt&quot;&gt;-s&lt;/span&gt; &lt;span class=&quot;si&quot;&gt;$(&lt;/span&gt;which zsh&lt;span class=&quot;si&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;安装oh-my-zsh&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sh &lt;span class=&quot;nt&quot;&gt;-c&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;$(&lt;/span&gt;wget https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh &lt;span class=&quot;nt&quot;&gt;-O&lt;/span&gt; -&lt;span class=&quot;si&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;修改主题为agnoster&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;vim ~/.zshrc

&lt;span class=&quot;c&quot;&gt;# 主题改为如下&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# ZSH_THEME=&quot;agnoster&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;安装 agnoster 主题依赖的字体 powerline ，并设置等宽字体，否则会出现乱码&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# clone&lt;/span&gt;
git clone https://github.com/powerline/fonts.git &lt;span class=&quot;nt&quot;&gt;--depth&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;1

&lt;span class=&quot;c&quot;&gt;# install&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;fonts
./install.sh

&lt;span class=&quot;c&quot;&gt;# clean-up a bit&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;cd&lt;/span&gt; ..
&lt;span class=&quot;nb&quot;&gt;rm&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-rf&lt;/span&gt; fonts
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;打开优化工具，等宽字体设置为 Ubuntu Mono derivative Powerline Regular&lt;/p&gt;

&lt;p&gt;注销后生效&lt;/p&gt;

&lt;h1 id=&quot;开启tcp-bbr拥塞控制算法&quot;&gt;开启TCP BBR拥塞控制算法&lt;/h1&gt;

&lt;p&gt;Linux kernel 4.9+ 已经支持&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tcp_bbr&lt;/code&gt;，ubuntu 18.04 默认使用4.15内核，可以直接开启&lt;/p&gt;

&lt;p&gt;写入配置文件&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;bash &lt;span class=&quot;nt&quot;&gt;-c&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'cat &amp;gt;&amp;gt; /etc/sysctl.conf'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;EOF&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;

net.core.default_qdisc=fq
net.ipv4.tcp_congestion_control=bbr
&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;EOF
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;让内核配置生效&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;sysctl &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;以下两条命令的结果中都有bbr，说明内核已经开启bbr&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sysctl net.ipv4.tcp_available_congestion_control
sysctl net.ipv4.tcp_congestion_control
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;以下命令看到有 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tcp_bbr&lt;/code&gt;，说明bbr已启动&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;lsmod | &lt;span class=&quot;nb&quot;&gt;grep &lt;/span&gt;bbr
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;我这边网络环境比较恶劣，开启bbr后大幅改善响应速度&lt;/p&gt;

&lt;h1 id=&quot;全局智能代理&quot;&gt;全局智能代理&lt;/h1&gt;

&lt;p&gt;流量分流方案: tun2socks + shadowsocks-libev&lt;/p&gt;

&lt;p&gt;常见透明代理方案(tcp+udp)使用ss-redir(运行在网关上，在mangle表PREROUTING链中用TPROXY转发内网udp)，但局限是无法转发网关自身的udp流量&lt;/p&gt;

&lt;p&gt;tun2socks支持转发本地udp，适用于想要把代理部署在本地的用户&lt;/p&gt;

&lt;p&gt;可选udp通路的丢包改善方案: UDPspeeder + udp2raw&lt;/p&gt;

&lt;p&gt;UDPspeeder提供了FEC(前向纠错)和多倍发包方案，可以在牺牲一定带宽的前提下大大降低丢包率(准确的说是在原始丢包率的网络状况下通过发送额外的冗余包来实现恢复出原始udp包，从而避免丢包重传)&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;优点: 降低丢包率，提高稳定性&lt;/li&gt;
  &lt;li&gt;缺点: 浪费额外带宽(也就意味着有效带宽减少，体现为udp下载速度变慢)，引入少量延迟&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;udp2raw可将udp包模拟成tcp，模拟三次握手，从而绕过ISP的qos限制，本质仍是udp&lt;/p&gt;

&lt;p&gt;DNS分流方案: overture&lt;/p&gt;

&lt;p&gt;overture是一个DNS分流净化软件，可配置多个上游DNS，自动选择最快应答，支持缓存，支持ECS，支持本地Host配置&lt;/p&gt;

&lt;p&gt;处理逻辑: 配置主从DNS集合，主DNS集为国内常用DNS服务提供商，本地ISP、DNSPod、AliDNS之流，从DNS一般配置一个支持ECS的DNS，主要用于查询国外域名，默认使用OpenDNS的443端口进行tcp查询。用户请求的域名如果在DomainFile中匹配，则直接使用从DNS解析。若主DNS解析得出的IP不在IPNetworkFile中，则使用从DNS解析&lt;/p&gt;

&lt;h2 id=&quot;ss-tun2socks&quot;&gt;ss-tun2socks&lt;/h2&gt;

&lt;p&gt;项目地址&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/zfl9/ss-tun2socks.git&quot;&gt;https://github.com/zfl9/ss-tun2socks.git&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;由于dns我用的是overture，脚本中的chinadns和pdnsd部分可以删掉，然后再整合UDPspeeder和udp2raw进去&lt;/p&gt;

&lt;p&gt;ss-tun2socks修改如下&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;#!/bin/bash&lt;/span&gt;

&lt;span class=&quot;nv&quot;&gt;main_cfg&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'/etc/tun2socks/ss-tun2socks.conf'&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-e&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;main_cfg&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;then
    &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-e&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[37m&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;main_cfg&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[0m  [&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[1;35mnot_exist&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[0m]&quot;&lt;/span&gt; 1&amp;gt;&amp;amp;2
    &lt;span class=&quot;nb&quot;&gt;exit &lt;/span&gt;1
&lt;span class=&quot;k&quot;&gt;else
    &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;source&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;main_cfg&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;fi

function &lt;/span&gt;chnip_update&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c&quot;&gt;#chnip_url=&quot;http://f.ip.cn/rt/chnroutes.txt&quot;&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;chnip_url&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;https://github.com/17mon/china_ip_list/raw/master/china_ip_list.txt&quot;&lt;/span&gt;
    curl &lt;span class=&quot;nt&quot;&gt;-4sSkL&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;chnip_url&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt; | egrep &lt;span class=&quot;nt&quot;&gt;-v&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'^\s*$|^\s*#'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;dns_chnroute&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;cat&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;dns_chnroute&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt; | xargs &lt;span class=&quot;nt&quot;&gt;-n1&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;echo &lt;/span&gt;add chnroute &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;ipset_save_file&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;

    ipset &lt;span class=&quot;nt&quot;&gt;-L&lt;/span&gt; chnroute &amp;amp;&amp;gt; /dev/null
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$?&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-ne&lt;/span&gt; 0 &lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;then
        &lt;/span&gt;ipset &lt;span class=&quot;nt&quot;&gt;-N&lt;/span&gt; chnroute &lt;span class=&quot;nb&quot;&gt;hash&lt;/span&gt;:net
    &lt;span class=&quot;k&quot;&gt;else
        &lt;/span&gt;ipset &lt;span class=&quot;nt&quot;&gt;-F&lt;/span&gt; chnroute &amp;amp;&amp;gt; /dev/null
    &lt;span class=&quot;k&quot;&gt;fi

    &lt;/span&gt;ipset &lt;span class=&quot;nt&quot;&gt;-R&lt;/span&gt; &amp;lt; &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;ipset_save_file&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;
    ipset &lt;span class=&quot;nt&quot;&gt;-S&lt;/span&gt; chnroute &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;ipset_save_file&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;function &lt;/span&gt;ipts_update&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    iptables &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; mangle &lt;span class=&quot;nt&quot;&gt;-F&lt;/span&gt; OUTPUT
    iptables &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; mangle &lt;span class=&quot;nt&quot;&gt;-F&lt;/span&gt; PREROUTING
    iptables &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; mangle &lt;span class=&quot;nt&quot;&gt;-F&lt;/span&gt; TUN2SOCKS &amp;amp;&amp;gt; /dev/null
    iptables &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; mangle &lt;span class=&quot;nt&quot;&gt;-X&lt;/span&gt; TUN2SOCKS &amp;amp;&amp;gt; /dev/null
    iptables &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; nat &lt;span class=&quot;nt&quot;&gt;-F&lt;/span&gt; PREROUTING
    iptables &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; nat &lt;span class=&quot;nt&quot;&gt;-F&lt;/span&gt; POSTROUTING

    iptables &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; mangle &lt;span class=&quot;nt&quot;&gt;-N&lt;/span&gt; TUN2SOCKS
    iptables &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; mangle &lt;span class=&quot;nt&quot;&gt;-A&lt;/span&gt; TUN2SOCKS &lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt; 0/8 &lt;span class=&quot;nt&quot;&gt;-j&lt;/span&gt; RETURN
    iptables &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; mangle &lt;span class=&quot;nt&quot;&gt;-A&lt;/span&gt; TUN2SOCKS &lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt; 10/8 &lt;span class=&quot;nt&quot;&gt;-j&lt;/span&gt; RETURN
    iptables &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; mangle &lt;span class=&quot;nt&quot;&gt;-A&lt;/span&gt; TUN2SOCKS &lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt; 127/8 &lt;span class=&quot;nt&quot;&gt;-j&lt;/span&gt; RETURN
    iptables &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; mangle &lt;span class=&quot;nt&quot;&gt;-A&lt;/span&gt; TUN2SOCKS &lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt; 169.254/16 &lt;span class=&quot;nt&quot;&gt;-j&lt;/span&gt; RETURN
    iptables &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; mangle &lt;span class=&quot;nt&quot;&gt;-A&lt;/span&gt; TUN2SOCKS &lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt; 172.16/12 &lt;span class=&quot;nt&quot;&gt;-j&lt;/span&gt; RETURN
    iptables &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; mangle &lt;span class=&quot;nt&quot;&gt;-A&lt;/span&gt; TUN2SOCKS &lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt; 192.168/16 &lt;span class=&quot;nt&quot;&gt;-j&lt;/span&gt; RETURN
    iptables &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; mangle &lt;span class=&quot;nt&quot;&gt;-A&lt;/span&gt; TUN2SOCKS &lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt; 224/4 &lt;span class=&quot;nt&quot;&gt;-j&lt;/span&gt; RETURN
    iptables &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; mangle &lt;span class=&quot;nt&quot;&gt;-A&lt;/span&gt; TUN2SOCKS &lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt; 240/4 &lt;span class=&quot;nt&quot;&gt;-j&lt;/span&gt; RETURN
    &lt;span class=&quot;c&quot;&gt;#iptables -t mangle -A TUN2SOCKS -d ${socks5_remote} -j RETURN&lt;/span&gt;
    &lt;span class=&quot;c&quot;&gt;# Reserved port 4000 for testing&lt;/span&gt;
    iptables &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; mangle &lt;span class=&quot;nt&quot;&gt;-A&lt;/span&gt; TUN2SOCKS &lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;socks5_remote&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; tcp &lt;span class=&quot;o&quot;&gt;!&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--dport&lt;/span&gt; 4000 &lt;span class=&quot;nt&quot;&gt;-j&lt;/span&gt; RETURN
    iptables &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; mangle &lt;span class=&quot;nt&quot;&gt;-A&lt;/span&gt; TUN2SOCKS &lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;socks5_remote&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; udp &lt;span class=&quot;o&quot;&gt;!&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--dport&lt;/span&gt; 4000 &lt;span class=&quot;nt&quot;&gt;-j&lt;/span&gt; RETURN
    iptables &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; mangle &lt;span class=&quot;nt&quot;&gt;-A&lt;/span&gt; TUN2SOCKS &lt;span class=&quot;nt&quot;&gt;-m&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;set&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--match-set&lt;/span&gt; chnroute dst &lt;span class=&quot;nt&quot;&gt;-j&lt;/span&gt; RETURN
    iptables &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; mangle &lt;span class=&quot;nt&quot;&gt;-A&lt;/span&gt; TUN2SOCKS &lt;span class=&quot;nt&quot;&gt;-j&lt;/span&gt; MARK &lt;span class=&quot;nt&quot;&gt;--set-mark&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;iptables_mark&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;

    iptables &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; mangle &lt;span class=&quot;nt&quot;&gt;-A&lt;/span&gt; OUTPUT &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; tcp &lt;span class=&quot;nt&quot;&gt;-j&lt;/span&gt; TUN2SOCKS
    iptables &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; mangle &lt;span class=&quot;nt&quot;&gt;-A&lt;/span&gt; OUTPUT &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; udp &lt;span class=&quot;nt&quot;&gt;-j&lt;/span&gt; TUN2SOCKS

    &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;intranet &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;iptables_intranet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[@]&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do
        &lt;/span&gt;iptables &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; mangle &lt;span class=&quot;nt&quot;&gt;-A&lt;/span&gt; PREROUTING &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; udp &lt;span class=&quot;nt&quot;&gt;-s&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$intranet&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$intranet&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--dport&lt;/span&gt; 53 &lt;span class=&quot;nt&quot;&gt;-j&lt;/span&gt; ACCEPT
        iptables &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; nat &lt;span class=&quot;nt&quot;&gt;-A&lt;/span&gt; PREROUTING &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; udp &lt;span class=&quot;nt&quot;&gt;-s&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$intranet&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$intranet&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--dport&lt;/span&gt; 53 &lt;span class=&quot;nt&quot;&gt;-j&lt;/span&gt; REDIRECT &lt;span class=&quot;nt&quot;&gt;--to-ports&lt;/span&gt; 53

        iptables &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; mangle &lt;span class=&quot;nt&quot;&gt;-A&lt;/span&gt; PREROUTING &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; tcp &lt;span class=&quot;nt&quot;&gt;-s&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$intranet&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-j&lt;/span&gt; TUN2SOCKS
        iptables &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; mangle &lt;span class=&quot;nt&quot;&gt;-A&lt;/span&gt; PREROUTING &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; udp &lt;span class=&quot;nt&quot;&gt;-s&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$intranet&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-j&lt;/span&gt; TUN2SOCKS

        iptables &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; nat &lt;span class=&quot;nt&quot;&gt;-A&lt;/span&gt; POSTROUTING &lt;span class=&quot;nt&quot;&gt;-s&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$intranet&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$intranet&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-j&lt;/span&gt; MASQUERADE
    &lt;span class=&quot;k&quot;&gt;done&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;function &lt;/span&gt;ipts_origin&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    iptables &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; mangle &lt;span class=&quot;nt&quot;&gt;-F&lt;/span&gt; OUTPUT
    iptables &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; mangle &lt;span class=&quot;nt&quot;&gt;-F&lt;/span&gt; PREROUTING
    iptables &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; mangle &lt;span class=&quot;nt&quot;&gt;-F&lt;/span&gt; TUN2SOCKS &amp;amp;&amp;gt; /dev/null
    iptables &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; mangle &lt;span class=&quot;nt&quot;&gt;-X&lt;/span&gt; TUN2SOCKS &amp;amp;&amp;gt; /dev/null
    iptables &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; nat &lt;span class=&quot;nt&quot;&gt;-F&lt;/span&gt; PREROUTING
    iptables &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; nat &lt;span class=&quot;nt&quot;&gt;-F&lt;/span&gt; POSTROUTING

&lt;span class=&quot;c&quot;&gt;#    for intranet in &quot;${iptables_intranet[@]}&quot;; do&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#        iptables -t nat -A PREROUTING -p udp -s $intranet -d $intranet --dport 53 -j DNAT --to-destination ${dns_original[0]}&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#        iptables -t nat -A POSTROUTING -s $intranet ! -d $intranet -j MASQUERADE&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#    done&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;function &lt;/span&gt;pbr_add&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    ip route add 0/0 via &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;tun2socks_gateway&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt; dev &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;tun2socks_tundev&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt; table &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;pbr_table&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;
    ip rule add fwmark &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;iptables_mark&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt; table &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;pbr_table&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;function &lt;/span&gt;pbr_del&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    ip rule show | &lt;span class=&quot;nb&quot;&gt;grep&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;fwmark &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;iptables_mark&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt; | &lt;span class=&quot;nb&quot;&gt;awk&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-F&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;':'&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'{print $1}'&lt;/span&gt; | xargs &lt;span class=&quot;nt&quot;&gt;-n1&lt;/span&gt; ip rule del pref &amp;amp;&amp;gt; /dev/null
    ip route flush table &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;pbr_table&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;function &lt;/span&gt;kernel_opts&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;si&quot;&gt;$(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cat&lt;/span&gt; /proc/sys/net/ipv4/ip_forward&lt;span class=&quot;si&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-ne&lt;/span&gt; 1 &lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;then
        &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;echo &lt;/span&gt;1 &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; /proc/sys/net/ipv4/ip_forward
    &lt;span class=&quot;k&quot;&gt;fi
    if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;si&quot;&gt;$(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cat&lt;/span&gt; /proc/sys/net/ipv4/conf/&lt;span class=&quot;nv&quot;&gt;$tun2socks_tundev&lt;/span&gt;/rp_filter&lt;span class=&quot;si&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-ne&lt;/span&gt; 2 &lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;then
        &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;echo &lt;/span&gt;2 &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; /proc/sys/net/ipv4/conf/&lt;span class=&quot;nv&quot;&gt;$tun2socks_tundev&lt;/span&gt;/rp_filter
    &lt;span class=&quot;k&quot;&gt;fi&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;function &lt;/span&gt;dns_update&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;# Generated by ss-tun2socks at &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;$(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;date&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'+%F %T'&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; /etc/resolv.conf
    &lt;span class=&quot;c&quot;&gt;#echo &quot;nameserver 127.0.0.1&quot; &amp;gt;&amp;gt; /etc/resolv.conf&lt;/span&gt;
    &lt;span class=&quot;c&quot;&gt;# 127.0.0.53 for ubuntu 18.04 lts&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;nameserver 127.0.0.53&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; /etc/resolv.conf
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;function &lt;/span&gt;dns_origin&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#    echo &quot;# Generated by ss-tun2socks at $(date '+%F %T')&quot; &amp;gt; /etc/resolv.conf&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#    for dns in &quot;${dns_original[@]}&quot;; do&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#        echo &quot;nameserver $dns&quot; &amp;gt;&amp;gt; /etc/resolv.conf&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#    done&lt;/span&gt;
    systemctl restart NetworkManager
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;function &lt;/span&gt;current_ip&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;ip_info&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;$(&lt;/span&gt;curl &lt;span class=&quot;nt&quot;&gt;-4skL&lt;/span&gt; ip.chinaz.com/getip.aspx | &lt;span class=&quot;nb&quot;&gt;sed&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-r&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;s/^&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\{&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;ip:'(.*)',address:'(.*)'&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\}&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$/&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\1&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\2&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;/g&quot;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;my_ip&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;$(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;ip_info&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt; | &lt;span class=&quot;nb&quot;&gt;awk&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-F&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'|'&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'{print $1}'&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;my_loc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;$(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;ip_info&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt; | &lt;span class=&quot;nb&quot;&gt;awk&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-F&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'|'&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'{print $2}'&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-e&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[37mIP:&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[0m &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;my_ip&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[37mLocation:&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[0m &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;my_loc&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;function &lt;/span&gt;start&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    dns_origin

    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-e&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;dns_chnroute&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;then
        &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-e&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[37m&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;dns_chnroute&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[0m  [&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[1;35mnot_exist&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[0m]&quot;&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;exit &lt;/span&gt;1
    &lt;span class=&quot;k&quot;&gt;fi

    &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;eval&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;tinymapper_runcmd&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;eval&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;udp2raw_runcmd&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;eval&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;speederv2_runcmd&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;

    &lt;span class=&quot;nb&quot;&gt;eval&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;socks5_runcmd&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;

    &lt;span class=&quot;nb&quot;&gt;nohup &lt;/span&gt;tun2socks &lt;span class=&quot;nt&quot;&gt;-tun-device&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$tun2socks_tundev&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-tun-address&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$tun2socks_address&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-tun-mask&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$tun2socks_netmask&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-tun-gw&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$tun2socks_gateway&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-local-socks-addr&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$socks5_listen&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-public-only&lt;/span&gt; &amp;lt; /dev/null &amp;amp;&amp;gt;&amp;gt; &lt;span class=&quot;nv&quot;&gt;$tun2socks_log&lt;/span&gt; &amp;amp;

    ipset &lt;span class=&quot;nt&quot;&gt;-X&lt;/span&gt; chnroute &amp;amp;&amp;gt; /dev/null
    ipset &lt;span class=&quot;nt&quot;&gt;-R&lt;/span&gt; &amp;lt; &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;ipset_save_file&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;

    ipts_update
    pbr_add
    kernel_opts

    dns_update
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;function &lt;/span&gt;stop&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;port&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;$(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$socks5_listen&lt;/span&gt; | &lt;span class=&quot;nb&quot;&gt;awk&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-F&lt;/span&gt;: &lt;span class=&quot;s1&quot;&gt;'{print $2}'&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;)&lt;/span&gt;
    ss &lt;span class=&quot;nt&quot;&gt;-lnptu&lt;/span&gt; | &lt;span class=&quot;nb&quot;&gt;grep&lt;/span&gt; :&lt;span class=&quot;nv&quot;&gt;$port&lt;/span&gt; | &lt;span class=&quot;nb&quot;&gt;grep&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-Eo&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'pid=[0-9]+'&lt;/span&gt; | &lt;span class=&quot;nb&quot;&gt;awk&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-F&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'{print $2}'&lt;/span&gt; | &lt;span class=&quot;nb&quot;&gt;sort&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-n&lt;/span&gt; | &lt;span class=&quot;nb&quot;&gt;uniq&lt;/span&gt; | xargs &lt;span class=&quot;nb&quot;&gt;kill&lt;/span&gt; &amp;amp;&amp;gt; /dev/null

    pkill &lt;span class=&quot;s1&quot;&gt;'^tun2socks$'&lt;/span&gt;

    pkill &lt;span class=&quot;s1&quot;&gt;'^tinymapper$'&lt;/span&gt;
    pkill &lt;span class=&quot;s1&quot;&gt;'^speederv2$'&lt;/span&gt;
    pkill &lt;span class=&quot;s1&quot;&gt;'^udp2raw$'&lt;/span&gt;

    ipts_origin
    ipset &lt;span class=&quot;nt&quot;&gt;-X&lt;/span&gt; chnroute &amp;amp;&amp;gt; /dev/null
    pbr_del

    dns_origin
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;function &lt;/span&gt;status&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;si&quot;&gt;$(&lt;/span&gt;pgrep &lt;span class=&quot;nt&quot;&gt;-c&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'^tun2socks$'&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-ge&lt;/span&gt; 1 &lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;then
        &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-e&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[37mtun2socks&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[0m&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[1;32mrunning&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[0m]&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;else
        &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-e&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[37mtun2socks&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[0m&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[1;35mstopped&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[0m]&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;fi

    &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;port&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;$(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$socks5_listen&lt;/span&gt; | &lt;span class=&quot;nb&quot;&gt;awk&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-F&lt;/span&gt;: &lt;span class=&quot;s1&quot;&gt;'{print $2}'&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;si&quot;&gt;$(&lt;/span&gt;ss &lt;span class=&quot;nt&quot;&gt;-lnpt&lt;/span&gt; | &lt;span class=&quot;nb&quot;&gt;grep&lt;/span&gt; :&lt;span class=&quot;nv&quot;&gt;$port&lt;/span&gt; | &lt;span class=&quot;nb&quot;&gt;wc&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-l&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-ne&lt;/span&gt; 0 &lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;then
        &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-e&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[37msocks5/tcp&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[0m&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[1;32mrunning&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[0m]&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;else
        &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-e&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[37msocks5/tcp&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[0m&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[1;35mstopped&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[0m]&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;fi

    if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;si&quot;&gt;$(&lt;/span&gt;ss &lt;span class=&quot;nt&quot;&gt;-lnpu&lt;/span&gt; | &lt;span class=&quot;nb&quot;&gt;grep&lt;/span&gt; :&lt;span class=&quot;nv&quot;&gt;$port&lt;/span&gt; | &lt;span class=&quot;nb&quot;&gt;wc&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-l&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-ne&lt;/span&gt; 0 &lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;then
        &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-e&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[37msocks5/udp&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[0m&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[1;32mrunning&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[0m]&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;else
        &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-e&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[37msocks5/udp&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[0m&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[1;35mstopped&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[0m]&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;fi

    if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;si&quot;&gt;$(&lt;/span&gt;pgrep &lt;span class=&quot;nt&quot;&gt;-c&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'^tinymapper$'&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-ge&lt;/span&gt; 1 &lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;then
        &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-e&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[37mtinymapper&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[0m&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[1;32mrunning&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[0m]&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;else
        &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-e&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[37mtinymapper&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[0m&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[1;35mstopped&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[0m]&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;fi

    if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;si&quot;&gt;$(&lt;/span&gt;pgrep &lt;span class=&quot;nt&quot;&gt;-c&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'^speederv2$'&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-ge&lt;/span&gt; 1 &lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;then
        &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-e&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[37mspeederv2&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[0m&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[1;32mrunning&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[0m]&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;else
        &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-e&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[37mspeederv2&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[0m&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[1;35mstopped&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[0m]&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;fi

    if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;si&quot;&gt;$(&lt;/span&gt;pgrep &lt;span class=&quot;nt&quot;&gt;-c&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'^udp2raw$'&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-ge&lt;/span&gt; 1 &lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;then
        &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-e&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[37mudp2raw &lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[0m&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[1;32mrunning&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[0m]&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;else
        &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-e&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[37mudp2raw &lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[0m&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[1;35mstopped&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[0m]&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;fi&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;function &lt;/span&gt;usage&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-e&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[37mUsage:&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[0m &lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[36m&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[0m &lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[1;37mCOMMAND&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[0m&quot;&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-e&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[1;37mCOMMAND&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[0m := {&quot;&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-e&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[33mstart&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[0m&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t\t&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;start ss-tun2socks&quot;&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-e&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[33mstop&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[0m&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t\t&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;stop ss-tun2socks&quot;&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-e&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[33mrestart&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[0m&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t\t&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;restart ss-tun2socks&quot;&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-e&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[33mstatus&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[0m&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t\t&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;status of ss-tun2socks&quot;&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-e&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[33mcurrent_ip&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[0m&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;get current ipaddr info&quot;&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-e&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[33mupdate_chnip&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[0m&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;update chnroute for ipset/chinadns&quot;&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-e&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;}&quot;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$1&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in
&lt;/span&gt;star&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c&quot;&gt;# start&lt;/span&gt;
    start
    status
    &lt;span class=&quot;p&quot;&gt;;;&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;stat&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c&quot;&gt;# status&lt;/span&gt;
    status
    &lt;span class=&quot;p&quot;&gt;;;&lt;/span&gt;
sto&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;   &lt;span class=&quot;c&quot;&gt;# stop&lt;/span&gt;
    stop
    status
    &lt;span class=&quot;p&quot;&gt;;;&lt;/span&gt;
r&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;     &lt;span class=&quot;c&quot;&gt;# restart&lt;/span&gt;
    stop
    status
    &lt;span class=&quot;nb&quot;&gt;echo
    &lt;/span&gt;start
    status
    &lt;span class=&quot;p&quot;&gt;;;&lt;/span&gt;

c&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;     &lt;span class=&quot;c&quot;&gt;# current_ip&lt;/span&gt;
    current_ip
    &lt;span class=&quot;p&quot;&gt;;;&lt;/span&gt;
u&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;     &lt;span class=&quot;c&quot;&gt;# update_chnip&lt;/span&gt;
    chnip_update
    &lt;span class=&quot;p&quot;&gt;;;&lt;/span&gt;

h&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;     &lt;span class=&quot;c&quot;&gt;# help&lt;/span&gt;
    usage
    &lt;span class=&quot;p&quot;&gt;;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    usage 1&amp;gt;&amp;amp;2
    &lt;span class=&quot;nb&quot;&gt;exit &lt;/span&gt;1
    &lt;span class=&quot;p&quot;&gt;;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;esac&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;ss-tun2socks.conf 配置文件如下&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;## socks5 配置
socks5_listen=&quot;127.0.0.1:1080&quot;  # ss本地监听地址
socks5_remote=&quot;**.**.**.**&quot;  # ss远程地址
socks5_remote_port=&quot;7788&quot;  # ss远程端口
udp2raw_remote_port=&quot;4020&quot;  # udp2raw远程端口
#socks5_runcmd=&quot;nohup ss-local -c /etc/ss-local.json &amp;lt; /dev/null &amp;amp;&amp;gt;&amp;gt; /var/log/ss-local.log &amp;amp;&quot; # 启动命令

socks5_runcmd=&quot;nohup ss-local -s 127.0.0.1 -p 6010 -b 127.0.0.1 -l 1080 -k '******' -m aes-256-gcm -u --fast-open &amp;lt; /dev/null &amp;amp;&amp;gt;&amp;gt; /var/log/ss-local.log &amp;amp;&quot; # 启动命令

tinymapper_runcmd=&quot;nohup tinymapper -l0.0.0.0:6010 -r$socks5_remote:$socks5_remote_port -t &amp;gt; /var/log/tinymapper.log 2&amp;gt;&amp;amp;1 &amp;amp;&quot;

udp2raw_runcmd=&quot;nohup udp2raw -c -l0.0.0.0:4021 -r$socks5_remote:$udp2raw_remote_port -a -k &quot;passwd&quot; --raw-mode faketcp --cipher-mode xor &amp;gt; /var/log/udp2raw_udp.log 2&amp;gt;&amp;amp;1 &amp;amp;&quot;

speederv2_runcmd=&quot;nohup speederv2 -c -l0.0.0.0:6010 -r127.0.0.1:4021 -k &quot;passwd&quot; -f20:10 --mode 0 -i 10 --timeout 8 --report 20 &amp;gt; /var/log/speederv2.log 2&amp;gt;&amp;amp;1 &amp;amp;&quot;

## tun2socks 配置
tun2socks_tundev=&quot;tun0&quot;                 # tun 网卡
tun2socks_address=&quot;10.0.0.1&quot;            # 接口地址
tun2socks_gateway=&quot;10.0.0.254&quot;          # 网关地址
tun2socks_netmask=&quot;255.255.255.0&quot;       # 网络掩码
tun2socks_log='/var/log/tun2socks.log'  # 日志文件

## ipset 配置
ipset_save_file='/etc/tun2socks/chnroute.ipset' # chnip列表文件
dns_chnroute='/etc/tun2socks/chnroute.txt' # 大陆地址段列表

## iptables 配置
iptables_mark='0x2333/0x2333'       # mangle mark
iptables_intranet=(192.168.0.0/16)  # 内网网段，多个空格隔开

## pbr 配置
pbr_table=100   # 路由表-表号

## dns 配置
dns_original=(**.**.**.** 119.29.29.29 180.76.76.76) # 正常网络的 DNS，最多 3 个，**.**.**.**替换成本地ISP提供的DNS
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;install.sh&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;#!/bin/sh&lt;/span&gt;

&lt;span class=&quot;nb&quot;&gt;sudo cp&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-af&lt;/span&gt; ss-tun2socks /usr/local/bin/
&lt;span class=&quot;nb&quot;&gt;sudo cp&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-af&lt;/span&gt; tun2socks.bin/tun2socks.x86_64 /usr/local/bin/tun2socks
&lt;span class=&quot;nb&quot;&gt;sudo chown &lt;/span&gt;root:root /usr/local/bin/tun2socks /usr/local/bin/ss-tun2socks
&lt;span class=&quot;nb&quot;&gt;sudo chmod&lt;/span&gt; +x /usr/local/bin/tun2socks /usr/local/bin/ss-tun2socks
&lt;span class=&quot;nb&quot;&gt;sudo mkdir&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-m&lt;/span&gt; 0755 &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; /etc/tun2socks
&lt;span class=&quot;nb&quot;&gt;sudo cp&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-af&lt;/span&gt; chnroute.txt /etc/tun2socks/
&lt;span class=&quot;nb&quot;&gt;sudo cp&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-af&lt;/span&gt; chnroute.ipset /etc/tun2socks/
&lt;span class=&quot;nb&quot;&gt;sudo cp&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-af&lt;/span&gt; ss-tun2socks.conf /etc/tun2socks/
&lt;span class=&quot;nb&quot;&gt;sudo chown&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-R&lt;/span&gt; root:root /etc/tun2socks
&lt;span class=&quot;nb&quot;&gt;sudo chmod &lt;/span&gt;0644 /etc/tun2socks/&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# speederv2 udp2raw tinymapper&lt;/span&gt;

&lt;span class=&quot;nb&quot;&gt;sudo cp&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-af&lt;/span&gt; speederv2_binaries/speederv2_amd64 /usr/local/bin/speederv2
&lt;span class=&quot;nb&quot;&gt;sudo cp&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-af&lt;/span&gt; udp2raw_binaries/udp2raw_amd64 /usr/local/bin/udp2raw
&lt;span class=&quot;nb&quot;&gt;sudo cp&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-af&lt;/span&gt; tinymapper_binaries/tinymapper_amd64 /usr/local/bin/tinymapper
&lt;span class=&quot;nb&quot;&gt;sudo chown &lt;/span&gt;root:root /usr/local/bin/speederv2 /usr/local/bin/udp2raw /usr/local/bin/tinymapper
&lt;span class=&quot;nb&quot;&gt;sudo chmod&lt;/span&gt; +x /usr/local/bin/speederv2 /usr/local/bin/udp2raw /usr/local/bin/tinymapper

&lt;span class=&quot;c&quot;&gt;# 自启&lt;/span&gt;

&lt;span class=&quot;nb&quot;&gt;sudo cp&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-af&lt;/span&gt; ss-tun2socks.service /lib/systemd/system/ss-tun2socks.service
&lt;span class=&quot;nb&quot;&gt;sudo chown &lt;/span&gt;root:root /lib/systemd/system/ss-tun2socks.service
&lt;span class=&quot;nb&quot;&gt;sudo chmod&lt;/span&gt; +x /lib/systemd/system/ss-tun2socks.service
&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;systemctl daemon-reload
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;overture&quot;&gt;overture&lt;/h2&gt;

&lt;p&gt;下载overture，项目地址&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/shawn1m/overture&quot;&gt;https://github.com/shawn1m/overture&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;在Releases页面下载最新版本overture-linux-amd64.zip&lt;/p&gt;

&lt;p&gt;解压到overture目录&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;unzip ./overture-linux-amd64.zip &lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt; ./overture
&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;overture
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;下载最新的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;china_ip_list.txt&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gfwlist.txt&lt;/code&gt;&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;wget &lt;span class=&quot;nt&quot;&gt;-c&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-N&lt;/span&gt; https://raw.githubusercontent.com/17mon/china_ip_list/master/china_ip_list.txt
wget &lt;span class=&quot;nt&quot;&gt;-c&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-N&lt;/span&gt; https://raw.githubusercontent.com/gfwlist/gfwlist/master/gfwlist.txt
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;修改配置文件config.json&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;vim ./config.json
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;主DNS列表中添加以下两个DNS，并设置cache和ttl&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;本地运营商自动分配的DNS&lt;/li&gt;
  &lt;li&gt;阿里DNS&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;{
  &quot;BindAddress&quot;: &quot;:53&quot;,
  &quot;PrimaryDNS&quot;: [
    {
      &quot;Name&quot;: &quot;Auto&quot;,
      &quot;Address&quot;: &quot;xx.xx.xx.xx:53&quot;,
      &quot;Protocol&quot;: &quot;udp&quot;,
      &quot;SOCKS5Address&quot;: &quot;&quot;,
      &quot;Timeout&quot;: 6,
      &quot;EDNSClientSubnet&quot;: {
        &quot;Policy&quot;: &quot;disable&quot;,
        &quot;ExternalIP&quot;: &quot;&quot;
      }
    },
    {
      &quot;Name&quot;: &quot;DNSPod&quot;,
      &quot;Address&quot;: &quot;119.29.29.29:53&quot;,
      &quot;Protocol&quot;: &quot;udp&quot;,
      &quot;SOCKS5Address&quot;: &quot;&quot;,
      &quot;Timeout&quot;: 6,
      &quot;EDNSClientSubnet&quot;: {
        &quot;Policy&quot;: &quot;disable&quot;,
        &quot;ExternalIP&quot;: &quot;&quot;
      }
    },
    {
      &quot;Name&quot;: &quot;AliDNS&quot;,
      &quot;Address&quot;: &quot;223.5.5.5:53&quot;,
      &quot;Protocol&quot;: &quot;udp&quot;,
      &quot;SOCKS5Address&quot;: &quot;&quot;,
      &quot;Timeout&quot;: 6,
      &quot;EDNSClientSubnet&quot;: {
        &quot;Policy&quot;: &quot;disable&quot;,
        &quot;ExternalIP&quot;: &quot;&quot;
      }
    }
  ],
  &quot;AlternativeDNS&quot;: [
    {
      &quot;Name&quot;: &quot;OpenDNS&quot;,
      &quot;Address&quot;: &quot;208.67.222.222:443&quot;,
      &quot;Protocol&quot;: &quot;tcp&quot;,
      &quot;SOCKS5Address&quot;: &quot;&quot;,
      &quot;Timeout&quot;: 6,
      &quot;EDNSClientSubnet&quot;: {
        &quot;Policy&quot;: &quot;disable&quot;,
        &quot;ExternalIP&quot;: &quot;&quot;
      }
    }
  ],
  &quot;OnlyPrimaryDNS&quot;: false,
  &quot;RedirectIPv6Record&quot;: false,
  &quot;IPNetworkFile&quot;: &quot;/etc/overture/china_ip_list.txt&quot;,
  &quot;DomainFile&quot;: &quot;/etc/overture/gfwlist.txt&quot;,
  &quot;DomainBase64Decode&quot;: true,
  &quot;HostsFile&quot;: &quot;/etc/overture/hosts_sample&quot;,
  &quot;MinimumTTL&quot;: 86400,
  &quot;CacheSize&quot; : 604800,
  &quot;RejectQtype&quot;: [255]
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;新建install.sh脚本，用于安装overture&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;#!/bin/sh&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# 复制程序到/usr/bin下&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;sudo cp&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-af&lt;/span&gt; ./overture-linux-amd64 /usr/local/bin/overture
&lt;span class=&quot;nb&quot;&gt;sudo chown &lt;/span&gt;root:root /usr/local/bin/overture
&lt;span class=&quot;nb&quot;&gt;sudo chmod&lt;/span&gt; +x /usr/local/bin/overture

&lt;span class=&quot;c&quot;&gt;# 复制配置文件到/etc/overture下&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;rsync &lt;span class=&quot;nt&quot;&gt;-r&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--delete&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--exclude&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;overture-linux-amd64&quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--exclude&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;domain_sample&quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--exclude&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;ip_network_sample&quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--exclude&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;install.sh&quot;&lt;/span&gt; ./ /etc/overture

&lt;span class=&quot;c&quot;&gt;# 配置overture服务&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;bash &lt;span class=&quot;nt&quot;&gt;-c&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'cat &amp;gt; /lib/systemd/system/overture.service'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;EOF&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;
[Unit]
Description=overture service

[Service]
ExecStart=/usr/local/bin/overture -c /etc/overture/config.json -l /etc/overture/overture.log
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target
&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;EOF

&lt;/span&gt;&lt;span class=&quot;c&quot;&gt;# 重载修改过的配置文件&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;systemctl daemon-reload
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;安装&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;./install.sh
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Ubuntu 18.04 LTS 默认使用systemd-resolved进行dns解析，停掉该服务，再启动overture&lt;/p&gt;

&lt;p&gt;停止systemd-resolved&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;systemctl stop systemd-resolved
&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;systemctl disable systemd-resolved
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;更改resolv.conf符号连接&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo mv&lt;/span&gt; /etc/resolv.conf /etc/resolv.conf.bak
&lt;span class=&quot;nb&quot;&gt;sudo ln&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-s&lt;/span&gt; /var/run/NetworkManager/resolv.conf /etc/resolv.conf
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;修改网络连接中的DNS服务器为127.0.0.53&lt;/p&gt;

&lt;p&gt;一些软件依赖于NetworkManage或/etc/resolv.conf中的域名服务器才能正常工作，所以我们要把所有DNS的请求接口修改为127.0.0.53:53&lt;/p&gt;

&lt;p&gt;重启NetworkManager&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;systemctl restart NetworkManager
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;启动overture前，检查53端口是否被占用&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;lsof &lt;span class=&quot;nt&quot;&gt;-i&lt;/span&gt;:53
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;如果被占用，就杀掉进程(一般被dnsmasq之类的DNS服务占用)&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo kill&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-9&lt;/span&gt; &amp;lt;PID&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;激活开机启动&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;systemctl &lt;span class=&quot;nb&quot;&gt;enable &lt;/span&gt;overture
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;启动overture服务&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;systemctl start overture
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;检查运行状态&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;systemctl status overture
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;至此，默认DNS服务由overture提供&lt;/p&gt;

&lt;h1 id=&quot;局部代理&quot;&gt;局部代理&lt;/h1&gt;

&lt;p&gt;有了全局智能代理，没必要使用局部代理，这里只是记录一下，需shadowsocks配合&lt;/p&gt;

&lt;p&gt;apt-get使用代理&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;vim apt_proxy_conf
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;添加&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Acquire::http::proxy &quot;http://127.0.0.1:1080/&quot;;
Acquire::ftp::proxy &quot;ftp://127.0.0.1:1080/&quot;;
Acquire::https::proxy &quot;https://127.0.0.1:1080/&quot;;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;更新时使用配置代理&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt-get update &lt;span class=&quot;nt&quot;&gt;-c&lt;/span&gt; apt_proxy_conf
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;proxychain5代理&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt-get &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;proxychains
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;配置&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;vim /etc/proxychains.conf
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;添加&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;socks5  127.0.0.1 1080
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;安装chrome&quot;&gt;安装Chrome&lt;/h1&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt-get &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;gdebi
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;gdebi google-chrome-stable_current_amd64.deb
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;安装redshift&quot;&gt;安装Redshift&lt;/h1&gt;

&lt;p&gt;Ubuntu 18.04 LTS 已经集成了夜灯模式，位于 设置-设备-显示-夜灯&lt;/p&gt;

&lt;p&gt;Redshift作为备选，有特殊需求可以安装&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt-get &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;redshift-gtk
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Redshift项目地址&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/jonls/redshift&quot;&gt;https://github.com/jonls/redshift&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;下载配置文件redshift.conf，放到如下目录&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;~/.config/redshift.conf
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;修改几个主要参数&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;; Set the day and night screen temperatures
temp-day=4300
temp-night=3300

; Disable the smooth fade between temperatures when Redshift starts and stops.
; 0 will cause an immediate change between screen temperatures.
; 1 will gradually apply the new screen temperature over a couple of seconds.
fade=0

[manual]
lat=22.5
lon=114.0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;运行，在托盘图标勾选Autostart&lt;/p&gt;

&lt;h1 id=&quot;安装dropbox&quot;&gt;安装Dropbox&lt;/h1&gt;

&lt;p&gt;下载最新的Stable版本包，解压到~/.dropbox-dist目录下&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;wget https://clientupdates.dropboxstatic.com/dbx-releng/client/dropbox-lnx.x86_64-48.4.58.tar.gz

&lt;span class=&quot;nb&quot;&gt;tar&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-zxvf&lt;/span&gt; dropbox-lnx.x86_64-48.4.58.tar.gz
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;新建dropbox.servive&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[Unit]
Description=Dropbox as a user service
After=local-fs.target network.target

[Service]
Type=simple
ExecStart=%h/.dropbox-dist/dropboxd
Restart=on-failure
RestartSec=1
# Note: don't set these in user mode- they're already set, and
# systemd won't have permission to set them- killing your service before
# it starts
# User=%U
# Group=%U

[Install]
WantedBy=default.target
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;把dropbox.service移动到用户服务目录下&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;mkdir&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; ~/.config/systemd/user
&lt;span class=&quot;nb&quot;&gt;mv&lt;/span&gt; ~/dropbox.service ~/.config/systemd/user/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;重载服务&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;systemctl &lt;span class=&quot;nt&quot;&gt;--user&lt;/span&gt; daemon-reload
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;开机自动启动服务&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;systemctl &lt;span class=&quot;nt&quot;&gt;--user&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;enable &lt;/span&gt;dropbox
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;启动服务&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;systemctl &lt;span class=&quot;nt&quot;&gt;--user&lt;/span&gt; start dropbox
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;安装jekyll&quot;&gt;安装Jekyll&lt;/h1&gt;

&lt;p&gt;安装ruby开发包&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt-get &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;ruby ruby-dev
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;安装jekyll和bundler&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;gem &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;jekyll bundler
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;进入Gemfile所在目录，更新依赖&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;bundle &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;或者安装gem包到指定目录，以隔离对系统gem的影响&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;bundle &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--path&lt;/span&gt; vendor/bundle
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;过程可能会报错，我的具体情况是 nokogiri 依赖 zlib&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt-get &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;zlib1g-dev
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;启动本地服务&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;bundle &lt;span class=&quot;nb&quot;&gt;exec &lt;/span&gt;jekyll serve &lt;span class=&quot;nt&quot;&gt;--baseurl&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;安装virtualbox&quot;&gt;安装Virtualbox&lt;/h1&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;virtualbox
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;安装扩展，可选&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;virtualbox-ext-pack
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;安装docker&quot;&gt;安装Docker&lt;/h1&gt;

&lt;p&gt;确保APT能使用https方式工作，并且CA证书已安装&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt-get &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;apt-transport-https ca-certificates curl software-properties-common
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;添加官方GPG密钥&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;curl &lt;span class=&quot;nt&quot;&gt;-fsSL&lt;/span&gt; https://download.docker.com/linux/ubuntu/gpg | &lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt-key add -
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;添加REPO并更新&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;deb [arch=amd64] https://download.docker.com/linux/ubuntu &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;$(&lt;/span&gt;lsb_release &lt;span class=&quot;nt&quot;&gt;-cs&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; stable&quot;&lt;/span&gt; | &lt;span class=&quot;nb&quot;&gt;sudo tee&lt;/span&gt; /etc/apt/sources.list.d/docker.list
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;或者直接追加进sources.list中&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;add-apt-repository &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;s2&quot;&gt;&quot;deb [arch=amd64] https://download.docker.com/linux/ubuntu &lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;$(&lt;/span&gt;lsb_release &lt;span class=&quot;nt&quot;&gt;-cs&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; &lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;
stable&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;更新&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt-get update
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;安装docker-ce&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt-get &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;docker-ce
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Docker Hub加速&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;vim /etc/docker/daemon.json
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;添加镜像加速地址&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;{
    &quot;registry-mirrors&quot;: [&quot;https://40fcvoar.mirror.aliyuncs.com&quot;]
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;重启docker服务&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;service docker restart
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;安装docker-machine&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;curl &lt;span class=&quot;nt&quot;&gt;-L&lt;/span&gt; https://github.com/docker/machine/releases/download/v0.13.0/docker-machine-&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;uname&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-s&lt;/span&gt;&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;-&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;uname&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-m&lt;/span&gt;&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;/tmp/docker-machine &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;chmod&lt;/span&gt; +x /tmp/docker-machine &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;sudo cp&lt;/span&gt; /tmp/docker-machine /usr/local/bin/docker-machine
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;系统备份--还原&quot;&gt;系统备份 &amp;amp; 还原&lt;/h1&gt;

&lt;p&gt;安装pigz多线程压缩&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt-get &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;pigz
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;备份&lt;/p&gt;

&lt;p&gt;备份文件所存放的位置，根据实际情况更改&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;BACKUP_FILE_PATH&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/media/howie/F63E9E535BFBE2AE/Ubuntu
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;备份软件包&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo cp&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-r&lt;/span&gt; /var/cache/apt/archives/&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$BACKUP_FILE_PATH&lt;/span&gt;/archives/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;执行备份&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo tar&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--use-compress-program&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;pigz &lt;span class=&quot;nt&quot;&gt;-cvpf&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$BACKUP_FILE_PATH&lt;/span&gt;/ubuntu.tar.gz &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;--exclude&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/proc &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;--exclude&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/tmp &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;--exclude&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/mnt &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;--exclude&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/dev &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;--exclude&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/sys &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;--exclude&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/run &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;--exclude&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/media &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;--exclude&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/var/log &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;--exclude&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/var/cache/apt/archives &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;--exclude&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/usr/src/linux-headers&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;--exclude&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/home/&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;/.gvfs &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;--exclude&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/home/&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;/.cache &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;--exclude&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/home/&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;/.local/share/Trash &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;--exclude&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/cdrom &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;--exclude&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/lost+found &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;--exclude&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/swapfile &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;--exclude&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/root/.cache &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;--exclude&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/var/tmp &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
/ &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$BACKUP_FILE_PATH&lt;/span&gt;/ubuntu.log 2&amp;gt;&amp;amp;1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;还原&lt;/p&gt;

&lt;p&gt;通过live cd进入试用ubuntu&lt;/p&gt;

&lt;p&gt;备份文件所存放的位置，根据实际情况更改&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;BACKUP_FILE_PATH&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/media/ubuntu/F63E9E535BFBE2AE/Ubuntu
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;查看分区情况&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;lsblk
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;挂载备份文件所在分区，一般会自动挂载在media下，自行查看&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;mount /dev/nvme0n1p5 /mnt
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;删除旧系统所有数据&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo rm&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-rf&lt;/span&gt; /mnt/&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;挂载UEFI分区&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo mkdir&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; /mnt/boot/efi
&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;mount /dev/nvme0n1p1 /mnt/boot/efi
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;恢复&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo tar&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-zxvpf&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$BACKUP_FILE_PATH&lt;/span&gt;/ubuntu.tar.gz &lt;span class=&quot;nt&quot;&gt;-C&lt;/span&gt; /mnt &lt;span class=&quot;nt&quot;&gt;--numeric-owner&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;新建目录&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;cd&lt;/span&gt; /mnt
&lt;span class=&quot;nb&quot;&gt;sudo mkdir&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; proc sys dev run mnt tmp var/log var/cache/apt/archives
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;复制软件包&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo cp&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-r&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$BACKUP_FILE_PATH&lt;/span&gt;/archives/&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt; /var/cache/apt/archives/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;修复grub2引导&lt;/p&gt;

&lt;p&gt;通过live cd进入试用ubuntu&lt;/p&gt;

&lt;p&gt;挂载安装的Linux分区&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;mount /dev/nvme0n1p5 /mnt
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;挂载EFI分区&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;mount /dev/nvme0n1p1 /mnt/boot/efi
&lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;i &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; /dev /dev/pts /proc /sys /run&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;mount &lt;span class=&quot;nt&quot;&gt;-B&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$i&lt;/span&gt; /mnt&lt;span class=&quot;nv&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;done&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;chroot到安装的Linux&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo chroot&lt;/span&gt; /mnt
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;重装grub-efi&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;apt-get &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--reinstall&lt;/span&gt; grub-efi
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;安装grub2到UEFI分区所在的硬盘上&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;grub-install /dev/nvme0n1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;更新grub配置&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;update-grub
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;</content><author><name>gemhowie</name></author><category term="Linux" /><summary type="html">安装完 Ubuntu 18.04 LTS 后进行一些必要的配置，方便使用。</summary></entry><entry><title type="html">LeetCode Easy Top Interview Questions</title><link href="https://gemhowie.github.io/blog/2017/12/01/leetcode-easy-top-interview-questions/" rel="alternate" type="text/html" title="LeetCode Easy Top Interview Questions" /><published>2017-12-01T00:00:00+08:00</published><updated>2017-12-01T00:00:00+08:00</updated><id>https://gemhowie.github.io/blog/2017/12/01/leetcode-easy-top-interview-questions</id><content type="html" xml:base="https://gemhowie.github.io/blog/2017/12/01/leetcode-easy-top-interview-questions/">&lt;p&gt;LeetCode Easy 难度，面试常考题。&lt;/p&gt;

&lt;p&gt;网页链接&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://leetcode.com/problemset/all/?difficulty=Easy&amp;amp;listId=wpwgkgt&quot;&gt;https://leetcode.com/problemset/all/?difficulty=Easy&amp;amp;listId=wpwgkgt&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;所有solution均在Python3下通过测试。&lt;/p&gt;

&lt;h2 id=&quot;1-two-sum&quot;&gt;1. Two Sum&lt;/h2&gt;

&lt;p&gt;Given an array of integers, return indices of the two numbers such that they add up to a specific target.&lt;/p&gt;

&lt;p&gt;You may assume that each input would have exactly one solution, and you may not use the same element twice.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;给定一个整形数组，返回其中两个元素的下标，使得这两个元素相加等于指定的目标值。&lt;/p&gt;

&lt;h3 id=&quot;思路&quot;&gt;思路&lt;/h3&gt;

&lt;p&gt;通过迭代&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enumerate&lt;/code&gt;对象，用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;target - v&lt;/code&gt;得到另一个要找的数，然后在剩下的元素中匹配，如果存在，则返回两个数的下标。&lt;/p&gt;

&lt;p&gt;第二个数的下标因为是在切片后的数组中匹配，得到结果后要加上&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;i + 1&lt;/code&gt;。&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Solution&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;twoSum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
    :type nums: List[int]
    :type target: int
    :rtype: List[int]
    &quot;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;second&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;target&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;second&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:]:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;second&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;7-reverse-integer&quot;&gt;7. Reverse Integer&lt;/h2&gt;

&lt;p&gt;Given a 32-bit signed integer, reverse digits of an integer.&lt;/p&gt;

&lt;p&gt;Example 1:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Input: 123
Output:  321
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Example 2:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Input: -123
Output: -321
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Example 3:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Input: 120
Output: 21
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;note&quot;&gt;Note:&lt;/h3&gt;

&lt;p&gt;Assume we are dealing with an environment which could only hold integers within the 32-bit signed integer range. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.&lt;/p&gt;

&lt;h3 id=&quot;思路-1&quot;&gt;思路&lt;/h3&gt;

&lt;p&gt;反转32位有符号整数，溢出时返回&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt;，通常的想法是转成&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;str&lt;/code&gt;，然后用倒数切片，最后判断正负号和是否溢出。&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Solution&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;reverse&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
        :type x: int
        :rtype: int
        &quot;&quot;&quot;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;abs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))[::&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;31&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
        python3 移除了cmp函数，在 python2 中可以这样写
        return cmp(x, 0) * y * (y &amp;lt; 2**31)
        &quot;&quot;&quot;&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# -2147483648 ~ 2147483647
# 7463847412
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;改进&quot;&gt;改进&lt;/h3&gt;

&lt;p&gt;溢出的判断不精确，还有如果不借助额外的空间，应该怎么解？&lt;/p&gt;

&lt;h2 id=&quot;13-roman-to-integer&quot;&gt;13. Roman to Integer&lt;/h2&gt;

&lt;p&gt;Given a roman numeral, convert it to an integer.&lt;/p&gt;

&lt;p&gt;Input is guaranteed to be within the range from 1 to 3999.&lt;/p&gt;

&lt;p&gt;给定一个罗马数字，将它转换成一个整数。输入值范围确保只从&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt;到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3999&lt;/code&gt;。&lt;/p&gt;

&lt;h3 id=&quot;思路-2&quot;&gt;思路&lt;/h3&gt;

&lt;p&gt;先定义一个字典，键为罗马数字的字母，值为字母代表的值。&lt;/p&gt;

&lt;p&gt;罗马数字有个特点，只有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;50&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;100&lt;/code&gt;…这样的值。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3&lt;/code&gt;就用基础的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt;来叠加，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;II&lt;/code&gt;代表&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;III&lt;/code&gt;代表&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;4&lt;/code&gt;则有点特殊，用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5-1&lt;/code&gt;表示，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt;放在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5&lt;/code&gt;的左边，意为减去，所以&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IV&lt;/code&gt;代表&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;同样的，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;6&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;7&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;8&lt;/code&gt;用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt;来叠加，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VI&lt;/code&gt;代表&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;6&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VII&lt;/code&gt;代表&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;7&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VIII&lt;/code&gt;代表&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;8&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;9&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;4&lt;/code&gt;类似，用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10-1&lt;/code&gt;表示，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt;放在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10&lt;/code&gt;的左边，所以&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IX&lt;/code&gt;代表&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;9&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;总结一下，就是，只有进位前一个数，比如&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;4&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;9&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;49&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;99&lt;/code&gt;需要用到倒序减一的方式表示，其他的都是用最靠近的数加上基础数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(1，10，100...)&lt;/code&gt;的个数来表示。&lt;/p&gt;

&lt;p&gt;所以可以循环罗马数字，相加减的次数(循环的次数)应该是罗马数字的个数减1，但是我们会用到一个变量来存储累加结果，所以次数会加1。&lt;/p&gt;

&lt;p&gt;我们只把罗马数字的前&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;len(s)-1&lt;/code&gt;个字母放在循环里处理，最后一个字母通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;return&lt;/code&gt;时的表达式给加上去，这样就可以处理只有一个罗马数字的情况。&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Solution&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;romanToInt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
        :type s: str
        :rtype: int
        &quot;&quot;&quot;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;roman&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'M'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'D'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;500&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'C'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'L'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'X'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'V'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'I'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;roman&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;roman&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;roman&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;roman&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;roman&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;14-longest-common-prefix&quot;&gt;14. Longest Common Prefix&lt;/h2&gt;

&lt;p&gt;Write a function to find the longest common prefix string amongst an array of strings.&lt;/p&gt;

&lt;p&gt;写一个函数从一个字符串数组中查找最长的共同前缀字符串。&lt;/p&gt;

&lt;h3 id=&quot;思路-3&quot;&gt;思路&lt;/h3&gt;

&lt;p&gt;Python的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;os&lt;/code&gt;模块已经实现这个功能，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;os.path.commonprefix(strs)&lt;/code&gt;，但通常情况下面试官不希望看到这个答案。:smile:&lt;/p&gt;

&lt;p&gt;第二个方法是使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;zip&lt;/code&gt;函数。&lt;/p&gt;

&lt;p&gt;在Python2中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;zip&lt;/code&gt;函数接收多个可迭代对象，分别把多个迭代对象的第1个，第2个…第n个元素(n取决于可迭代对象中长度最短的那一个)组成&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tuple&lt;/code&gt;，然后返回一个包含这些&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tuple&lt;/code&gt;的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;list&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;在Python3中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;zip&lt;/code&gt;函数的返回值变成了&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;zip&lt;/code&gt;对象，想得到Python2的结果，可以通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;list()&lt;/code&gt;来转换&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;zip&lt;/code&gt;对象。&lt;/p&gt;

&lt;p&gt;具体思路是，用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;*strs&lt;/code&gt;将数组中的字符串作为多个可迭代对象参数传给&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;zip&lt;/code&gt;，然后用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enumerate&lt;/code&gt;获取&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;index&lt;/code&gt;和对应的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tuple&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;如果有共同前缀，那么&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tuple&lt;/code&gt;中的元素应该都是相同的，用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;set&lt;/code&gt;转换可以去重，然后&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;len&lt;/code&gt;判断元素个数。&lt;/p&gt;

&lt;p&gt;如果从某个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;index&lt;/code&gt;对应的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tuple&lt;/code&gt;开始，去重后元素个数大于1，说明字符已经不相同，只需要截取当前&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;index&lt;/code&gt;前的部分，即为最长共同前缀。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;for..else..&lt;/code&gt;的写法有个陷阱，当&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;for&lt;/code&gt;循环中不存在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;return&lt;/code&gt;或&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;break&lt;/code&gt;时，循环结束后会继续执行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;else&lt;/code&gt;部分的内容。&lt;/p&gt;

&lt;p&gt;下面这个例子，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;for&lt;/code&gt;中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;if&lt;/code&gt;如果都不存在满足的条件，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;return&lt;/code&gt;就不会执行，循环结束后执行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;else&lt;/code&gt;内容。这意味着每个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tuple&lt;/code&gt;中都只包含同一个元素，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;else&lt;/code&gt;中返回&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;min(strs)&lt;/code&gt;或者&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;min(strs, key=len)&lt;/code&gt;即为最长共同前缀。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;return strs[0][:i]&lt;/code&gt;需要解释一下，当输入是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;['z', 'abc']&lt;/code&gt;时，经过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;zip&lt;/code&gt;函数处理后的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tuple&lt;/code&gt;是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;('z', 'a')&lt;/code&gt;，元素个数大于1。&lt;/p&gt;

&lt;p&gt;也就是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;index&lt;/code&gt;为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt;时就不存在相同的字符了，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strs[0][:i]&lt;/code&gt;即&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strs[0][:0]&lt;/code&gt;，结果是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;''&lt;/code&gt;，所以不必担心&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;else&lt;/code&gt;中使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;min(strs)&lt;/code&gt;会根据&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ASCII&lt;/code&gt;返回&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;abc&lt;/code&gt;这种结果，因为这种情况下根本不会走&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;else&lt;/code&gt;分支。只是像这样依赖于条件控制，有时会使人产生困惑。&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Solution&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;longestCommonPrefix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;strs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
        :type strs: List[str]
        :rtype: str
        &quot;&quot;&quot;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;strs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&quot;&lt;/span&gt;
        
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;letter_group&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;zip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;strs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)):&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;letter_group&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;strs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;strs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;第三个方法，用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;functools&lt;/code&gt;的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reduce&lt;/code&gt;函数。&lt;/p&gt;

&lt;h2 id=&quot;20-valid-parentheses&quot;&gt;20. Valid Parentheses&lt;/h2&gt;

&lt;p&gt;Given a string containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[’ and ‘]’, determine if the input string is valid.&lt;/p&gt;

&lt;p&gt;The brackets must close in the correct order, “()” and “()[]{}” are all valid but “(]” and “([)]” are not.&lt;/p&gt;

&lt;p&gt;给定一个只包含&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;'('，')'，'['，']'，'{'，'}'&lt;/code&gt;的字符串，判断输入是否有效。&lt;/p&gt;

&lt;p&gt;括号必须以正确的顺序结束。&lt;/p&gt;

&lt;h3 id=&quot;思路-4&quot;&gt;思路&lt;/h3&gt;

&lt;p&gt;循环输入的字符串，碰到开始括号则入栈，碰到关闭括号则从写好的字典中获取对应的开始括号，和栈顶出栈的括号比较，如果不等，则输入无效，相等则循环直到栈为空。&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Solution&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;isValid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
        :type s: str
        :rtype: bool
        &quot;&quot;&quot;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;l&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;
        
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;
        
        &lt;span class=&quot;n&quot;&gt;check&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;')'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'('&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;']'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'['&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'}'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'{'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;stack&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'('&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'['&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'{'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;stack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stack&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;check&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
                    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;21-merge-two-sorted-lists&quot;&gt;21. Merge Two Sorted Lists&lt;/h2&gt;

&lt;p&gt;Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Input: 1-&amp;gt;2-&amp;gt;4, 1-&amp;gt;3-&amp;gt;4
Output: 1-&amp;gt;1-&amp;gt;2-&amp;gt;3-&amp;gt;4-&amp;gt;4
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;合并两个有序链表并作为一个新链表返回。新的链表应该通过拼接前两个链表的节点来生成。&lt;/p&gt;

&lt;h3 id=&quot;思路-5&quot;&gt;思路&lt;/h3&gt;

&lt;p&gt;递归版本，函数的主要功能是比较两个链表，确保&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;l1&lt;/code&gt;不为空节点，并且&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;l1&lt;/code&gt;第一个节点中值最小，如果不满足，交换&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;l1&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;l2&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;l1&lt;/code&gt;作为新链表的第一个节点，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;l1.next&lt;/code&gt;则重新指向通过递归调用函数本身，得到第二小的元素节点，直到某个递归深度时其中一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;list&lt;/code&gt;为空，最后返回&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;l1&lt;/code&gt;。&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Solution&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;mergeTwoLists&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        &quot;&quot;&quot;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l1&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l2&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;l1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l1&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l1&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;l1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;next&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mergeTwoLists&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;l1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;循环版本&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None
&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Solution&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;mergeTwoLists&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        &quot;&quot;&quot;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;dummy&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cur&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ListNode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l1&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;l1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l1&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;cur&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;next&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l1&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;l1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;next&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;cur&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cur&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;next&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;cur&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;next&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l1&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l2&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dummy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;next&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;26-remove-duplicates-from-sorted-array&quot;&gt;26. Remove Duplicates from Sorted Array&lt;/h2&gt;

&lt;p&gt;Given a sorted array, remove the duplicates in-place such that each element appear only once and return the new length.&lt;/p&gt;

&lt;p&gt;Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Given nums = [1,1,2],

Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.
It doesn't matter what you leave beyond the new length.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;给定一个有序的数组，通过原位操作移除重复元素，让每个元素只出现一次并返回新的长度。&lt;/p&gt;

&lt;p&gt;不能分配额外的空间给另一个数组，你必须在O(1)的额外内存中通过原位操作修改输入的数组来完成它。&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;给定 nums = [1,1,2]，

你的函数应该返回长度为2，且nums最开始的两个元素分别为1和2。无论在新长度之后留下什么都没关系。
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;思路-6&quot;&gt;思路&lt;/h3&gt;

&lt;p&gt;因为需要&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;in-place&lt;/code&gt;，即原位操作，而且只能用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;O(1)&lt;/code&gt;的额外内存，所以不能用数组这种会随着规模变大而变大的临时变量。&lt;/p&gt;

&lt;p&gt;用一个变量&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;i&lt;/code&gt;来表示新长度，用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;for n in nums&lt;/code&gt;遍历这个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nums&lt;/code&gt;，比较后一个元素与前一个元素，因为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nums&lt;/code&gt;是有序的，如果后一个元素大于前一个，则通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nums[i] = n&lt;/code&gt;覆盖原来的值，并且&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;i&lt;/code&gt;自增，如果比较结果相同，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;i&lt;/code&gt;不变，继续比较下一个元素。直到遍历完&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nums&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;i&lt;/code&gt;的值即为不重复元素的个数。&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Solution&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;removeDuplicates&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
        :type nums: List[int]
        :rtype: int
        &quot;&quot;&quot;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]:&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;nums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;</content><author><name>gemhowie</name></author><category term="LeetCode" /><summary type="html">LeetCode Easy 难度，面试常考题。</summary></entry></feed>