1. 程式人生 > >『力薦彙總』這些 VS Code 快捷鍵太好用,忍不住錄了這34張gif動圖

『力薦彙總』這些 VS Code 快捷鍵太好用,忍不住錄了這34張gif動圖

之前寫過三篇文章,收穫了極其不錯的閱讀量與轉發量: - [你真的會用 VS Code 的 Ctrl、Shift和Alt嗎?高效易用的快捷鍵:多游標、跳轉引用等輕鬆搞定](https://mp.weixin.qq.com/s/aQ6IwOP06-rP56CXTBN1Iw) - [VS Code 中的 Vim 操作 | 無需修改 VSC 預設快捷鍵 | 常用組合與邏輯](https://mp.weixin.qq.com/s/GgPyGX8eu4AgUDi3h7fPsw) - [跳轉引用、禪模式...VS Code 中 11 個快捷鍵/操作,讓你的效率與舒適度翻倍!](https://mp.weixin.qq.com/s/ZqWrIkG3vtdpwpp3u_baiA) 一方面,我確實將 VS Code 作為自己的主力編輯器:遠端操作時用、寫筆記時用、寫程式碼(C++/Python/JavaScript)時也用,我已經具有了些許心得與經驗;另一方面,自己也很喜歡積累或閱讀相關資料(比如韓駿老師的《Visual Studio Code 權威指南》),並且在實踐與學習中做成筆記(比如錄製gif)。 **本文將彙總我錄製的 34 張 gif 動圖,為讀者朋友推薦並整理『遠離滑鼠,提升效率』的 VS Code 快捷操作。** 本文大綱: - 6 個 `Ctrl+Shift+Alt` 組合 - 8 個你可能不知道的重要操作 - 3 個有趣又通用的小眾外掛 - 5+ 的 vim + vscode 操作 ## 6 個 Ctrl+Shift+Alt 組合 ### Ctrl Ctrl 控制螢幕的移動與游標的移動。 ![Ctrl + 左右](https://gitee.com/piperliu/picture/raw/master/2021-2-3/1612329604953-A1_c%E5%B7%A6%E5%8F%B3.gif) Ctrl 加上左右鍵,以單詞為單位跳轉。 ![Ctrl + 上下](https://gitee.com/piperliu/picture/raw/master/2021-2-3/1612329604955-A1_c%E4%B8%8A%E4%B8%8B.gif) Ctrl 加上上下鍵,小幅度滾動螢幕(滾動一行)。 ### Shift Shift 相當於「拖動滑鼠」,與方向鍵結合選中文字。 ![Shift + 上下左右](https://gitee.com/piperliu/picture/raw/master/2021-2-3/1612329604959-A1_s%E4%B8%8A%E4%B8%8B%E5%B7%A6%E5%8F%B3.gif) 有了 Shift ,不再用滑鼠拖動。 ### Alt Alt 較為特殊。 ![Alt + 上下](https://gitee.com/piperliu/picture/raw/master/2021-2-3/1612329604963-A1_a%E4%B8%8A%E4%B8%8B.gif) Alt 與上下鍵結合,英文叫做「copy line」,相當於拖著這一行上下移動。 ![Alt + 左右](https://gitee.com/piperliu/picture/raw/master/2021-2-3/1612329604978-a_%E5%B7%A6%E5%8F%B3.gif) Alt 與左右鍵結合,實現「跳轉」。比如我們去看某個函式的定義,我們跳轉過去,當我們看完了,想要跳轉回來時,不需要再動腦回找,直接使用 `Alt + ←` 即可(`Alt + backspace`同)。 ### Ctrl + Shift ![Ctrl + Shift + 上下左右](https://gitee.com/piperliu/picture/raw/master/2021-2-3/1612329604966-c+s%E4%B8%8A%E4%B8%8B%E5%B7%A6%E5%8F%B3.gif) `Ctrl + Shift + 上下左右` 較好理解,相當於 Ctrl 與 Shift 相結合, Shift 起選中的作用, Ctrl 則讓方向鍵的步長更長(以單詞為單位)。 ### Ctrl + Alt ![Ctrl + Alt + 上下](https://gitee.com/piperliu/picture/raw/master/2021-2-3/1612329604971-c+a%E4%B8%8A%E4%B8%8B.gif) `Ctrl + Alt + 上下` 是 **多游標** 。注意使用 Escape 退出多游標模式。 ### Shift + Alt ![Shift + Alt + 上下](https://gitee.com/piperliu/picture/raw/master/2021-2-3/1612329604974-s+a%E4%B8%8A%E4%B8%8B.gif) `Shift + Alt + 上下`,複製這一行。 ## 8 個你可能不知道的重要操作 ### F12 跳轉引用 ![使用 F12 實現跳轉](https://gitee.com/piperliu/picture/raw/master/2021-2-17/1613566736176-f12.gif) 如上圖: - `F12` 跳轉到變數、函式、類的定義處 - `Shift + F12` 跳轉到其引用處;多個引用時,可以選擇具體哪個 reference 使用 `F12` 也可以跳轉到環境庫的原始碼中,如下圖,我跳轉到了庫 gym (使用 conda 安裝到 lib)的 `core.py` 。 ![F12 跳轉到 gym 庫的定義中](https://gitee.com/piperliu/picture/raw/master/2021-2-17/1613566736170-f12_gym.gif) ### 查詢與替換 ![Ctrl + F 恐怕在任何軟體中都很常用](https://gitee.com/piperliu/picture/raw/master/2021-2-17/1613566736164-search.gif) 使用 `Ctrl + F` 可以進行查詢;使用 `Ctrl + H` 則彈出替換視窗。如上,我將 `foo` 替換為 `foobar` ,使用 `Alt + R` 支援正則語法。 ### Ctrl + Shift + P `Ctrl + Shift + P` 恐怕是各個編輯器、偵錯程式中最常用的操作了:彈出命令視窗,然後我們輸入相關命令的名稱,回車則執行該命令。 ![Ctrl + Shift + P](https://gitee.com/piperliu/picture/raw/master/2021-2-17/1613567485749-ctrl_p.gif) 如上圖,我使用 `Ctrl + Shift + P` ,執行 `View: Split Editor Right` 命令,接著,使用 `Ctrl + P` 調用出視窗切換器(檔案切換器)。 ### 切換視窗 處於一堆、相互重疊的檔案, VS code 稱其為一個「group」。我們通常要用到「group的組內切換」和「group間切換」。 ![group 切換](https://gitee.com/piperliu/picture/raw/master/2021-2-17/1613566736159-ctrl_num.gif) 如上圖,`Ctrl + <你要去的 group 編號>` 來把游標(的注意力 focus)集中到你要去的 group 上。上面 `Ctrl + 1` 切換到左邊的 group ;`Ctrl + 2` 切換到右邊的 group 。 而 `Alt + <數字>` 則是在 group 內切換標籤頁。 此外,注意到 `Ctrl + Shift + E` 開啟或關閉檔案管理器 Explorer ,就是左側的那個樹狀圖。而 `Ctrl + B` 是開關 Side Bar ,即側邊欄。 ### 新建、關閉與除錯臺 使用 `Ctrl + ~` 開關終端,而 `Ctrl + J` 則是開關 Panel ,即含有終端、偵錯程式的底部容器。 ![新建、關閉與除錯臺](https://gitee.com/piperliu/picture/raw/master/2021-2-17/1613566736154-ctrl_n.gif) 此外,`Ctrl + N` 是新建檔案,`Ctrl + W` 是關閉當前檔案(這在所有 windows 應用上幾乎都適用)。 ### 禪模式 有時候,節目顯得過於複雜臃腫,分散了我們的注意力,視野不清爽,也會降低舒適度。**使用禪模式 Zen Mode 則會遮蔽這些干擾。** ![開關禪模式](https://gitee.com/piperliu/picture/raw/master/2021-2-17/1613566736150-zen.gif) 可以使用 `Ctrl + Shift + P` 輸入 `Zen` 來呼叫相關命令。此外,我將其快捷鍵改為了(在左下角的小齒輪的 Keymaps 中更改) `Ctrl + Alt + Z` ,如上圖,甚是舒爽。 ### group size 一塊小小的顯示器,還要被兩個或以上的 group 平分割槽域,我們的程式碼閱讀螢幕實在是太『窄』了。 ![group size](https://gitee.com/piperliu/picture/raw/master/2021-2-17/1613566736145-group_size.gif) 如上圖,我們可以用 `Toggle Editor Group Sizes` 來切換閱讀模式,讓其更寬一些。我設定了其快捷鍵為 `Ctrl + Alt + G` 。 ### Ctrl + i / Ctrl + / 如下圖,使用 `Ctrl + i` 可以呼叫程式碼提示,而 `Ctrl + /` 可以將選中的文字註釋掉,或者取消註釋。 ![Ctrl + i 程式碼提示](https://gitee.com/piperliu/picture/raw/master/2021-2-17/1613566736139-ctrl+i.gif) ## 3 個有趣又通用的小眾外掛 ### 外掛:vscode-input-sequence 一個 vscode-input-sequence 解決了我們輸入序列資料的煩惱。 ![vscode-input-sequence](https://gitee.com/piperliu/picture/raw/master/2021-2-17/1613566736135-sequence.gif) 如上圖,配合多游標,我們可以輸入一個序列的資料,並且可以決定序列的遞進規則、遞進步長、佔位、進位制等。 ### 外掛:Relative Path ![Relative Path](https://gitee.com/piperliu/picture/raw/master/2021-2-17/1613566736181-vscode_relative_path.png) 我很喜歡 Relative Path ,如下圖,輸入 `Ctrl + Shift + H` 便自動補全檔案的相對路徑。 ![Relative Path 補全了圖片的相對路徑](https://gitee.com/piperliu/picture/raw/master/2021-2-17/1613566736131-relative_path.gif) ### 外掛:Auto Markdown TOC ![Auto Markdown TOC from Hunter Tran](https://gitee.com/piperliu/picture/raw/master/2021-2-17/1613566736186-vscode_toc.png) 有很多關於 markdown 自動生成目錄的外掛,我覺得對中文最友善的,應該是 Hunter Tran 的 Auto Markdown TOC 了。 ![使用 Auto Markdown TOC 自動生成目錄](https://gitee.com/piperliu/picture/raw/master/2021-2-17/1613566736125-markdown_toc.gif) 如上,使用 Auto Markdown TOC 自動生成目錄(同樣地,在`Ctrl + Shift + P` 中呼叫相關命令)。 ## 5+ 的 vim + vscode 操作 ### Vim 外掛安裝 ![下載 Vim 外掛](https://gitee.com/piperliu/picture/raw/master/2021-2-4/1612415283716-image.png) 如上圖,下載 Vim 外掛。這裡需要注意,強烈推薦 **不使用Vim自帶的組合快捷鍵 Ctrl ,因為其會覆蓋掉 VS Code 本身的快捷鍵以及其他外掛的快捷鍵** 。 ![取消勾選 useCtrlKeys 設定](https://gitee.com/piperliu/picture/raw/master/2021-2-4/1612415858401-image.png) 如上圖,可以在 Settings 中搜索 useCtrlKeys 配置,並取消勾選。 ![vim scroll](https://gitee.com/piperliu/picture/raw/master/2021-2-4/1612451335227-image.png) 值得注意的是,我們依然可以使用 `Ctrl + u` 和 `Ctrl + d` 實現上下滾動頁面,滾動行數預設為半頁大小。 ### Vim 基礎 這裡要介紹的是 Vim 基礎中的基礎,但還是沒有 Vim 基礎的初學者也可以去 B 站搜尋 Vim 相關的教程再鞏固一下(我之後也可能錄視訊)。 ![vim 基礎](https://gitee.com/piperliu/picture/raw/master/2021-2-4/1612452825436-vim_1.gif) 如上圖: - Vim 中預設是 normal 模式,此時敲擊字母是在敲擊命令,而非編輯文字;輸入 `i` 進入 insert 模式,此時敲擊字母才是輸入字母 - insert 模式下,退出鍵 `Escape` 回到 normal 模式 - normal 模式下,`hjkl`分別是`左下上右` ![vim 中 a i o 操作](https://gitee.com/piperliu/picture/raw/master/2021-2-4/1612452825440-vim_2.gif) 如上: - normal 模式下,`Shift + a`(以後記作`A`)移到行末,並轉為 insert 模式;`I`是行前 - normal 模式下,`o`是下插一行,並跳轉 ![vim 中 w b 操作](https://gitee.com/piperliu/picture/raw/master/2021-2-4/1612452825445-vim_3.gif) 如上: - normal 模式下,`w`是向前移動一個單詞,`b`則是向後移動一個單詞 ![vim 中的複製貼上](https://gitee.com/piperliu/picture/raw/master/2021-2-4/1612452825450-vim_4.gif) 如上: - normal 模式下,`d`是開啟剪下模式,因此,`dw`則是剪下一個單詞;`dd`是剪下一行 - normal 模式下,`y`則是複製,而`p`是貼上 ![vim 中 s x](https://gitee.com/piperliu/picture/raw/master/2021-2-5/1612485089566-vim_sx.gif) 如上: - normal 模式下,`s`是刪除當前游標所指並轉為 insert 模式,`x`是刪除並且不轉換 ![vim 中 g](https://gitee.com/piperliu/picture/raw/master/2021-2-4/1612452825456-vim_5.gif) 如上: - normal 模式下,`15gg`是跳轉到`15`行 ![vim 中的 visual](https://gitee.com/piperliu/picture/raw/master/2021-2-4/1612452825459-vim_v.gif) 如上: - normal 模式下,是開啟選中 visual 模式 ### VS Code 中常用 Vim 組合邏輯 Vim 愛好者常說:Vim是一種邏輯,而非死記硬背快捷鍵(我還算不上 Vim 愛好者,這句話不是我說的,是 Linux 社群的一些朋友們說的)。 我對於 Vim 『邏輯』的理解,大概如下: - 大小寫是一種相反的邏輯 - VS Code 中一些 Ctrl 與滾動 - `i(`意味著`在括號裡` 展開的解釋如下文。 #### 大小寫是一種相反的邏輯 ```cpp int foo_xyz = 1; ^ ``` 我們知道,當游標在`_`處時,想要刪除 `_xyz` ,那麼就要首先 **找到最近的z的位置,然後刪除這個區間內的東西。** 在 vim 中,則是輸入 `dfz` 來表達, `fz` 則是 **找到 z 的意思** 。 ![vim 中 f 與 F](https://gitee.com/piperliu/picture/raw/master/2021-2-4/1612453901034-vim_f.gif) 那麼,向前查詢是什麼呢?答案是大寫的 `F` ,具體例子如上圖。 ![vim 中 o 與 O](https://gitee.com/piperliu/picture/raw/master/2021-2-4/1612453901037-vim_o.gif) 如上,用 `o` 可以很輕鬆地向下插入一行,而用 `O` 則是向上插入一行。 #### VS Code 中一些 Ctrl 與滾動 ![vim 中的 []](https://gitee.com/piperliu/picture/raw/master/2021-2-5/1612454601296-vim_ctrl_1.gif) 如上,使用 `Shift + []` 是以「程式碼塊」為單位跳行。 ![vim 中 Ctrl + []](https://gitee.com/piperliu/picture/raw/master/2021-2-5/1612454601305-vim_ctrl_2.gif) 如上,`Ctrl + []` 實現縮排的調整。這裡結合了 visual 模式。 ![vim 中滾動](https://gitee.com/piperliu/picture/raw/master/2021-2-5/1612454601318-vim_ctrl_3.gif) 如上,`[[` 跳到檔案首行,而 `]]` 則是行末。 ![vim 中 H L M z](https://gitee.com/piperliu/picture/raw/master/2021-2-5/1612454601329-vim_ctrl_4.gif) 如上,`H` 是到螢幕的頂部,`L` 是螢幕的底部,`M` 是螢幕的中央;而 `zz` 是『將游標放到螢幕中央,且不移動游標的位置(滾動螢幕)』,`zt` 是將游標放到頂部,`zb` 是底部。 #### i( 意味著在括號裡 ```python print("delete me") ``` 如上,當我們想清空 `print()` 函式括號內的東西,我們「有邏輯地」告訴 vim 即可。把游標移到括號內,輸入 `ci(` 或 `ci)`,其中 `i(` 是『在括號內』。 ![一種邏輯](https://gitee.com/piperliu/picture/raw/master/2021-2-5/1612455215572-17.gif) 如上,不僅是 `c` 操作,也不僅僅是 `()` ,對於 `""`、`[]` 等都適用。 此外,還有很多邏輯可以組合,比如:**j是向下跳轉一行,那麼 10j 就是向下10行。** 此外,`/` 查詢功能在 vs code 中同樣適用。但是,我更喜歡 vs code 自帶的 `ctrl + f` 查詢。 **** > 你好,我是小拍,985大學管理科學與工程專業在讀研究生、技術愛好者與自學者,同時也是 **『持續改善 · よい品よい考”』** 思想的支持者和踐行者。轉載&合作&學習事宜,歡迎加我微信 PiperLHJ