Vim安裝使用以及一些避坑指南

itgoyo發表於2018-12-14

Vim

詳細Vim快捷鍵大全

Vim的配置以spf-13為例子

專案地址: github.com/spf13/spf13…

Vim的安裝

  • Linux, *nix, Mac OSX Installation

The easiest way to install spf13-vim is to use our automatic installer by simply copying and pasting the following line into a terminal. This will install spf13-vim and backup your existing vim configuration. If you are upgrading from a prior version (before 3.0) this is also the recommended installation.

Requires Git 1.7+ and Vim 7.3+

curl https://j.mp/spf13-vim3 -L > spf13-vim.sh && sh spf13-vim.sh
複製程式碼

If you have a bash-compatible shell you can run the script directly:

sh <(curl https://j.mp/spf13-vim3 -L)
複製程式碼

此過程會比較長,包括外掛的下載還有安裝,這個過程將近1個多小時

  • Installing on Windows

On Windows and *nix Git and Curl are required. Also, if you haven't done so already, you'll need to install Vim. The quickest option to install all three dependencies (Git, Curl, Vim and spf13-vim) is via Chocolatey NuGet. After installing Chocolatey, execute the following commands on the command prompt:

Install with cmd.exe(run as admin mode)

@powershell -NoProfile -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"
複製程式碼
C:\> choco install spf13-vim
複製程式碼

一直Y,整個過程大概2h左右

Vim外掛的介紹

  • Vundle(The best plugin manager)

Vundle is an excellent system built on the same principles as Pathogen, but with an integrated plugin management system that is Git and Github aware.

spf13-vim uses the Vundle plugin management system to have a well organized vim directory (Similar to mac's app folders). Vundle also ensures that the latest versions of your plugins are installed and makes it easy to keep them up to date.

  • NERDTree(file navigation)

NERDTree is a file explorer plugin that provides "project drawer" functionality to your vim editing. You can learn more about it with :help NERDTree or checkout my post on NERDTree.

Use Ctrl+e to toggle NERDTree

  • ctrlp(fast file finder)

Ctrlp replaces the Command-T plugin with a 100% viml plugin. It provides an intuitive and fast mechanism to load files from the file system (with regex and fuzzy find), from open buffers, and from recently used files.

Use Ctrl+p to toggle Ctrlp

  • neocomplcache(autocomplete++)

NeoComplCache is an amazing autocomplete plugin with additional support for snippets. It can complete simulatiously from the dictionary, buffer, omnicomplete and snippets. This is the one true plugin that brings Vim autocomplete on par with the best editors.

Use Ctrl+n to toggle neocomplcache

  • Tagbar(tag generation and navigation)

spf13-vim includes the Tagbar plugin. This plugin requires exuberant-ctags and will automatically generate tags for your open files. It also provides a panel to navigate easily via tags

Use ,tt to toggle neocomplcache

關於在Mac端ctags無效的解決方法

brew.sh/

github.com/universal-c…

github.com/universal-c…


在Windows端,如果想切換到別的碟符進行操作的話,使用

:NERDTree D:\\

進行目錄的跳轉


Preview: To preview markdown format you need to install bluecloth gem

在使用previewMarkdown的時候出現了 Preview: To preview markdown format you need to install bluecloth gem

解決方法:

  • sudo gem install redcarpet

  • sudo gem install bluecloth

Linux出現的問題

can't find header files for ruby at /usr/lib/ruby/include/ruby.h

解決方法:sudo apt-get install ruby-dev

提示找不到tag檔案

用法:
    1.生成標籤檔案(cmd到專案的目錄中執行)
        在當前目錄下(執行$提示符後面的命令):
        $ctags -R .
      -R表示recursive,遞迴,為當前目錄及其子目錄中的c檔案生成標籤檔案。最後一個.表示在當前目錄。
        執行完當前目錄會多一個檔案tags,就是c標籤的索引檔案。
    2.跳轉
        1)用vim開啟一個已經建過標籤的c檔案    
        2)ctrl+] 找到游標所在位置的標籤定義的地方
        3)ctrl+t 回到跳轉之前的標籤處
    3.視窗顯示方法
         命令Tagbar toggle開啟相應的方法視窗顯示
    注意:此時執行vim,必須在"tags"檔案所在的目錄下執行。否則,執行它會找不到"tags"檔案,而需要在vim中用":set tags="命令設定"tags"檔案的路徑。對於一個稍微大點的專案,你可能在任何一個目錄下開啟vim,然而在每個目錄下都生成一個tags檔案並不 是個好主意,那麼如何解決呢?方法是在.vimrc中增加一行:
        set tags=tags;/
    這是告訴vim在當前目錄找不到tags檔案時請到上層目錄查詢。
