記錄vim和vi相關

vim和vi相關

有關vim和vi的相關資訊
https://vim.rtorr.com/
https://github.com/kaochenlong/cch/blob/master/README.md

快捷鍵

  • 拷貝當前行yy,貼上p, 5yy拷貝5行
  • 刪除當前行dd, 5dd刪除5行
  • 查找關鍵詞命令行下/關鍵詞 n是查找下一個位置
  • 設置文件的行號和取消文件的行號命令行下 :set nu set nonu
  • 編輯文件到達首行gg , 到達最未行G在正常模式
  • 輸入時撒消動作u,在正常模式
  • 將光標移到指定的行數 13G

(1) 花式插入模式
i -> 在光標前插入
I -> 在句子前插入
a -> 在光標後插入
A -> 在句子後插入
o -> 在當前行下插入一行
O -> 在當前行上插入一行
(2) 花式移動光標
0 -> 數字零,到行頭
$ -> 到本行行尾
(3) visual
ctrl+v d刪除 shift+i 加入 esc兩次
shift+v

一般vim設定檔

1
2
3
4
5
6
" sudo vim .vimrc

set cursorline
set tabstop=4
set shiftwidth=4

Vim的設定檔

命令

1
2
# 重新載入目前vim的設定檔
:source
1
2
3
4
5
6
7
8
9
10
"設定行號
set number
"設定剪貼版共用
set clipboard=unnamed
"設定讓搜尋結果更明顯
set hlsearch
"設定游標所在處有底線效果
set cursouline
"不要產生Swap檔
set noswapfile

tab還是space?

  • set softtabstop=2
    使用2個space取代tab
  • set shiftwidth=2
    在>的時候使用2個空格做為縮排
  • set expandtab
    使用space取代tab
    命令
    :retab 動新設定文件裡的tab

關於搜尋

  • set ignorecase
    設定搜尋時不分大小寫
  • set incsearch
    只要部份關鍵字就有搜尋效果

關於編輯器的樣子

  • set showtabline=2
    只要有任何分頁都錢秀出來
  • set splitbelow
    新增水平視窗的時候會出現在下方
  • set splitright
    新增垂直視窗的時候會出現在右方

工程師的編輯器

  • syntax on
    打開程式碼語法高亮(幫程式碼上色)
  • filetype on
    偵測檔案型態
  • filetype indent on
    根據程式種類進行縮排
  • filetype plugin on
    根㯫程式語言種類載入外掛

以下是參考的設定

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

"設定內容:

set number
set clipboard=unnamed
set cursorline
set noswapfile

" search
set hlsearch
set ignorecase
set incsearch

" tab and space
set softtabstop=2
set shiftwidth=2
set expandtab

" tab
set showtabline=2
set splitbelow
set splitright

" color
syntax on
colorscheme darkblue

" filetype
filetype on
filetype indent on
filetype plugin on

Vim的設定之KeyMapping_part1

命令 Normal Mode Visual Mode Insert mode
map V V
nmap V
vmap V
imap V

指令

1
2
3
4
5
6
" 查看特殊鍵
:help key-notation
" 取消Key Mapping unmpa/nunmap/vunmap/iunmap
:unmap
" 把全部的對映都清掉
:mabclear
  • 想把方向鍵的功態關掉,強迫自已使用HJKL來移動游標
    1
    2
    :h key-notation
    :map <Up> <Nop>

Vim的設定之KeyMapping_part2

  • map時無窮迴圈

    • noremap no recursice mapping
    • 什麼時候該用noremap?
  • 如果把原來的功能map用掉了怎麼辦

    • Leader 預設值是 \
    • 用Leader有什麼好處
      1
      2
      " 把Leader設定成逗號
      :let mapleader = ","

Vim的設定之KeyMapping_part3

  • 設定成舊的vi
    1
    set nocompatible
  • 可檢視目前非預設值的設定
    1
    :set 
  • 主題
    1
    2
    3
    4
    colorscheme 列表
    " colorscheme ctrl+d
    " 如果沒有這個主題
    try ... catch ...endtry
  • 顯示目前游標所在地的狀態
    1
    set ruler
  • 剩下3行就開始捲動
    1
    set scrolloff=3
  • 斷行
    1
    2
    3
    4
    5
    6
    " 設定如果文字太多的話自動折行
    set wrap
    set nowrap
    " 在比較適當的地方換行
    set linebreak

  • 設定在左下角顯示現在模式
    1
    set showmode
  • 設定顯示敲打了什麼指令
    1
    set showcmd
  • 比較聰渡的搜尋
    1
    set smarcase
  • 設定顯示看不到的東西
    1
    2
    set list
    set nolist

