我的Linux生活日記 19.來看看文字編輯工具-vim

2024-02-28 我的Linux生活日記 linux

我的Linux生活日記 19.來看看文字編輯工具-vim

使用電腦一定會用到文字編輯工具,在熟悉的微軟世界是記事本,那在Linux 是什麼呢?Text Editor,是我第一次使用的工具,同時也是Gnome桌面預設的文字編輯工具。不過還有很多種類下就帶大家看看有哪些。

視窗

Gnome Text Editor

這是從 ubuntu 開始接觸 Linux 會碰到的文字編輯工具,功能跟微軟的記事本一樣簡單容易上手,而且也用語法亮度顯示,到現在開發還是會當作輔助工具。

notepadqq

作為一個工程師常常需要打開不同語言的腳本,原本在windows我自己是習慣使用Notepad++,但是踏進linux之後發現Notepad++ 只能透過wine執行,後來我在網路上找到了 notepadqq ,下面是在Debian安裝的方式:

  1. 新增下面兩個資套件來源
sudo touch /etc/apt/sources.list.d/notepadqq.list
sudo echo "deb http://ppa.launchpad.net/notepadqq-team/notepadqq/ubuntu trusty main" >> /etc/apt/sources.list.d/notepadqq.list
sudo echo "deb-src http://ppa.launchpad.net/notepadqq-team/notepadqq/ubuntu trusty main" >> /etc/apt/sources.list.d/notepadqq.list
  1. 新增公鑰
sudo apt-key adv --recv-key --keyserver keyserver.ubuntu.com 63DE9CD4
  1. 更新並安裝
sudo apt update && sudo apt -y install notepadqq

終端機

vi

vi 是大部分 Linux 發行版本在終端機裡一定有的文字編輯工具,級是在最小安裝版本也會有。在使用上有三個模式:

  • 一般模式

進入vi 之後就是一般模式,在這個模式下可以瀏覽文件內容。除了瀏覽文件內容以外還可以進行剪下整行複製整行等工作

剪下整行: 鍵盤直接輸入 dd

複製整行: 鍵盤直接輸入 yy

將剛剛剪下/複製的內容插入到下一行: pp

將剛剛剪下/複製的內容插入到上一行: PP

  • 編輯模式

在一般模式下輸入i就可以進入編輯模式,在這個模式中就可以打字啦

  • 指令列模式

在一般模式下輸入:就可以進入指令列模式,比較常用的功能像儲存關閉文件並離開

儲存: 鍵盤直接輸入 w

強制儲存: 鍵盤直接輸入 w!

關閉文件並離開: 鍵盤直接輸入 q

強制關閉文件並離開: 鍵盤直接輸入 q!

當然也可以做儲存後關閉: 鍵盤直接輸入 wq

vim

vim 是 vi 的加強版,也是我自己比較長使用的文字編輯工具,主要是他有支援豐富的語法亮度,還有很多外掛可以用,所以明天我會介紹我的配置啦!

接下來分享我自己在工作機上常用的 vim設定,我沒有設定得很複雜,能讓文件簡單清楚顯示就是我的目標。畢竟我沒有打算將 vim 變成 IDE的夢想。

命令列模式

:%s/search_from/replace_to/g

vimrc 個人化設定

註解

註解 " 雙引號

" 這是註解
用空格代替Tab鍵

有時候撰寫 yaml 文件就會手癢按下 tab ,我覺的這個功能對我來說非常重要。

set et " expandtab,用空格代替Tab鍵
語法識別

會自動識別語法,我想是依據副檔名吧。

syntax enable
顯示游標所在的列
set cursorline
顯示游標所在的行
set cursorcolumn
自動判斷編碼

打開未知的中文文件編碼非常重要,不然會出現一片亂碼。

" 自動判斷編碼
set fileencodings=utf-8,utf-16,big5,gb2312,gbk,gb18030,euc-jp,euc-kr,latin1
if has("multi_byte")

else
    echoerr "If +multi_byte is not included, you should compile Vim with big features."
endif

set encoding=utf-8
顯示行號

雖然 vim 右下角忌諱顯示行號,但是在每行的左邊要跟 PM 或是跟同事溝通時會比較方便。

set nu
自動換行
set wrap " 自動換行
插件
套件管理工具
  • 下載管理工具
git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
  • 執行安裝
vim +PluginInstall
  • 腳本
" Vundle start
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()

" Your plugin start


" Your plugin end 

call vundle#end()            " required
filetype plugin indent on    " required
" Vundle end
Markdown

常常用 markdown 作筆記下面這幾個套件很實用。

  • 安裝
