跳到內容

學習 vi 編輯器/Vim/Vim 在 Windows 上

來自華夏公益教科書,為開放世界提供開放書籍

學習 vi 編輯器: 熟悉基本任務讓工作更輕鬆高階任務細節Vi 克隆 (Vim基本導航模式技巧和竅門程式設計師需要了解的有用內容增強 VimVim 在 Windows 上VimL 指令碼語言, Vile, BB vi)vi 參考

Vim 可用於 Microsoft Windows,但該平臺存在許多問題,使得有效使用 Vim 非常具有挑戰性。本節將討論一些與使用或考慮在 Windows 系統上使用 Vim 的使用者相關的問題。

對於希望在 Windows 上安裝 Vim 的使用者,有多種安裝選項。

其中包括

  • 自安裝可執行檔案
  • Win32s GUI 可執行檔案
  • Windows 3.1 GUI 可執行檔案
  • Win32 控制檯可執行檔案
  • Win64 二進位制檔案(適用於 x64)

此外,如果需要,使用者可以從原始碼編譯 Vim。如果使用者希望新增對預設情況下未編譯到預先存在的二進位制檔案中的其他選項的支援,這可能有用甚至必要。也許在 Windows 上編譯最常見的場景是使用者希望新增對預設情況下未包含在 Windows 二進位制檔案中的程式語言(特定語言或特定版本的語言)的支援。

安裝 Vim 時,使用者必須為 Windows 預設情況下不包含的任何必要元素(例如編譯器和 make)提供支援。

一般用法

[編輯 | 編輯原始碼]

不熟悉 Vim 工作原理和一般“vi 工作方式”的使用者可能會發現 Vim 尤其具有挑戰性。如果他們已經習慣了 Windows 上大多數其他文字編輯器的流程,尤其如此。因此,如果你想在 Windows 上使用 Vim,實際上不止一種方式。在 Windows 上使用 Vim 的一種方式可以稱為*標準 vim 工作流程* (Vim);另一種方式則是*windows vim 工作流程* (WinVim)。

對於那些不適應 Vim 通常帶來的陡峭學習曲線的使用者來說,使用“WinVim”風格絕對有優勢;然而,Vim 中的許多功能在使用“WinVim”模式時很難使用。選擇使用哪種使用模式或兩者結合,很大程度上取決於個人喜好。

WinVim 行為級別

[編輯 | 編輯原始碼]

:behave mswin 配置允許使用者將編輯器行為設定為反映“WinVim”工作風格的某些方面。這使“WinVim”風格成為一個“縮減版”;那些想要非常詳細地模擬 WinVim 風格的使用者,必須做的不僅僅是設定“behave mswin”。有關詳細資訊,請參見 :help behave

Cream 編輯器

[編輯 | 編輯原始碼]

Cream 編輯器是 Vim 配置的一個示例,它專為喜歡“WinVim”工作風格的使用者而設計。

相容性

[編輯 | 編輯原始碼]

對於那些熟悉類 Unix 作業系統並利用這些系統來增強和擴充套件 Vim 的使用者來說,Windows 作業系統可能會呈現一些意想不到的差異和限制。

路徑分隔符

[編輯 | 編輯原始碼]

Windows(預設情況下)使用反斜槓字元來分隔檔案路徑中的路徑步驟。這對於不習慣此操作的使用者來說可能具有挑戰性。有關詳細資訊,請參見 :help shellslash

命令管道

[編輯 | 編輯原始碼]

Windows 命令外殼不支援與 Unix 系統相同的方式的命令管道和重定向。一些使用者可能會發現這尤其具有挑戰性,因為它極大地限制了 Vim 與編輯器外部的其他程式互動的方式。但是,有一些解決方法,允許使用者近似模擬管道和重定向帶來的某些功能。

臨時文字傳輸檔案

[編輯 | 編輯原始碼]

臨時文字傳輸檔案是一種解決方法,允許 Vim 在 Windows 上與其他程式互動,而無需使用管道和重定向。基本方法是在本地檔案系統上指定一個或多個臨時檔案,Vim 和外部程式使用這些檔案來“傳輸”彼此之間的文字。

字型和語言支援

[編輯 | 編輯原始碼]

