首頁 > web前端 > js教程 > 主體

JS:VSCode 技巧

PHPz
發布: 2024-07-30 09:29:49
原創
865 人瀏覽過

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 模式提供了一個無幹擾的編碼環境。按 Ctrl + K Z(或在 Mac 上按 Cmd + K Z)進入 Zen 模式。這會隱藏所有工具列和麵板,只留下您的程式碼。

31. 程式碼導航

使用以下功能有效率地瀏覽您的程式碼庫:

  • 轉到定義:F12 或右鍵>轉到定義。
  • 查看定義:Alt + F12(或 Mac 上的 Option + F12)查看內聯定義。
  • 前往符號:Ctrl + Shift + O(或 Mac 上的 Cmd + Shift + O)跳到目前檔案中的符號。

32. 專案經理

專案管理器擴充功能可協助您管理和在多個專案之間切換。它允許您保存當前項目、列出最近的項目並在它們之間快速切換。

33. 代碼拼字檢查器

使用程式碼拼字檢查器擴充功能避免程式碼和註解中出現令人尷尬的拼字錯誤。它會突出顯示拼寫錯誤並提供建議,使您的文件和變數名稱更加專業。

34. Git 圖

使用 Git Graph 擴充功能可視化儲存庫的提交歷史記錄。它提供了分支、提交和合併的圖形表示,使您更容易理解專案的流程。

35. 版本控制比較

VS Code 具有強大的 diff 功能。您可以透過選擇檔案並按 Ctrl + D(或在 Mac 上按 Cmd + D)來比較檔案或檢視變更。這對於在提交或解決合併衝突之前檢查更改非常有用。

36. 程式碼指標

CodeMetrics 擴充功能為您的程式碼提供複雜性指標,幫助您識別可能需要重構的區域。它顯示了函數和類別的認知複雜性,使維護乾淨且有效率的程式碼變得更加容易。

37. 按鍵綁定自訂

您可以自訂按鍵綁定以適合您的工作流程。使用 Ctrl + K Ctrl + S(或 Mac 上的 Cmd + K Cmd + S)開啟鍵綁定編輯器,然後修改現有捷徑或建立新捷徑。

38. 文件和程式碼導航

VS Code 提供強大的檔案和程式碼導航功能:

  • 快速開啟:使用 Ctrl + P(或 Mac 上的 Cmd + P)透過鍵入檔案名稱的一部分來快速開啟檔案。
  • 向後和向前導航:使用 Ctrl + - 和 Ctrl + Shift + -(或在 Mac 上使用 Cmd + - 和 Cmd + Shift + -)在遊標歷史記錄中移動。
  • 麵包屑:啟用麵包屑以在編輯器頂部顯示檔案的目前位置和層次結構。

39. 自動關閉標籤和自動重新命名標籤

這些擴充功能對於 HTML 和 JSX 開發特別有用。自動關閉標籤會在您鍵入時自動關閉標籤,而自動重新命名標籤會同步開始標籤和結束標籤之間的變更。

40. 特定於項目的擴充

安裝適合您特定專案需求的擴充功能。例如,如果您正在處理 Python 項目,Python 擴充功能提供了一套全面的工具,包括 IntelliSense、linting 和偵錯。對於 Java 項目,請考慮 Java 擴充包。

這些技巧應該會讓您忙碌一段時間並增強您的 VS Code 設定。享受探索所有這些功能并快樂編碼,夥計!

以上是JS:VSCode 技巧的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:dev.to
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板