如何在 Rider 設定 IdeaVim

在 VS IDE 用了 Vim 一段時間了,現在我要將 VS IDE 的 Vim 設定搬到 Rider,以下是我的設定步驟

下圖出自:https://blog.csdn.net/qq_42239765/article/details/103874859?utm_medium=distribute.pc_relevant.none-task-blog-title-3&spm=1001.2101.3001.4242

image

IdeaVim

安裝

Settings -> Plugins -> 搜尋 vim

Plugins 
Q' vin-l 
Search Results (10) 
IdeaVim 
8.6M 
> 
Settings 
Appearance & Behavior 
Keymap 
Editor 
Plugins 
Environment 
Version Control 
Build, Execution, Deployment 
Languages & Frameworks 
Tools 
Other Settings 
4.41 
Marketplace 
Sort By: Relevance 
INSTALL 
INSTALL 
Installed 
x 
IdeaVim 
8.6M 4.41 JetBrains 
LL@orLL LKeymapJL 0.59 
IdeaVimExtension 
U 52.8K 04.68 
Idea Vim-EasyMotion 
03.87 
Auto Switch Input Source In IdeaVi.. 
INSTALL 
Plugin homepage 
Vim emulation plug-in for IDES based on the IntelliJ platform. 
IdeaVim supports many Vim features including normal"nsert/visual modes, 
motion keys, deletion/changing, marks, registers, some Ex commands, Vim 
regexps, configuration via -/.ideavimrc, macros, window commands, etc. 
See also: 
• GitHub repository: documentation and contributing 
• Issue tracker: feature requests and bug reports 
Change Notes

 

裝好之後重新啟動 Rider

IDE and Plugin Updates 
x 
Restart JetBrains Rider to apply changes in plugins? 
RESTART 
NOT NOW

 

在 IDE 的右下角可以找到 IdeaVim 的 Icon,執行畫面如下:

IdeaVim 
1+Shift+F12 
el 
Its agc 
Open -/.ideavimrc 
Settings... 
EAP 
Contacts & Help 
Version 0.59

 

自訂 Vim 啟用/停用 熱鍵

在某些情境需要關閉 Vim,比如說多攔選取,Vim 就需要停用,我想和 Vistual Studio 的設定一樣都是 Ctrl + Shift + F12,

設定步驟,在 Rider -> Ctrl+Alt + s -> 選擇 Keymap -> Vim Emulator -> Add Keyboard Shortcut