複製程式碼

視窗切換快捷鍵

Ctrl+w+w

關閉分屏

關閉當前視窗。
Ctrl+W c
關閉當前視窗,如果只剩最後一個了,則退出Vim。
Ctrl+W q
複製程式碼

開啟多個視窗了之後,怎麼快速切換

:buffers 列表 :bn下一個 :bp 上一個 :b17

取消查詢遺留的邊框

set: nohlseach
複製程式碼

VIM編輯多行

vim進了多行編輯模式:<ESC>之後按CTRL+V進入visual block模式(列編輯)。游標移到某行行首,進入visual block模式,上下鍵選擇行,按I(i的大寫字母),輸入##,然後按<ESC>鍵,這樣就在多行行首新增##了。也可以在多行的固定位置新增固定字元。

如果要刪除這些##,進入visual block模式,選中這些##,按d即可。
複製程式碼

全域性搜尋Ack

按鍵 功能
? 顯示鍵盤對映
o 開啟檔案
O 開啟檔案關閉QuickFix視窗
go 預覽檔案,但焦點留在ack搜尋結果上
t 在新標籤頁開啟檔案
T 在新標籤頁開啟但不切換到那個標籤頁
h 分屏開啟
H 分屏開啟,但焦點停留在ack搜尋結果上
v 豎直分屏開啟
gv 豎直分屏開啟,但焦點停留在ack搜尋結果上
q 關閉QuickFix視窗

windows

在Windows下安裝它們可以使用Chocolatey,安裝方法如下:首先以管理員許可權開啟cmd視窗,然後執行下列命令,首先以管理員許可權開啟cmd視窗(管理員那個終端才有效),然後執行下列命令

@"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"
複製程式碼

安裝完了之後

choco install ack
複製程式碼

Mac

brew install ack
複製程式碼

如果出現亂碼

'perl' 不是內部或外部命令,也不是可執行的程式
複製程式碼

下載安裝ActivePerl,配置環境變數即可解決此問題。 下載地址

SpaceVim

該配色使用的是gruvbox

如果想更換成此主題

  • 在vimrc檔案中增加
 "gruvbox主題"
Plugin 'morhetz/gruvbox'
set bg=dark
colorscheme gruvbox

複製程式碼

然後全域性搜尋let g:airline_theme,改成如下顏色防止顏色不協調

  let g:airline_theme = 'dark'
複製程式碼

終端執行 PluginInstall完成之後,重啟就可以了

怎麼開啟最進開啟過的檔案

啟動Vim的時候Ctrl+o即可

標記

使用標記可以快速移動。到達標記後,可以用Ctrl+o返回原來的位置。 Ctrl+o和Ctrl+i 很像瀏覽器上的 後退 和 前進 。

m{a-z}: 標記游標所在位置,區域性標記,只用於當前檔案。

m{A-Z}: 標記游標所在位置,全域性標記。標記之後,退出Vim, 重新啟動,標記仍然有效。

`{a-z}: 移動到標記位置。

‘{a-z}: 移動到標記行的行首。

`{0-9}:回到上[2-10]次關閉vim時最後離開的位置。

`: 移動到上次編輯的位置。"也可以,不過`精確到列,而"精確到行 。如果想跳轉到更老的位置,可以按C-o,跳轉到更新的位置用C-i。

`”: 移動到上次離開的地方。

`.: 移動到最後改動的地方。

:marks 顯示所有標記。

:delmarks a b – 刪除標記a和b。

:delmarks a-c – 刪除標記a、b和c。

:delmarks a c-f – 刪除標記a、c、d、e、f。

:delmarks! – 刪除當前緩衝區的所有標記。

:help mark-motions 檢視更多關於mark的知識。

Linux版本中貼上系統剪下板內容失敗

- 首先,檢視vim版本是否支援clipboard

vim --version | grep "clipboard"
複製程式碼

clipboard前面有一個小小的減號,說明不支援。

如果不支援的話,需要安裝圖形化介面的vim,或者重新編譯vim
sudo apt-get install vim-gnome
複製程式碼

安裝完成後再次執行:

vim --version | grep "clipboard"
複製程式碼

發現已經支援clipboard

那麼我們的目的是要複製到系統剪下板則需要選中內容後輸入命令:"+y

貼上到特定的暫存器也是同理。例如"+p將系統剪下板的內容拷貝到vim中(非編輯模式下)。

快速跳轉外掛Easymotion

vim-easymotion

配置如下:

let g:EasyMotion_smartcase = 1
"let g:EasyMotion_startofline = 0 " keep cursor colum when JK motion
map <Leader><leader>h <Plug>(easymotion-linebackward)
map <Leader><Leader>j <Plug>(easymotion-j)
map <Leader><Leader>k <Plug>(easymotion-k)
map <Leader><leader>l <Plug>(easymotion-lineforward)
" 重複上一次操作, 類似repeat外掛, 很強大
map <Leader><leader>. <Plug>(easymotion-repeat)
複製程式碼
  • 用法1: 跳轉到當前游標前後的位置(w/b)
  • 用法2: 搜尋跳轉(s)
  • 用法3: 行級跳轉(jk)
  • 用法4: 行內跳轉(hl)
  • 用法5: 重複上一次動作(.)

建議

1.還可以f和t, 不過建議簡單化, 一個w/b走天下.

2.如果你不經常使用s, 可以將s改鍵, nmap s (easymotion-s), 這樣你只需要輸入s就可以進行搜尋快速跳轉(強迫症表示不能忍....) 具體做法見官方文件

3.預設作為這個外掛的快捷鍵其實挺好的, 貌似沒有其他外掛會導致衝突, 還可以配置一整套, 強迫症很滿意

4.可以配置2/n個字元的搜尋跳轉, 更精準, 按需自取(個人覺得太複雜了沒必要) 文件和文件

5.這個外掛專心做好跳轉就好, 沒必要把搜尋的活給做了

Vim視窗切換

滑鼠在各個視窗間迴圈移動: ctrl+w+(小寫的 hjkl), "非線性"的跳轉的: ctrl_w+t(top : 左上角, +b: bottom, 右下角), p: preview: 上一個子視窗. set mouse=a 所有all 的狀態下都可以使用 滑鼠..

視窗本身的位值的移動: ctrl_w + r: 視窗本身, 不是滑鼠指標順時針 (向下, 向右 移動), R : 則是逆時針反方向(向上, 向左)移動. ctrl_w+x: 左右上下對應位置的視窗 對調. 要注意視窗必須是 對應的, 如果不對應將無法對換, 比如左邊一個大視窗, 右邊有兩個小的 子視窗, 則左右不能互換.

視窗本身 的位置移動, 而且大小也發生"最大化"變化 Ctrl_w+ HJKL( 注意是大寫的字母 H, J, K, L , 表示要按shift才能實現的)... 要注意, 可以通過 windows 視窗 "貼邊" 最大化來理解, H和 L 就是 向左或向右 最大化貼邊 顯示; 而 JK 則是 向上 或 向下 貼邊 最大化顯示. 最大化後 就不能 再次操作復原視窗了, 其實也沒有必要

調整視窗的水平/垂直尺寸? 用ctrl+ w 結合 >, <調整水平尺寸, 用+ - 調整垂直尺寸, 這個是微調. 也可以用純粹的命令用 :resize +/- n, 或者 vertical resize +/- n (支援命令簡寫, 但是要能夠使命令被唯一確定才行. 通常要用5,10,15, 20的大小間隔來調...太小了沒有意義)

除了這些調整/ 遍歷滑鼠的方法, 還有一個關閉子視窗的問題. 關閉的方式, 除了命令外, 還有視窗關閉 的方式: 用ctrl+w + q(quit), c(close), o(other)等.

VIM註釋外掛nerdcommenter

nerdcommenter

使用:  
1、 \cc 註釋當前行和選中行  
2、 \cn 沒有發現和\cc有區別  
3、 \c<空格> 如果被選區域有部分被註釋,則對被選區域執行取消註釋操作,其它情況執行反轉註釋操作  
4、 \cm 對被選區域用一對註釋符進行註釋,前面的註釋對每一行都會新增註釋  
5、 \ci 執行反轉註釋操作,選中區域註釋部分取消註釋,非註釋部分新增註釋  
6、 \cs 新增性感的註釋,程式碼開頭介紹部分通常使用該註釋  
7、 \cy 新增註釋,並複製被新增註釋的部分  
8、 \c$ 註釋當前游標到改行結尾的內容  
9、 \cA 跳轉到該行結尾新增註釋,並進入編輯模式  
10、\ca 轉換註釋的方式,比如: /**/和//  
11、\cl \cb 左對齊和左右對其,左右對其主要針對/**/  
12、\cu 取消註釋  
複製程式碼

It requires Vim 7.3.885 or later with Lua support (“+lua”)

方案一:

brew install vim --with-lua

方案二:

brew uninstall vim
brew install luajit
brew install vim --with-luajit
複製程式碼

更多文章請轉到github.com/itgoyo/500D…

歡迎關注本人的Github:itgoyo

相關文章