Plugin 'gabrielelana/vim-markdown'
Plugin 'tyru/open-browser.vim'
Plugin 'kannokanno/previm'
NERDTree
  • 安裝
Plugin 'preservim/nerdtree'
  • NERDTree 快捷鍵
ctrl + w + h    光標 focus 左側樹形目錄
ctrl + w + l    光標 focus 右側文件顯示窗口
ctrl + w + w    光標自動在左右側窗口切換
ctrl + w + r    移動當前窗口的佈局位置
我自己常用的設定

最後附上我自己的設定

" 個人化設定

set et                  " expandtab,用空格代替Tab鍵
set background=dark
set cursorline          " 顯示游標所在的列
set cursorcolumn        " 顯示游標所在的行
set wrap                " 自動換行
set nu                  " 自動換行
set t_Co=256            " 指定 vim 的顏色


" 自動判斷編碼
set fileencodings=utf-8,utf-16,big5,gb2312,gbk,gb18030,euc-jp,euc-kr,latin1
if has("multi_byte")

else
    echoerr "If +multi_byte is not included, you should compile Vim with big features."
endif

set encoding=utf-8


" Vundle start
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()

" let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'
Plugin 'vim-airline/vim-airline'

" Your plugin start

" NERDTree

autocmd vimenter * NERDTree                  " 自動開啟Nerdtree
let g:NERDTreeWinSize = 25                    " 設定 NERDTree 視窗大小
map <F5> :NERDTreeToggle<CR>                  " 開啟/關閉nerdtree快捷鍵
autocmd vimenter * if !argc()|NERDTree|endif  " 開啟vim時如果沒有檔案自動開啟NERDTree
autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif "當NERDTree為剩下的唯一視窗時自動關閉
let g:NERDTreeDirArrowExpandable = '▸'        " NERDTree 圖標
let g:NERDTreeDirArrowCollapsible = '▾'       " NERDTree 圖標
let g:NERDTreeHidden=0                        "不顯示隱藏檔案
"       let NERDTreeMinimalUI = 1
"       let NERDTreeDirArrows = 1
let g:NERDTreeNodeDelimiter = "\u00a0"


" git

Plugin 'airblade/vim-gitgutter'
set updatetime=200                           " 每兩秒檢查一次有差異
highlight GitGutterAdd    ctermfg=blue
highlight GitGutterChange ctermfg=green
highlight GitGutterDelete ctermfg=red

" nerdtree

Plugin 'preservim/nerdtree'


" Your plugin end 

call vundle#end()            " required
filetype plugin indent on    " required
" Vundle end

常用快捷建

  • Crtl + w w 循环所有窗口

  • Crtl + w h 让你离开一个窗口

  • Crtl + w j 把你拉下窗户

  • Crtl + w k 带你上一个窗口

  • Crtl + w l 带你到一个窗口

套件管理工具

  • 下載管理工具
git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
  • 執行安裝
vim +PluginInstall

NERDTree 安裝

cd ~
mkdir nerdtree
cd nerdtree
wget http://www.vim.org/scripts/download_script.php?src_id=17123 -O nerdtree.zip 
unzip nerdtree.zip
mkdir -p ~/.vim/{plugin,doc}
cp ~/nerdtree/plugin/NERD_tree.vim ~/.vim/plugin/
cp ~/nerdtree/doc/NERD_tree.txt ~/.vim/doc/
vim ~/.vimrc
加入:map <F5> :NERDTreeToggle<CR>
  • NERDTree 快捷鍵
