跳到內容

VI 模式

Introduction

Vim Logo

Kate's VI mode is a project to bring Vim-like, modal editing to the Kate text editor and by extension to other KDE programs who share the same editor component. The project started as a Google Summer of Code project in 2008 – where all the basic functionality was written. I have continued to maintain and further develop this code and the number of missing features from Vim are slowly decreasing. Most Vim users will already be productive in Kate's VI mode. A list of what's missing is found at the bottom of the page.

This page is meant to be an updated overview of this work.

To enable the VI input mode, go to
Settings → Configure Kate… → Editing → VI Input Mode.
It can also be toggled with the "VI Input Mode" setting in the "Edit" menu. (The default shortcut key is Meta+Ctrl+V – where Meta usually is the Windows key).

目標

VI 模式的目標_不是_要完全替代 Vim 或是支援 Vim 的_所有_功能。它的目標是讓 Vim 的文字編輯邏輯 - 和您所學到的 Vim 習慣 - 能夠用於將 Kate 作為內部編輯器的程式。這些程式包含

  1. Kate 文字編輯器本身
  2. KWrite – KDE 的簡潔文字編輯器
  3. KDevelop – 一個許多程式語言用的進階 IDE
  4. Kile – LaTeX 編輯器

VI 模式希望與這些程式乾淨地整合,並在合理的時候會與 Vim 的行為不同。舉例來說,Kate 的 VI 模式中 :w 會開啟儲存對話框。

與 Vim 不相容之處

除了缺乏的東西以外,Kate 的 VI 模式只有幾個功能與 Vim 不相容。以下為不相容功能的列表,以及不相容的原因。

  1. Kate: UCtrl+r 是重做
    Vim: Ctrl+r 是普通重做,U 則是用來重做一行中所有最新的變更
    在 Kate 的 VI 模式讓 U 進行普通重做的原因是因為 ctrl+r 預設被 Kate 的取代(搜尋與取代)功能所使用。VI 模式預設不會覆寫 Kate 的快捷鍵(這可以在_設定 → 設定 Kate… → 編輯 → Vi 輸入模式_進行設定),所以需要有一個普通按鍵來觸發重做的動作。另外,Vim 當中 U 命令的行為在 Kate 內部的復原系統中沒有很好的對應,所以要支援該功能也會非常難。
  2. Kate: :print shows the "print" dialogue
    Vim: :print prints the lines of the given range like its grandfather ed Commands like :print are available not only in the VI mode but for users using “regular” Kate, too – I have therefore chosen to let the :print command open the print dialogue – following the principle of least surprise instead of mimicking Vim's behaviour.
  3. Kate: Y yanks to end of line.
    Vim: Y yanks whole line, just like yy. VI's behaviour for the Y command is in practice a bug; For both change and delete commands, cc/dd will do its action on the current line and C/D will work from the cursor column to the end of the line. However, both yy and Y yanks the current line.In Kate's VI Mode Y will yank to the end of the line. This is described as "more logical" in the Vim documentation.
  4. Kate: :map alters the selected lines of the document using the provided JavaScript expression.
    Vim: :map adds the provided mapping to Normal and Visual modes. The "map" command was already reserved by Kate; in 4.12+, you can use a combination of :nmap and :vmap to replace it.

支援的命令

Supported normal/visual mode commands

按鍵描述
aEnter Insert Mode and append
AEnter Insert Mode and append to EOL
iEnter Insert Mode
IInsert before first non-blank char in line
vEnter Visual Mode
VEnter Visual Line Mode
<c-v>Enter Visual Block Mode
gvRe-select Visual
oOpen new line under
OOpen new line over
JJoin lines
cChange
CChange to EOL
ccChange line
sSubstitute char
SSubstitute line
ddDelete line
dDelete
DDelete to EOL
xDelete char
XDelete char backward
guMake lowercase
guuMake lowercase line
gUMake uppercase
gUUMake uppercase line
yYank
yyYank line
YYank to EOL
pPaste
PPaste before
r.Replace character
REnter replace mode
:切換至命令列
/搜尋
u復原
<c-r>重做
U重做
m.Set mark
>>Indent line
<<Unindent line
>Indent lines
<Unindent lines
<c-f>Scroll page down
<pagedown>Scroll page down
<c-b>Scroll page up
<pageup>Scroll page up
<c-u>Scroll half page up
<c-d>Scroll half page down
zzCentre view on cursor
gaPrint character code
.Repeat last change
==Align line
=Align lines
~Change case
<c-a>Add to number
<c-x>Subtract from number
<c-o>Go to prev jump
<c-i>Go to next jump
<c-w>hSwitch to left view
<c-w><c-h>Switch to left view
<c-w><left>Switch to left view
<c-w>jSwitch to down view
<c-w><c-j>Switch to down view
<c-w><down>Switch to down view
<c-w>kSwitch to up view
<c-w><c-k>Switch to up view
<c-w><up>Switch to up view
<c-w>lSwitch to right view
<c-w><c-l>Switch to right view
<c-w><right>Switch to right view
<c-w>wSwitch to next view
<c-w><c-w>Switch to next view
<c-w>sSplit horizontally
<c-w>SSplit horizontally
<c-w><c-s>Split horizontally
<c-w>vSplit vertically
<c-w><c-v>Split vertically
gtSwitch to next tab
gTSwitch to prev tab
gqqFormat line
gqFormat lines
q. / qBegin/ finish recording macro using the named macro register.

Supported motions

