」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > JS:VSCode 技巧

JS:VSCode 技巧

發佈於2024-07-30
瀏覽:523

JS: VSCode Tips

Discovering little tricks like that can really streamline your workflow. Here are some more tips and tricks for making the most out of your coding experience in VS Code, especially when you're working with Node.js:

1. Integrated Terminal

You already found out about using the terminal in VS Code, but did you know you can split terminals? Use Ctrl Shift 5 (or Cmd Shift 5 on Mac) to open a split terminal. This is great for running a server in one terminal while executing scripts or running tests in another.

2. Debugger

VS Code has a built-in debugger that works seamlessly with Node.js. Set breakpoints in your code by clicking in the gutter next to the line numbers, and then press F5 to start the debugger. You can step through your code, inspect variables, and view call stacks, making it much easier to find and fix issues.

3. Live Server Extension

If you're working on web projects, the Live Server extension is a game-changer. It automatically refreshes your browser whenever you save changes to your HTML, CSS, or JavaScript files. Just install the extension, right-click your index.html file, and select "Open with Live Server."

4. Emmet Abbreviations

For HTML and CSS, Emmet abbreviations can save you tons of time. For example, typing ul>li*5 and pressing Tab will generate an unordered list with five list items. It's like magic!

5. Prettier Extension

The Prettier code formatter helps keep your code consistent and clean. Install the Prettier extension and then format your code with Alt Shift F (or Option Shift F on Mac). You can also configure it to format your code on save.

6. Code Snippets

Custom snippets can speed up your coding. Go to File > Preferences > User Snippets and create a new snippet file for JavaScript. For example, you can create a snippet for a console log:

"log": {
    "prefix": "clg",
    "body": ["console.log('$1');"],
    "description": "Log output to console"
}

Now, typing clg followed by Tab will expand to console.log();.

7. IntelliSense

VS Code’s IntelliSense offers intelligent code completion, parameter info, and member lists. For JavaScript, it’s particularly useful when dealing with complex objects or unfamiliar libraries. You can trigger it manually by pressing Ctrl Space.

8. Extensions for Node.js

There are a few must-have extensions for Node.js developers:

  • Node.js Extension Pack: A collection of useful extensions like npm, ESLint, and Node.js Modules Intellisense.
  • Nodemon: This extension allows you to use the Nodemon utility which automatically restarts your Node.js application when file changes in the directory are detected.

9. Source Control Integration

VS Code has Git integration built-in. You can manage your repositories, stage changes, make commits, and even resolve merge conflicts directly from the editor. Use the Source Control panel on the left sidebar for all your Git operations.

10. Command Palette

The Command Palette (Ctrl Shift P or Cmd Shift P on Mac) is the gateway to all VS Code commands. You can quickly search and execute commands, open files, and run extensions without leaving your keyboard.

11. Multi-Cursor Editing

Place multiple cursors in your code by holding Alt (or Option on Mac) and clicking where you want to add a cursor. You can also use Ctrl Alt Down (or Cmd Option Down on Mac) to add a cursor below the current line. This is super handy for making the same edit in multiple places.

12. Explorer Shortcuts

Navigate your files quickly using the explorer panel on the left. Ctrl P (or Cmd P on Mac) brings up the quick open file menu, allowing you to jump to any file in your project with just a few keystrokes.

13. Task Runner

You can define tasks in VS Code to run scripts or commands with ease. Create a tasks.json file in the .vscode folder of your project and configure tasks like building your project or running tests.

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "npm run build",
            "problemMatcher": []
        }
    ]
}

Now you can run your build task from the Command Palette or the terminal.

14. Code Lens

For JavaScript, Code Lens shows inline references to functions and variables, making it easier to see where they are used. Enable it in settings under Editor: Code Lens.

15. Bracket Pair Colorization

To make nested code easier to read, enable bracket pair colorization. This can be done by adding "editor.bracketPairColorization.enabled": true to your settings.json file.

16. Workspace Settings

VS Code allows you to configure settings specific to your project by adding a .vscode folder with settings.json inside your project directory. This is useful for maintaining consistent settings across team members.

{
    "editor.tabSize": 2,
    "files.exclude": {
        "**/.git": true,
        "**/.DS_Store": true
    }
}

17. Remote Development

VS Code's Remote Development extensions let you work with code on remote machines or in containers. You can use the Remote - SSH extension to connect to a remote server or Remote - Containers to develop inside Docker containers. This is excellent for maintaining a consistent dev environment.

18. Settings Sync

You can sync your settings, extensions, and keyboard shortcuts across different devices using Settings Sync. Enable it from the gear icon in the bottom left, then "Turn on Settings Sync..." and sign in with your GitHub or Microsoft account.