ctrl + w + h    光標 focus 左側樹形目錄
ctrl + w + l    光標 focus 右側文件顯示窗口
ctrl + w + w    光標自動在左右側窗口切換
ctrl + w + r    移動當前窗口的布局位置
o       在已有窗口中打開文件、目錄或書簽,並跳到該窗口
go      在已有窗口 中打開文件、目錄或書簽,但不跳到該窗口
t       在新 Tab 中打開選中文件/書簽,並跳到新 Tab
T       在新 Tab 中打開選中文件/書簽,但不跳到新 Tab
i       split 一個新窗口打開選中文件,並跳到該窗口
gi      split 一個新窗口打開選中文件,但不跳到該窗口
s       vsplit 一個新窗口打開選中文件,並跳到該窗口
gs      vsplit 一個新 窗口打開選中文件,但不跳到該窗口
!       執行當前文件
O       遞歸打開選中 結點下的所有目錄
x       合攏選中結點的父目錄
X       遞歸 合攏選中結點下的所有目錄
e       Edit the current dif
雙擊    相當於 NERDTree-o
中鍵    對文件相當於 NERDTree-i,對目錄相當於 NERDTree-e
D       刪除當前書簽
P       跳到根結點
p       跳到父結點
K       跳到當前目錄下同級的第一個結點
J       跳到當前目錄下同級的最後一個結點
k       跳到當前目錄下同級的前一個結點
j       跳到當前目錄下同級的後一個結點
C       將選中目錄或選中文件的父目錄設為根結點
u       將當前根結點的父目錄設為根目錄,並變成合攏原根結點
U       將當前根結點的父目錄設為根目錄,但保持展開原根結點
r       遞歸刷新選中目錄
R       遞歸刷新根結點
m       顯示文件系統菜單
cd      將 CWD 設為選中目錄
I       切換是否顯示隱藏文件
f       切換是否使用文件過濾器
F       切換是否顯示文件
B       切換是否顯示書簽
q       關閉 NerdTree 窗口
?       切換是否顯示 Quick Help
:tabnew [++opt選項] [+cmd] 文件      建立對指定文件新的tab
:tabc   關閉當前的 tab
:tabo   關閉所有其他的 tab
:tabs   查看所有打開的 tab
:tabp   前一個 tab
:tabn   後一個 tab
功能	                    快捷键	      解释
新建文件	                ma	          在要创建文件的目录中按命令 ma然后键入你要创建的文件名称即可。
删除文件	                md	          在要删除的文件上按命令md然后输入y回车即可。
移动文件/修改文件名	      mm	          在要修改的文件上按命令mm然后输入对应的目录和名称回车即可。
设置当前目录为项目根目录	C	            在要设置为根目录的目录上按命令C【大写】即可。(刚学vim的时候总是不小心按到u命令把不必要的目录设置成了我的项目根目录,当时一直没找到比较好的解决方案很尴尬只能重启vim解决现在只要按C命令改回来即可)
查看当前文件所在目录	  :NERDTreeFind	  执行命令 :NERDTreeFind 或则在.vimrc中添加 map <leader>v :NERDTreeFind<CR> 全局使用 <leader>v命令(我的是,v)直接显示当前文件所在目录。

bash-support

安裝
cd ~/Doenloads
wget -O bash-support.zip http://www.vim.org/scripts/download_script.php?src_id=9890
mkdir ~/.vim # if the directory does not exist already
cd ~/.vim
unzip ~/Doenloads/bash-support.zip

新增 ~/.vimrc 內容

filetype plugin on
bash-support 常用快捷鍵
\sc case in … esac
\sl elif then
\sf for in do done
\sfo for ((…)) do done
\si if then fi
\sie if then else fi
\ss select in do done
\st until do done
\sw while do done
\sfu function
\se echo ­e “\n”
\sp printf “\n”
常用功能
  • 搜尋並取代字串
:%s/search_from/replace_to/g

自己的配置

set nocompatible              " be iMproved, required
filetype off                  " required
set et                        " expandtab,用空格代替Tab鍵
au InsertLeave *.eml write    " 每次退出插入模式時自動儲存

" 語法識別
syntax enable
" 終端機背景色 : dark / light
set background=dark

" 執行程式
nmap <F8> :call CompileRun()<CR>
func! CompileRun()
    exec "w"
    
    if &filetype == 'python'
        exec " !time python3 %"
    elseif &filetype == 'java'
        exec " !javac %"
    exec " !time java %<"
        elseif &filetype == 'sh'
    exec "w !bash"
    endif
endfunc

filetype plugin on

" vim-bash-support
let g:BASH_AuthorName   = 'PolloChang'
let g:BASH_Email        = '[email protected]'
let g:BASH_Company      = 'Open Source Corporation'

 
" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" alternatively, pass a path where Vundle should install plugins
"call vundle#begin('~/some/path/here')


" let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'
Plugin 'vim-airline/vim-airline'
Plugin 'airblade/vim-gitgutter'
Plugin 'preservim/nerdtree'
"autocmd vimenter * NERDTree  "自動開啟Nerdtree
let g:NERDTreeWinSize = 25 "設定 NERDTree 視窗大小
"開啟/關閉nerdtree快捷鍵
map <F5> :NERDTreeToggle<CR>
"開啟vim時如果沒有檔案自動開啟NERDTree
autocmd vimenter * if !argc()|NERDTree|endif
"當NERDTree為剩下的唯一視窗時自動關閉
autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif
let g:NERDTreeDirArrowExpandable = '▸'
let g:NERDTreeDirArrowCollapsible = '▾'
let g:NERDTreeHidden=0     "不顯示隱藏檔案
"       let NERDTreeMinimalUI = 1
"       let NERDTreeDirArrows = 1
let g:NERDTreeNodeDelimiter = "\u00a0"