在 Windows XP 中,gVim 7.4 (7.4.280) 在顯示非西方字元方面存在問題。即使字元不可見,gVim 也會保留這些字元,並且相同文字在支援更多字型的程式(例如辦公套件或 Web 瀏覽器)中應該看起來正常。

可顯示的字型可以透過編輯選單 > 選擇字型...進行更改,可以選擇用於特定語言的字型。字型視窗僅包含等寬字型的字型列表,這使得字型選擇很少。可以選擇現有的非西方字型,並且根據字型支援的指令碼,所選指令碼(在指令碼:)的示例區域將顯示非西方指令碼示例以及西方字元。要確認是否支援某種語言指令碼,請開啟指令碼:下拉選單。例如,支援韓語的字型在指令碼:下拉選單中包含韓文。

對於大多數西方指令碼,下表顯示了 gVim 可見的開源等寬字型的非詳盡選擇,這些字型提供了最多的語言支援和最佳的可讀性。這些字型必須單獨下載,但可以與專用軟體包一起提供。為了比較,顯示了 Windows 預設的Lucida。(字型大小要求可能因使用者而異。)

字型名稱 許可證 版本 包含在 支援的指令碼 備註
DejaVu Sans Mono FOSS:各種許可證 2.35 8:西歐、希臘、土耳其、波羅的海、中歐、西里爾、越南、阿拉伯 基於 Bitstream Vera.
維基百科文章:DejaVu 字型.
Liberation Mono FOSS:SIL 開放字型許可證 2.00.1 LibreOffice 5.0 8:西歐、希伯來語、希臘、土耳其、波羅的海、中歐、西里爾、越南 Courier New 度量相容。OFL 自版本 2+ 起。
維基百科:Liberation 字型.
Lucida Console 非免費 1.60 Windows XP SP3、MS Office 2007 5:西歐、希臘、土耳其、中歐、西里爾 這是從 Windows 2000 開始的預設控制檯字型,以及從 Windows 2000 到 Windows 8 的預設記事本字型。
Ubuntu Mono FOSS:Ubuntu 字型許可證 0.80 6:西歐、希臘、土耳其、中歐、西里爾 維基百科:Ubuntu(字型).

與 CUA 風格編輯器的比較

[edit | edit source]

下表顯示了從 CUA 風格編輯器的角度來看的一些基本 Vim 功能的比較。

這旨在作為那些可能不熟悉 Vim 但熟悉使用標準“檔案 :: 編輯 :: 幫助”風格選單的基於 Windows 的編輯器的使用者的起點,例如 Notepad.exe,它預設情況下在大多數 Windows 安裝中可用。

### --------------------
file                           ;;   :h files                                ;;  blank___
file                           ;;   :h buffers                              ;;  blank___
file.exit                      ;;   :qa                                     ;;  close all files and quit
file.explore                   ;;   :Vexplore .                             ;;  (:h :Vexplore)
file.explore                   ;;   vim .                                   ;;  startup on a directory
file.open                      ;;   :e .                                    ;;  open directory
file.open                      ;;   :e /foo                                 ;;  open directory
file.open                      ;;   :e /foo/bar.txt                         ;;  open file
file.open.hyperlink            ;;    <c-w>f	                                ;;  open in a new window (Ctrl-w f)
file.open.hyperlink            ;;   gf                                      ;;  open in same window
file.open.hyperlink            ;;   <c-w>gf                                 ;;  open in a new tab (Ctrl-w gf)
file.reload                    ;;   :edit                                   ;;  reload to last-saved version of the file
file.revert                    ;;   :edit                                   ;;  revert to last-saved version of the file
file.save                      ;;   :w                                      ;;  write the current buffer to file
file.save_all                  ;;   :wa                                     ;;  save all modified buffers
file.save_and_quit             ;;   ZZ                                      ;;  :help ZZ "write the current file and quit

### --------------------
autocorrect                    ;;  :help abbreviations                      ;;  blank___

### --------------------
cursor                         ;;  :help motion.txt                         ;;  cursor motions and selections
cursor.back                    ;;  ''                                       ;;  :help ''
cursor.move                    ;;  arrow keys                               ;;  the CUA way
cursor.move                    ;;  hjkl                                     ;;  the vim way
cursor.top_of_file             ;;  gg                                       ;;  compare to c-home in CUA editors
cursor.top_of_file             ;;  :normal gg                               ;;  blank___