19. Keyboard Shortcuts

Mastering keyboard shortcuts can significantly boost your productivity. Here are a few more handy ones:

  • Ctrl B (or Cmd B on Mac) to toggle the sidebar.
  • Ctrl (or Cmd on Mac) to open the integrated terminal.
  • Ctrl K, Ctrl S (or Cmd K, Cmd S on Mac) to open the keyboard shortcuts editor.

20. Extensions for Productivity

Explore these extensions to further enhance your productivity:

  • Bracket Pair Colorizer 2: Colors matching brackets to make nested code more readable.
  • Path Intellisense: Autocompletes filenames when you start typing a path in your import statements.
  • Todo Tree: Tracks TODO comments in your code and lists them in the explorer.

21. REST Client

The REST Client extension allows you to make HTTP requests directly from VS Code and view the responses. Create a .http or .rest file and write your requests:

GET https://api.github.com/users/octocat

Run the request by clicking "Send Request" above the request line.

22. Polacode

Polacode is an extension that lets you create beautiful code snapshots. You can use it to generate images of your code for documentation or sharing. Simply select the code, right-click, and select "Polacode: Open" to capture the snapshot.

23. CSS Peek

For front-end developers, CSS Peek allows you to view CSS definitions directly in your HTML file by hovering over class names or IDs.

24. GitLens

GitLens supercharges the built-in Git capabilities by providing insights into your repository. It shows who changed a line of code and when, helps with navigating through commit history, and even offers inline blame annotations.

25. Snippets and Extensions for Frameworks

Depending on your tech stack, there are specific snippets and extensions you might find useful:

  • React: The ES7 React/Redux/React-Native snippets extension offers shorthand snippets for creating components, hooks, and more.
  • Vue.js: Vetur is the go-to extension for Vue.js development, providing syntax highlighting, snippets, and more.
  • Angular: Angular Essentials pack includes Angular Language Service and other helpful tools.

26. Jupyter Notebooks

VS Code supports Jupyter Notebooks, making it an excellent tool for data science and machine learning projects. Install the Jupyter extension to create, edit, and run Jupyter notebooks directly in the editor.

27. Docker Integration

If you're working with Docker, the Docker extension is invaluable. It allows you to build, manage, and deploy containerized applications directly from VS Code. You can view containers, images, and registries all from the sidebar.

28. Markdown Preview

VS Code has built-in support for Markdown. You can preview Markdown files by opening the file and pressing Ctrl Shift V (or Cmd Shift V on Mac). This is great for writing documentation or README files.

29. Workspace Trust

VS Code introduces Workspace Trust, which allows you to control the level of trust you assign to the files within a workspace. This is especially useful when working with code from unknown sources, ensuring your environment remains secure.

30. Zen Mode

For those times when you need to focus, Zen Mode provides a distraction-free coding environment. Enter Zen Mode by pressing Ctrl K Z (or Cmd K Z on Mac). This hides all toolbars and panels, leaving you with just your code.

31. Code Navigation

Efficiently navigate your codebase with features like:

  • Go to Definition: F12 or right-click > Go to Definition.
  • Peek Definition: Alt F12 (or Option F12 on Mac) to view the definition inline.
  • Go to Symbol: Ctrl Shift O (or Cmd Shift O on Mac) to jump to symbols in the current file.

32. Project Manager

The Project Manager extension helps you manage and switch between multiple projects. It allows you to save your current project, list your recent projects, and quickly switch between them.

33. Code Spell Checker

Avoid embarrassing typos in your code and comments with the Code Spell Checker extension. It highlights spelling errors and offers suggestions, making your documentation and variable names more professional.

34. Git Graph

Visualize your repository’s commit history with the Git Graph extension. It provides a graphical representation of your branches, commits, and merges, making it easier to understand the flow of your project.

35. Version Control Diffing

VS Code has powerful diffing capabilities. You can compare files or view changes by selecting a file and pressing Ctrl D (or Cmd D on Mac). This is useful for reviewing changes before committing or resolving merge conflicts.

36. Code Metrics

The CodeMetrics extension provides complexity metrics for your code, helping you identify areas that might need refactoring. It shows the cognitive complexity of functions and classes, making it easier to maintain clean and efficient code.

37. Keybinding Customization

You can customize your keybindings to suit your workflow. Open the keybindings editor with Ctrl K Ctrl S (or Cmd K Cmd S on Mac) and modify existing shortcuts or create new ones.

38. File and Code Navigation