Plugin 'xuyuanp/nerdtree-git-plugin'
let g:NERDTreeGitStatusShowIgnored = 1
let g:NERDTreeGitStatusIndicatorMapCustom = {
    \ "Modified"  : "✹",
    \ "Staged"    : "✚",
    \ "Untracked" : "✭",
    \ "Renamed"   : "➜",
    \ "Unmerged"  : "═",
    \ "Deleted"   : "✖",
    \ "Dirty"     : "✗",
    \ "Clean"     : "✔︎",
    \ 'Ignored'   : '☒',
    \ "Unknown"   : "?"
    \ }


Plugin 'ryanoasis/vim-devicons'
set encoding=UTF-8
let g:webdevicons_enable_nerdtree = 1
let g:webdevicons_conceal_nerdtree_brackets = 1
let g:webdevicons_enable_airline_tabline = 1
let g:webdevicons_enable_airline_statusline = 1

Plugin 'tiagofumo/vim-nerdtree-syntax-highlight'
let g:NERDTreeFileExtensionHighlightFullName = 1
let g:NERDTreeExactMatchHighlightFullName = 1
let g:NERDTreePatternMatchHighlightFullName = 1
let g:NERDTreeHighlightFolders = 1
let g:NERDTreeHighlightFoldersFullName = 1
let s:brown = "905532"
let s:aqua =  "3AFFDB"
let s:blue = "689FB6"
let s:darkBlue = "44788E"
let s:purple = "834F79"
let s:lightPurple = "834F79"
let s:red = "AE403F"
let s:beige = "F5C06F"
let s:yellow = "F09F17"
let s:orange = "D4843E"
let s:darkOrange = "F16529"
let s:pink = "CB6F6F"
let s:salmon = "EE6E73"
let s:green = "8FAA54"
let s:Turquoise = "40E0D0"
let s:lightGreen = "31B53E"
let s:white = "FFFFFF"
let s:rspec_red = "FE405F"
let s:git_orange = "F54D27"
let s:gray = "808A87"
let g:NERDTreeExtensionHighlightColor = {} " this line is needed to avoid error
let g:NERDTreeExtensionHighlightColor['o'] = s:gray " sets the color of o files to blue
let g:NERDTreeExtensionHighlightColor['h'] = s:blue " sets the color of h files to blue
let g:NERDTreeExtensionHighlightColor['c'] = s:green " sets the color of c files to blue
let g:NERDTreeExtensionHighlightColor['cpp'] = s:green " sets the color of cpp files to blue
let g:NERDTreeExtensionHighlightColor['c++'] = s:green

Plugin 'itspriddle/vim-shellcheck'


" Markdown 插件
Plugin 'gabrielelana/vim-markdown'
Plugin 'tyru/open-browser.vim'
Plugin 'kannokanno/previm'

" All of your Plugins must be added before the following line
call vundle#end()            " required
filetype plugin indent on    " required
" To ignore plugin indent changes, instead use:
"filetype plugin on
"
" Brief help
" :PluginList       - lists configured plugins
" :PluginInstall    - installs plugins; append `!` to update or just :PluginUpdate
" :PluginSearch foo - searches for foo; append `!` to refresh local cache
" :PluginClean      - confirms removal of unused plugins; append `!` to auto-approve removal
"
" see :h vundle for more details or wiki for FAQ
" Put your non-Plugin stuff after this line
"

" git set
set updatetime=200
highlight GitGutterAdd    ctermfg=blue
highlight GitGutterChange ctermfg=green
highlight GitGutterDelete ctermfg=red

" 個人化設定
"
set t_Co=256
set cursorline " 顯示游標所在的列
" hi CursorLine cterm=none ctermbg=DarkMagenta ctermfg=White
set cursorcolumn " 顯示游標所在的行

" 自動判斷編碼
set fileencodings=utf-8,utf-16,big5,gb2312,gbk,gb18030,euc-jp,euc-kr,latin1
if has("multi_byte")

else
    echoerr "If +multi_byte is not included, you should compile Vim with big features."
endif

set encoding=utf-8

set wrap " 自動換行
set nu

set history=1000

參考資料

Vim樹狀目錄插件NERDTree安裝和使用

【Vim 編輯器 配置指南】訂製個人的編輯神器

Make Vim as Your Bash-IDE Using bash-support Plugin