Keymap 
2 
Visual Studio by Yao 
Get more keymaps in Settings I Plugins 
> 
Settings 
- Behavior 
Appearanc 
Keymap 
Plugins 
Environment 
Version Control 
Build, Execution, Deployment 
Languages & Frameworks 
Tools 
Other Settings 
MANAGE LAYERS 
Main menu 
Emulator 
Reload 
Shortcuts 
Actions 
Emulator 
{c. reload 
Based on Visual Studio keymap 
Add Keyboard Shortcut 
Add Mouse Shortcut 
Add Abbreviation 
Remove 
Reset Shortcuts 
3 
SAVE 
_ trl•shiftti-l_ 
Ctrl*Shift+F1 
CANCEL

 

自訂 Vim 熱鍵

它會讀取 %UserProfile%/.ideavimrc;如果檔案不存在,會跳出以下視窗讓你選擇,vim 的熱鍵設定就只是一個文字檔

Register New File Type Association 
The file 'ideavimrc• is not associated with any file type. Please define the association: 
File pattern 
.ideavimrc 
Open matching files in JetBrains Rider: 
CypeScrlpt 
TypeScript Config 
TypeScript JSX 
UlElement Style Sheet File (Unity) 
ce UlElement UXML File (Unity) 
Unity Asset 
O Open matching files in associated application 
0K 
CANCEL

 

按鍵對映

主要有 map,noremap,unmap,mapclear四個 命令,並且有四個字首i,c,n,v修飾

 

四個命令

  • nore:非遞迴
  • map:遞迴的對映
    如果 a 鍵被對應成了 b 鍵,c 鍵又被對應成了 a 鍵,遞迴對應會將,c 鍵對應成 b 鍵
  • unmap:刪除某個對映
  • mapclear:清除對映

 

四種修飾

  • n:一般模式下生效
  • v:可視模式下生效
  • i:插入模式下生效
  • c:命令列模式下生效

 

如何定義

以下是我常在用的部分熱鍵,將它貼到 .ideavimrc

"設定前贅詞
let mapleader=','

"載入vim設定檔
nmap <Leader>so :source C:\Users\yao\.ideavimrc<CR>

"設定搜尋時高亮顯示
set incsearch

"設定搜尋後高亮顯示
set hlsearch

"清除高亮搜尋
nnoremap <Leader>sc :nohlsearch<CR>
inoremap <Leader>sc <Esc>:nohlsearch<CR>

"顯示相對座標
"set relativenumber
set number

"顯示命令
set showmode

"<Ctrl><f>視窗往下捲一頁
nnoremap <Leader>f <C-f>

"<Ctrl><b>視窗往上捲一頁
nnoremap <Leader>b <C-b>

"<Ctrl><d>視窗往下捲半頁
nnoremap <Leader>d <C-d>

"<Ctrl><u>視窗往上捲半頁
nnoremap <Leader>u <C-u>

"<Ctrl><e>視窗往下捲一行
nnoremap <Leader>e <C-e>

"<Ctrl><y>視窗往上捲一行
nnoremap <Leader>y <C-y>

"退出目前文件
nnoremap <Leader>q :q!<CR>

"退出所有文件
nnoremap <Leader>Q :qa!<CR>

"複製目前單字到寄存器 o
nnoremap <Leader>y mo"+yiw"ayiw
第一次使用的時候要重開 Rider,接下來只要 .ideavimrc 檔案有異動,就按 ,so,載入新的設定,在一般模式下輸入 ,+ q,就會關閉文件

 

Rider Action 對映

除了可以針對 Vim 對應熱鍵,也可以對應 Rider 的 Action,這裡的關鍵就在於怎麼找出 Rider 的 Action

在一般模式下,輸入 :actionlist

file 
9 
8 
7 
6 
5 
4 
3 
2 
1 
1€ 
Edit View Navigate 
DEBUG I ANY CPU 
Code 
Refdctor Build Run 
ADD CONFIGURATION... 
Jests 
To 
Class Library 
: actionlist 
Terminal 
using System; 
namespace ClassLibrary1 
public class Classl 
(Y ClassLibrary1 
Performance Profiler •e Z: NuGet g: Unit Tests Dynamic Program Analysi 
V ClassLibrary1 10:1 CRLF UTE-8 4 spaces @

 

他會將所有的 Action 呈現出來,可以複製到記事本,比較好查看

Edit View Navigate 
DEBUG I ANY CPU 
- Actions 
scut 
$Detete 
$LRU 
$paste 
$Redo 
$SearchWeb 
$SetectAtt 
$1J11d0 
<anonymous-group-O> 
MORE 
Terminal 
file 
<C-L> 
Code 
Refdctor Build Run 
ADD CONFIGURATION... 
Jests 
To 
Class Library 
<S-Ins> 
cc-s-Z> 
ClassLibrary1 
Performance Profiler •e Z: NuGet g: Unit Tests 
V ClassLibrarY1 10:1 CRLF UTE-8 
Dynamic Program Analysi 
4 spaces @

 

若要查詢某一個功能,但不知道它的完整名稱,:actionlist xxx,xxx 指的就是查詢字串,我以 File Member 為例,這個功能是列出目前檔案有哪些成員

功能選單是 Navigate -> File Member

用 Ctrl + Shift + A 找 File Member

All 
Classes 
Files 
Symbols 
Actions 
File 
File 
File Encoding 
File History 
amember Alt+\ 
File Nome 
File Open Actions 
Include disabled actions 
- Alt+ErlteI to assign d 
Navigate 
Edit cop

找到 Action,按 Alt+Enter  可以直接設定熱鍵,這功能很棒

 

我直覺想到

:actionlist member

:actionlist file

 

最後,在 :actionlist file ,看到 Rider 的熱鍵 Alt + \,FileStructurePopup 就是 File Member 的指令

53 
54 
55 
56 
57 
58 
59 
FilePropertiesGroup 
FileStructurePopup 
FileWatcher. runForFiLes 
FindUsagesInFiLe 
Git . FileHistory . ContextMenu

 

最後,把它放到 .ideavimrc,存檔

"顯示檔案成員
nmap ,m :action FileStructurePopup<CR>

 

切換到 Rider 的編輯畫面,輸入 ,so 載入新的設定,輸入 ,m

 

熱鍵只需要針對常用的功能設定即可,不需要設定所有的功能

 

參考:
https://github.com/JetBrains/ideavim

Relative Line Numbers

使用 Vim 還需要相對座標,除了使用 set relativenumber 之外,還可以安裝以下套件,這個套件在 Plugins 找不到,需要到網頁下載,手動安裝

https://plugins.jetbrains.com/plugin/7414-relative-line-numbers/versions

 

安裝結果如下:右邊白色的 Number,是 Vim 的相對座標,左邊則是絕對座標

 

如果不喜歡它內建的顏色,還可以變更顏色,輸入 Ctrl + Alt + S 搜尋 VCS,找到 Color Scheme -> VCS Annotations -> Foregrond

 

Rider 2020.3 之後的版本,已經不支援這個套件,目前的解法還是使用 IdeaVim 的座標,我啟用混用座標

完整
:set number relativenumber

簡寫
:set nu rnu

 

停用

:set number! relativenumber!
:set nu! rnu!
 

AceJump

除了用 IdeaVim 定位之外,還可以使用 AceJump

安裝

Settings -> Plugins -> 搜尋 AceJump,安裝它

Settings 
> Appearance & Behavior 
Keymap 
> Editor 
Plugins 
Environment 
> Version Control 
> Build, Execution, Deployment 
) Languages & Frameworks 
> Tools 
> Other Settings 
plugins 
Q' acejumd 
Search Results (3) 
AceJump 
396.4K 
4.53 
Marketplace 
Sort By: Relevance 
INSTALL 
INSTALL 
Installed 
AceJump 
U 396.4K f 4.53 AceJump 
3.6.3 
INSTALLED 
visible in 
pe a 
AceJump-Lite 
ur..5K 
emacsIDEAs 
U 39.1K 0 4.63 
Plugin homepage 
AceJump allows you to quickly navigate the caret to a 
the editor. See a demo of AceJump in action! Simply hi "ctrl•;", 
character, then type the matching character to Ace Jump. 
Change Notes 
swe: 335.8K

 

Ctrl + ; 開啟 AceJump 模式,輸入 s ,這裡你可以換成你想定位的內容,可以從下圖看到 s 已經有標上額外的標籤,輸入標籤的內容焦點鍵盤焦點就會移過去了

file Edit View Navigate Code 
I ANY CPU 
C# Classl.cs x 
jdeavimrc 
Refdctor Build Run 
ADD CONFIGURATION... 
Jests 
To 
Class Library 
4 
3 
2 
1 
4 134 
1 
2 
3 
4 
5 
6 
7 
Terminal 
map zrp : action 
"*fi--ti 
map zri : action 
map zrv :action 
map zrf : action 
Performance Profiler •e Z: NuGet g: Unit Tests 
Misc Files 
134:1 CRLF UTE-8 
g 34 v 
Dynamic Program Analysi 
4 spaces @

 

參考:
https://github.com/acejump/AceJump

 

若有謬誤,煩請告知,新手發帖請多包涵


Microsoft MVP Award 2010~2017 C# 第四季
Microsoft MVP Award 2018~2022 .NET

Image result for microsoft+mvp+logo