VS Code offers powerful file and code navigation capabilities:

  • Quick Open: Use Ctrl P (or Cmd P on Mac) to quickly open files by typing part of their name.
  • Navigate Back and Forward: Use Ctrl - and Ctrl Shift - (or Cmd - and Cmd Shift - on Mac) to move through your cursor history.
  • Breadcrumbs: Enable breadcrumbs to show the current location and hierarchy of your file at the top of the editor.

39. Auto Close Tag and Auto Rename Tag

These extensions are particularly useful for HTML and JSX development. Auto Close Tag automatically closes tags as you type, while Auto Rename Tag synchronizes changes between the opening and closing tags.

40. Project-Specific Extensions

Install extensions tailored to your specific project needs. For example, if you're working on a Python project, the Python extension provides a comprehensive set of tools including IntelliSense, linting, and debugging. For a Java project, consider the Java Extension Pack.

These tips should keep you busy for a while and supercharge your VS Code setup. Enjoy exploring all these features and happy coding, dude!

版本聲明 本文轉載於:https://dev.to/dariansdev/js-40-vscode-tips-2jmg?1如有侵犯,請聯絡[email protected]刪除
最新教學 更多>
  • 大批
    大批
    方法是可以在物件上呼叫的 fns 數組是對象,因此它們在 JS 中也有方法。 slice(begin):將陣列的一部分提取到新數組中,而不改變原始數組。 let arr = ['a','b','c','d','e']; // Usecase: Extract till index ...
    程式設計 發佈於2024-12-20
  • 儘管程式碼有效,為什麼 POST 請求無法擷取 PHP 中的輸入?
    儘管程式碼有效,為什麼 POST 請求無法擷取 PHP 中的輸入?
    解決PHP 中的POST 請求故障在提供的程式碼片段:action=''而非:action="<?php echo $_SERVER['PHP_SELF'];?>";?>"檢查$_POST陣列:表單提交後使用 var_dump 檢查 $_POST 陣列的內容...
    程式設計 發佈於2024-12-20
  • 如何在 PHP 中組合兩個關聯數組,同時保留唯一 ID 並處理重複名稱?
    如何在 PHP 中組合兩個關聯數組,同時保留唯一 ID 並處理重複名稱?
    在 PHP 中組合關聯數組在 PHP 中,將兩個關聯數組組合成一個數組是常見任務。考慮以下請求:問題描述:提供的代碼定義了兩個關聯數組,$array1和$array2。目標是建立一個新陣列 $array3,它合併兩個陣列中的所有鍵值對。 此外,提供的陣列具有唯一的 ID,而名稱可能重疊。要求是建構一...
    程式設計 發佈於2024-12-20
  • 插入資料時如何修復「常規錯誤:2006 MySQL 伺服器已消失」?
    插入資料時如何修復「常規錯誤:2006 MySQL 伺服器已消失」?
    插入記錄時如何解決「一般錯誤:2006 MySQL 伺服器已消失」介紹:將資料插入MySQL 資料庫有時會導致錯誤「一般錯誤:2006 MySQL 伺服器已消失」。當與伺服器的連線遺失時會出現此錯誤,通常是由於 MySQL 配置中的兩個變數之一所致。 解決方案:解決此錯誤的關鍵是調整wait_tim...
    程式設計 發佈於2024-12-20
  • 在 Go 中使用 WebSocket 進行即時通信
    在 Go 中使用 WebSocket 進行即時通信
    构建需要实时更新的应用程序(例如聊天应用程序、实时通知或协作工具)需要一种比传统 HTTP 更快、更具交互性的通信方法。这就是 WebSockets 发挥作用的地方!今天,我们将探讨如何在 Go 中使用 WebSocket,以便您可以向应用程序添加实时功能。 在这篇文章中,我们将介绍: WebSoc...
    程式設計 發佈於2024-12-20
  • Bootstrap 4 Beta 中的列偏移發生了什麼事?
    Bootstrap 4 Beta 中的列偏移發生了什麼事?
    Bootstrap 4 Beta:列偏移的刪除和恢復Bootstrap 4 在其Beta 1 版本中引入了重大更改柱子偏移了。然而,隨著 Beta 2 的後續發布,這些變化已經逆轉。 從 offset-md-* 到 ml-auto在 Bootstrap 4 Beta 1 中, offset-md-*...
    程式設計 發佈於2024-12-20
  • 使用“list.List”是建立帶有字串鍵和列表值的 Go 映射的最佳方法嗎?
    使用“list.List”是建立帶有字串鍵和列表值的 Go 映射的最佳方法嗎?
    建立字串到清單的對應問題:您想要建立一個有字串型別鍵的映射和列表類型的值。以下程式碼片段是否是正確的方法:package main import ( "fmt" "container/list" ) func main() { x :=...
    程式設計 發佈於2024-12-19
  • 使用 html css 和 javascript 幻覺的 Tic-Tac-Toe 遊戲 https://www.instagram.com/webstreet_code/
    使用 html css 和 javascript 幻覺的 Tic-Tac-Toe 遊戲 https://www.instagram.com/webstreet_code/
    在 Instagram 上關注我們:https://www.instagram.com/webstreet_code/ ? ✨ 有玻璃效果的井字遊戲! ✨? 我剛剛使用 HTML、CSS 和 JavaScript 構建了一款經典的 Tic-Tac-Toe 遊戲,具有時尚的玻璃態設計。觀看視頻,看看...
    程式設計 發佈於2024-12-19
  • TB 級資料庫的 MySQL 與 NoSQL:聚集索引何時是正確的解決方案?
    TB 級資料庫的 MySQL 與 NoSQL:聚集索引何時是正確的解決方案?
    MySQL:探索資料庫設計迷宮優化大型資料庫時,必須考慮資料庫設計策略以提高效能。在給定的場景中,包含執行緒的 TB 級資料庫由於其龐大的規模而面臨效能挑戰。本文探討了 MySQL 和 NoSQL 之間的選擇,重點介紹了 MySQL 的 innodb 引擎及其聚集索引的優點。 了解 MySQL 的 ...
    程式設計 發佈於2024-12-19
  • 為什麼我的 Spring Boot 應用程式不自動建立資料庫架構?
    為什麼我的 Spring Boot 應用程式不自動建立資料庫架構?
    在 Spring Boot 中自動建立資料庫架構啟動 Spring Boot 應用程式時,可能會遇到自動建立資料庫架構的問題。以下故障排除步驟旨在解決此問題:1.實體類別包:確保實體類別位於使用@EnableAutoConfiguration註解的類別的同一個套件或子包中。否則,Spring 將不會...
    程式設計 發佈於2024-12-18
  • CSS3 轉場是否提供事件來偵測起點和終點?
    CSS3 轉場是否提供事件來偵測起點和終點?
    了解 CSS3 過渡事件CSS3 過渡允許在 Web 元素上實現流暢的動畫和視覺效果。為了增強使用者體驗並使操作與這些轉換同步,監控其進度非常重要。本文解決了 CSS3 是否提供事件來檢查過渡何時開始或結束的問題。 W3C CSS 過渡草案W3C CSS 過渡草案規定CSS 轉換會觸發對應的 DOM...
    程式設計 發佈於2024-12-18
  • Java 中可以手動釋放記憶體嗎?
    Java 中可以手動釋放記憶體嗎?
    Java 中的手動內存釋放與垃圾回收與C 不同,Java 採用託管內存框架來處理內存分配和釋放由垃圾收集器(GC) 自動執行。這種自動化方法可以提高記憶體利用率並防止困擾 C 程式的記憶體洩漏。 Java 中可以手動釋放記憶體嗎? 由於 Java 的記憶體管理是由GC,它沒有提供像 C 中的 fre...
    程式設計 發佈於2024-12-18
  • Java 1.6 中如何可靠地確定檔案是否為符號連結?
    Java 1.6 中如何可靠地確定檔案是否為符號連結?
    在 Java 1.6 中驗證符號連結確定符號連結的存在對於各種文件處理操作至關重要。在 Java 中,識別符號連結時需要考慮一些潛在問題,特別是在目錄遍歷的上下文中。 檢查符號連結的常見方法是比較文件的絕對路徑和規範路徑。規範路徑表示檔案的標準化路徑,而絕對路徑可能包括符號連結。傳統上,概念是如果這...
    程式設計 發佈於2024-12-17
  • 如何使背景顏色透明,同時保持文字不透明?
    如何使背景顏色透明,同時保持文字不透明?
    背景顏色的不透明度而不影響文本在Web 開發領域,實現透明度通常對於增強視覺吸引力和網站元素的功能。常見的要求是對 div 背景套用透明度,同時保留所包含文字的不透明度。這可能會帶來挑戰,特別是在確保跨瀏覽器相容性方面。 rgba 解決方案最有效且廣泛支持的解決方案是利用「RGBA」(紅、綠、藍、A...
    程式設計 發佈於2024-12-17
  • PHP 字串比較:`==`、`===` 或 `strcmp()` – 您應該使用哪個運算子?
    PHP 字串比較:`==`、`===` 或 `strcmp()` – 您應該使用哪個運算子?
    PHP 中的字串比較:'=='、'===' 或 'strcmp()'? PHP 中的字串比較PHP 可以使用不同的運算子來完成,例如「==」、「===」或「strcmp()」函數。此比較涉及檢查兩個字串是否相等。 '==' 與'...
    程式設計 發佈於2024-12-17

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3