vim之自動命令

有三個範例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
" 視窗切換時候顯示/隱藏游標底線
autocmd WinEnter * setlocal cursorline
autocmd WinLeave * setlocal nocursorline

" 存檔時自動把行末多餘的空白刪除
autocmd BufWritePre * :%s/\s\+$//e

" 按下 F5 執行程式
if executable("ruby")
autocmd BufRead,BufNewFile *.rb noremap <F5> :% w !ruby -w<Enter>
else
autocmd BufRead,BufNewFile *.rb noremap <F5> :echo "you need to install Ruby first!"
endif

if executable("node")
autocmd BufRead,BufNewFile *.js noremap <F5> :% w !node<Enter>
else
autocmd BufRead,BufNewFile *.rb noremap <F5> :echo "you need to install Node.js first!"
endif

命令

1
2
3
4
"有關autocmd的事件
:h autocmd-events
"更多關於autocmd請查閱
:h autocmd

整理你的 Vim 設定

把設定全部寫在.vimrc有問題嗎?
vim 有一套推薦的規劃

1
:h vimfiles

vim的外掛

  • 外掛管理工具
    1
    2
    3
    4
    - VimPlug https://github.com/junegunn/vim-plug
    - Pathogen https://github.com/tpope/vim-pathogen
    - Vundle https://github.com/VundleVim/Vundle.vim
    - CSS Color https://github.com/ap/vim-css-color
  • 外掛夠用就好,用不到的就不要裝
  • 建議用Git管理設定

Vim 外掛介紹 part 1

相關連結:

Vim 外掛介紹 part 2

安裝外掛:
- ctrlp https://github.com/ctrlpvim/ctrlp.vim
- emmethttps://github.com/mattn/emmet-vim
- surroundhttps://github.com/tpope/vim-surround
- vim-repeathttps://github.com/tpope/vim-repeat

額外設定:
ctrlp

1
2
3
4
5
6
7
8
9
10
11
let g:ctrlp_custom_ignore = {
\ 'dir': '\v[\/]\.(git|hg|svn)$',
\ 'file': '\v\.(exe|so|dll)$',
\ 'link': 'some_bad_symbolic_links',
\ }
```
emmet
``` vim=
let g:user_emmet_install_global = 0
autocmd FileType html,css,scss EmmetInstall
autocmd Filetype html,css,scss imap <silent> <expr> <tab> emmet#expandAbbrIntelligent("\<tab>")

Vim 外掛介紹 part 3

額外設定:

  • NERDTree 設定:

切換目錄時可以切換起始目錄(Root Directory)

1
let NERDTreeChDirMode = 2

ctrlp 設定:

  1. 排除不想搜尋的 tmp 目錄。
  2. 把內建的 grep 換成速度更快的 silver searcher
1
2
3
4
5
6
7
8
9
10
11
12
let g:ctrlp_by_filename = 1
let g:ctrlp_custom_ignore = {
\ 'dir': '\v[\/]\.(git|hg|svn)$|tmp$',
\ 'file': '\v\.(exe|so|dll)$',
\ 'link': 'some_bad_symbolic_links',
\ }

if executable('ag')
set grepprg=ag\ --nogroup\ --nocolor
let g:ctrlp_user_command = 'ag %s -l --nocolor -g ""'
let g:ctrlp_use_caching = 0
endif

需另外安裝 Silver Searcher:
https://github.com/ggreer/the_silver_searcher

參考連結:

Vim 外掛介紹 part 4

額外設定:
https://gist.github.com/kaochenlong/a999aee921b477a79fbbb7d251ecff9d

配色

vim的其它

搜尋和取代

1
:%s/text/cch/g

vim高見龍參考

安裝現成設定檔

參考資料

http://wiki.csie.ncku.edu.tw/vim/vimrc?printable
https://stackoverflow.com/questions/58676693/prevent-vscode-from-unfolding-code-when-cursor-moves-past-folded-section

https://www.yuexun.me/blog/the-vim-guide-for-vs-code-users/
https://chengjingchao.com/2020/06/13/VS-Code-%E4%B8%8E-Vim/

將看到脫窗的註解文字 深藍色 改變顏色

1
2
3
4
shell# vim /home/eric/.vimrc

hi Comment ctermfg=cyan

這是記錄Vim的顏色的設定

原來的設定有在註解看不清楚,所以來設定一下