按鍵描述
hLeft
<left>Left
<backspace>Left
jDown
<down>Down
<enter>Down to first non blank
kUp
<up>Up
Up to first non blank
lRight
<right>Right
<space>Right
$To EOL
<end>To EOL
To 0 column
<home>To 0 column
^To first character of line
f.Find char
F.Find char backward
t.To char
T.To char backward
;Repeat last t. or f. command
,Repeat last t. or f. command
nFind next
NFind prev
ggTo first line
GTo last line
wWord forward
WWORD forward
bWord backward
BWORD backward
eTo end of word
ETo end of WORD
geTo end of prev word
gETo end of prev WORD
%To matching item
`[a-zA-Z><]To mark
'[a-zA-Z><]To mark line
[[To previous brace block start
]]To next brace block start
[]To previous brace block end
][To next brace block end
*To next occurrence of word under cursor
#To prev occurrence of word under cursor
HTo first line of window
MTo middle line of window
LTo last line of window
gjTo next visual line
gkTo prev visual line

Supported text objects

按鍵描述
iwInner word
awA word
iWInner WORD
aWA WORD
i"Inner double quote
a"A double quote
i’Inner single quote
a’A single quote
i`Inner back quote
a`A back quote
ibInner paren
i)Inner paren
i(Inner paren
abA paren
a)A paren
a(A paren
iBInner curly bracket
o}Inner curly bracket
i{Inner curly bracket
aBA curly bracket
a}A curly bracket
a{A curly bracket
i<Inner inequality sign
i>Inner inequality sign
a<A inequality sign
a>A inequality sign
i[Inner bracket
I]Inner bracket
a[A bracket
a]A bracket
i,Inner comma
a,A comma

Supported insert mode commands

按鍵描述
<c-d>Unindent
<c-t>Indent
<c-e>Insert from below
<c-y>Insert from above
<c-w>Delete word
<c-r>.Insert content of register
<c-o>Switch to normal mode for one command
<c-a>Increase number under cursor
<c-x>Decrease number under cursor

The Comma Text Object

This is something that I have been missing in Vim. The comma text object makes it easy to modify parameter lists in C-like languages and other comma separated lists. It is basically the area between two commas or between a comma and a bracket. In the line shown in the illustration to the right, the three ranges this text object can span are highlighted in red.

comma to

Comma text object ranges. If the cursor is over, say, “arg2”, pressing c i , (“change inner comma”) would delete “double arg2” and place the cursor between the two commas in insert mode. A very convenient way to change a function's parameters.

Emulated Vim Command Bar

Kate 4.11 introduced a hidden config option that make /, ? and : bring up a new search/ command in place of the usual Kate Find / Replace / Command-line bar. The bar is intended to replicate many of the features of Vim's command bar, and also to fix many of the issues with Kate Vim mode's interaction with Kate's Find/Replace bar (interactive replace not working; incremental search not positioning the cursor correctly; not usable in mappings/macros; etc).

The following shortcuts are provided by the emulated command bar; as with Vim, these can be remapped with cmap, cnoremap, etc:

按鍵描述
<c-r>.insert contents of register.
<c-r><c-w>Insert word under the (document) cursor.
<c-p>Invoke context-specific completion (see below)
move back/ up in the completion list.
<c-p>Move forward/ down in the completion list.
<c-space>Kate Vim Extension.
Auto-complete word from document.
<c-d>Kate Vim Extension.
In a sed-replace expression (i.e. s/find/replace/[g][c][i]),
clear the "find" term and place the cursor there.
<c-f>Kate Vim Extension.
In a sed-replace expression (i.e. s/find/replace/[g][c][i]),
clear the “replace” term and place the cursor there.
<c-g>.Kate Vim Extension.
As with ., insert the content of the named register,
but escape it in such a way that when used with a search,
we search for the literal content of the register;
not the content of the register interpreted as a regex.

The "context-specific completion" is decided as follows:

  • In a search bar (/ or ?), auto-complete from search history (which includes searches initiated via *; and #; searches done in sed-replace expressions; etc.)
  • In an empty command bar (:), auto-complete from command history (NB: auto-completion of command names is invoked automatically when you begin typing).
  • In a command-bar containing a sed-replace expression (e.g. :s/find/replace/gc), if the cursor is positioned over "find", auto-complete from the "search" history; if over the "replace", auto-complete from the history of "replace" terms.

When executing a sed-replace expression in the command bar with the "c" flag (e.g. s/find/replace/gc), a Vim-style interactive search/replace expression will be initiated.

Some example usages of the emulated command bar, with GIF animations, are given in this blog. In 4.11, the emulated command bar can be enabled by setting the hidden config option "Vi Input Mode Emulate Command Bar" to true in your katerc/kwriterc/kdeveloprc.

缺乏功能

As stated earlier, the goal of Kate's VI Mode is not to support 100% of Vim's features, however, there are some features which are sorely missed

  • Visual block mode – especially the ability to prepend/append text to the visual block selection.
  • Having ex commands available in other programs than the Kate application.
  • The search code needs improvement and the * and # commands should just be regular searches.

If you miss other features or want to help on the ones mentioned above, feel free to contact me or send patches! :-)

Change Log

  • 2010-05-16:
    Initial version. Collected the information from blog entries and README files to make a single source of current information.
  • 2010-05-17:
    Ctrl+A and Ctrl+X added (increment/decrement number under cursor).
  • 2010-08-30:
    Moved page to kate-editor.org.
  • 2010-09-10:
    Fixed the text on the comma text object and made some formatting fixes
  • 2021-02-03:
    Converted key-binding tables to markdown for hugo.