### --------------------
edit                           ;;   blank___                                ;;  blank___
edit.cut                       ;;   blank___                                ;;  blank___
edit.copy                      ;;   blank___                                ;;  blank___
edit.copy_line                 ;;   yy                                      ;;  copy (yank) the entire line
edit.cut.by_pattern            ;;   :g/pattern/y A                          ;;  "./try-basicops-edit.txt.txt"
edit.find                      ;;   /                                       ;;  blank___
edit.find_in_files             ;;   vimgrep                                 ;;  "./try-basicops-find-replace.txt"
edit.find_results              ;;   :g//                                    ;;  href="C:/sm/docs/mydaydirs/2013/week11/vim-cua-learnbit.txt"
edit.paste                     ;;   blank___                                ;;  blank___
edit.select.line               ;;   blank___                                ;;  blank___
edit.select.all                ;;   blank___                                ;;  blank___
edit.select.pargraph           ;;   blank___                                ;;  blank___
edit.preferences               ;;   see preferences                         ;;  blank___
edit.currentfile.prefs         ;;   see preferences                         ;;  blank___

### --------------------
edit.change.uppercase           ;;                                          ;; change text to uppercase

### --------------------
edit.delete_blank_lines        ;; :g/^\s*$/:delete
edit.slash_forward             ;;   :{range}s/\\/\//                        ;;  | echo "slash forward"
edit.slash_back                ;;   :{range}s/\//\\/                        ;;  | echo "slash back"
edit.slash_forward             ;;   :s=\\=/=g                               ;;  | echo "slash forward"
edit.slash_back                ;;   :s=/=\\=g                               ;;  | echo "slash back"
edit.sort.file                 ;;   :sort                                   ;;  :h sort
edit.sort.range                ;;   :{range}sort                            ;;  :h sort   :h range
edit.sort.paragraph            ;;   blank___                                ;;  blank___
edit.sort.unique               ;;   :{range}sort u                          ;;  blank___
edit.ws_trim                   ;;   :%s=\s\+$==                             ;;  trim trailing whitespace

### --------------------
insert                         ;;   :help insert                            ;;  insert is a separate "mode" in vim
insert.autocomplete            ;;   ctrl-x // ctrl-n // ctrl-p              ;;  blank___
insert.date                    ;;   google://vim insert date                ;;  insert current system date. there are multiple ways to do this
insert.date (linux  )          ;;   :r !date                                ;;  blank___
insert.date (windows)          ;;   blank___                                ;;  blank___
insert.directory_list          ;;   :r ! ls -al;                            ;;  blank___

### --------------------
search                         ;;   blank___                                ;;  blank___
search.matching lines          ;;   :g/{pattern}/:print                     ;;  print all lines matching pattern
search.matching lines          ;;   :g/{pattern}/:print                     ;;  print all lines matching pattern

### --------------------
code.syntax_coloring           ;;   :set ft=php                             ;;  blank___

### --------------------
navigate.home                   ;; 1G                                       ;; go to top of file
navigate.home                   ;; gg                                       ;; go to top of file
navigate.home                   ;; 1gg                                      ;; go to top of file
navigate.end                    ;; 9999gg                                   ;; go to end of file (assumes file is shorter than 9999 lines)
navigate.end                    ;; G                                        ;; go to end of file

### --------------------
preferences                    ;;   vim ~/.vimrc                            ;;  easy editing of preferences
preferences.komodo             ;;   compare preferences pane in komodo      ;;  "C:\sm\docs\mydaydirs\2012\week36\editor-preferences-pane-001.png"
preferences.komodo             ;;   current file preferences in komodo      ;;  "C:\sm\docs\mydaydirs\2012\week36\editor-preferences-pane-001.png"
preferences.appearance         ;;   :set showcmd                            ;;  show keystrokes that you have typed in normal mode
preferences.cursor_position    ;;   :set ruler?                             ;;  blank___
preferences.line_num           ;;   :set number // :set nonumber            ;;  blank___
preferences.filetype           ;;   :set ft=php                             ;;  blank___
preferences.setting_change     ;;   :set ft=                                ;;  blank___
preferences.setting_view       ;;   :set ft?                                ;;  blank___
preferences.syntax_coloring    ;;   :set ft=php                             ;;  blank___
preferences.keyboard           ;;   :map                                    ;;  display key mappings

### --------------------
tools.interop                  ;;   :helpgrep away and come back            ;;  blank___
tools.python                   ;;   :echo has("python")                     ;;  blank___
tools.external                 ;;   :! drush cc all;                        ;;  example running drush
tools.external                 ;;   :! git commit -a -m "%:t"               ;;  example running git with interpolated current filename
tools.run                      ;;   :! git commit -a -m "%:t"               ;;  run the current file in another program
tools.run                      ;;   :! git commit -a -m "%:t"               ;;  run the current file as a script
tools.macro                    ;;   :helpgrep big changes                   ;;  "./try-basicops-tools.txt"
tools.repeat_command           ;;   <up>                                    ;;  "./try-basicops-tools.txt"
tools.normal_command           ;;   :normal                                 ;;  blank___

### --------------------
tools.scripting                ;;     :help user-function                   ;;  creating custom functions in vim
tools.scripting                ;;     :help vim-script-intro                ;;  creating custom functions in vim

### --------------------
window                         ;;    :help window                           ;;  "./try-basicops-windows.txt"
window.close_others            ;;    :only                                  ;;  blank___
window.equalize                ;;    ctrl-w =                               ;;  equalize the size of all windows
window.history.navigate        ;;    :pop :tag                              ;;  :h tag
window.history.forward         ;;    :ta[g]                                 ;;  :h ctrl-]
window.history.back            ;;    ctrl-O                                 ;;  :helpgrep tutorial // :help usr_01 //
window.history.back            ;;    :po[p]                                 ;;  blank___
window.rotate                  ;;    ctrl-w ctrl-r                          ;;  :help window-moving
window.rotate                  ;;    ctrl-w r                               ;;  
window.reshuffle               ;;    ctrl-w H                               ;;  two_handed_cmd
window.reshuffle               ;;    ctrl-w J                               ;;  two_handed_cmd
window.reshuffle               ;;    ctrl-w K                               ;;  two_handed_cmd
window.reshuffle               ;;    ctrl-w L                               ;;  two_handed_cmd
window.resize                  ;;    :resize +10                            ;;  height increase current window size
window.resize                  ;;    :resize -10                            ;;  height decrease current window size
window.resize                  ;;    :vertical resize -10                   ;;  width decrease current window size
window.resize                  ;;    :vertical resize +10                   ;;  width increase current window size
window.resize                  ;;    ctrl-w =                               ;;  (aka window.equalize)
window.split                   ;;    :split                                 ;;  blank___
window.split                   ;;    ctrl-w s                               ;;  blank___
window.navigate                ;;    ctrl-w + ctrl-w                        ;;  move cursor cycle thru all windows
window.scrolling               ;;    :set scrollbind                        ;;  dual pane windows scroll together
window.split.vertical          ;;    ctrl-w v                               ;;  blank___
window.split.vertical          ;;    :vsplit                                ;;  blank___
window.split.close             ;;    :close                                 ;;  closes the currently focused split
window.split.close             ;;    ctrl-w q                               ;;  closes the currently focused split
window.navigate                ;;    ctrl-w + arrow keys                    ;;  move cursor around between split windows (mouse_alternative)
window.tabs                    ;;    :h tabs                                ;;  blank___
window.tabprevious             ;;    :tabprevious                           ;;  blank___
window.tabnext                 ;;    :tabnext                               ;;  blank___
window.tabnext                 ;;    gt                                     ;;  blank___
window.tabmove                 ;;    <ctrl-arrow> (windows)                 ;;  requires MyVim_MoveTabPage
window.tabmove                 ;;    :tabmove N                             ;;  :tabmove 0 makes it the first tab // :help :tabm

### --------------------
help                           ;;   :help // :h // :help help               ;;  overview of the help system
help.find                      ;;   :helpgrep /pattern/                     ;;  search for a term in the help system (see also :copen)
help.context_specific          ;;   :helpgrep help-context                  ;;  shows the different contexts for which you can get help
help.autocomplete              ;;   ctrl-d                                  ;;  shows completions for vim help

### --------------------
### [cross-reference: influx_either_kevin_pay]


學習 vi 編輯器: 熟悉基本任務讓工作更輕鬆高階任務細節Vi 克隆 (Vim基本導航模式技巧和竅門程式設計師需要了解的有用內容增強 VimVim 在 Windows 上VimL 指令碼語言, Vile, BB vi)vi 參考


華夏公益教科書