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

JS:VSCode 技巧

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

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]刪除
最新教學 更多>
  • 您如何在Laravel Blade模板中定義變量?
    您如何在Laravel Blade模板中定義變量?
    在Laravel Blade模板中使用Elegance 在blade模板中如何分配變量對於存儲以後使用的數據至關重要。在使用“ {{}}”分配變量的同時,它可能並不總是最優雅的解決方案。 幸運的是,Blade通過@php Directive提供了更優雅的方法: $ old_section =...
    程式設計 發佈於2025-04-01
  • 如何使用不同數量列的聯合數據庫表?
    如何使用不同數量列的聯合數據庫表?
    合併列數不同的表 當嘗試合併列數不同的數據庫表時,可能會遇到挑戰。一種直接的方法是在列數較少的表中,為缺失的列追加空值。 例如,考慮兩個表,表 A 和表 B,其中表 A 的列數多於表 B。為了合併這些表,同時處理表 B 中缺失的列,請按照以下步驟操作: 確定表 B 中缺失的列,並將它們添加到表的...
    程式設計 發佈於2025-04-01
  • 在GO中構造SQL查詢時,如何安全地加入文本和值?
    在GO中構造SQL查詢時,如何安全地加入文本和值?
    在go中構造文本sql查詢時,在go sql queries 中,在使用conting and contement和contement consem per時,尤其是在使用integer per當per當per時,per per per當per. [&​​&&&&&&&&&&&&&&&默元組方法在...
    程式設計 發佈於2025-04-01
  • Java是否允許多種返回類型:仔細研究通用方法?
    Java是否允許多種返回類型:仔細研究通用方法?
    在Java中的多個返回類型:一種誤解類型:在Java編程中揭示,在Java編程中,Peculiar方法簽名可能會出現,可能會出現,使開發人員陷入困境,使開發人員陷入困境。 getResult(string s); ,其中foo是自定義類。該方法聲明似乎擁有兩種返回類型:列表和E。但這確實是如此嗎...
    程式設計 發佈於2025-04-01
  • 如何從PHP中的Unicode字符串中有效地產生對URL友好的sl。
    如何從PHP中的Unicode字符串中有效地產生對URL友好的sl。
    為有效的slug生成首先,該函數用指定的分隔符替換所有非字母或數字字符。此步驟可確保slug遵守URL慣例。隨後,它採用ICONV函數將文本簡化為us-ascii兼容格式,從而允許更廣泛的字符集合兼容性。 接下來,該函數使用正則表達式刪除了不需要的字符,例如特殊字符和空格。此步驟可確保slug僅包...
    程式設計 發佈於2025-04-01
  • 如何同步迭代並從PHP中的兩個等級陣列打印值?
    如何同步迭代並從PHP中的兩個等級陣列打印值?
    同步的迭代和打印值來自相同大小的兩個數組使用兩個數組相等大小的selectbox時,一個包含country代碼的數組,另一個包含鄉村代碼,另一個包含其相應名稱的數組,可能會因不當提供了exply for for for the uncore for the forsion for for ytry...
    程式設計 發佈於2025-04-01
  • 如何使用node-mysql在單個查詢中執行多個SQL語句?
    如何使用node-mysql在單個查詢中執行多個SQL語句?
    Multi-Statement Query Support in Node-MySQLIn Node.js, the question arises when executing multiple SQL statements in a single query using the node-mys...
    程式設計 發佈於2025-04-01
  • 如何解決由於Android的內容安全策略而拒絕加載腳本... \”錯誤?
    如何解決由於Android的內容安全策略而拒絕加載腳本... \”錯誤?
    Unveiling the Mystery: Content Security Policy Directive ErrorsEncountering the enigmatic error "Refused to load the script..." when deployi...
    程式設計 發佈於2025-04-01
  • 如何使用Regex在PHP中有效地提取括號內的文本
    如何使用Regex在PHP中有效地提取括號內的文本
    php:在括號內提取文本在處理括號內的文本時,找到最有效的解決方案是必不可少的。一種方法是利用PHP的字符串操作函數,如下所示: 作為替代 $ text ='忽略除此之外的一切(text)'; preg_match('#((。 &&& [Regex使用模式來搜索特...
    程式設計 發佈於2025-04-01
  • 如何實時捕獲和流媒體以進行聊天機器人命令執行?
    如何實時捕獲和流媒體以進行聊天機器人命令執行?
    在開發能夠執行命令的chatbots的領域中,實時從命令執行實時捕獲Stdout,一個常見的需求是能夠檢索和顯示標準輸出(stdout)在cath cath cant cant cant cant cant cant cant cant interfaces in Chate cant inter...
    程式設計 發佈於2025-04-01
  • 在Ubuntu/linux上安裝mysql-python時,如何修復\“ mysql_config \”錯誤?
    在Ubuntu/linux上安裝mysql-python時,如何修復\“ mysql_config \”錯誤?
    mysql-python安裝錯誤:“ mysql_config找不到”“ 由於缺少MySQL開發庫而出現此錯誤。解決此問題,建議在Ubuntu上使用該分發的存儲庫。使用以下命令安裝Python-MysqldB: sudo apt-get安裝python-mysqldb sudo pip in...
    程式設計 發佈於2025-04-01
  • 為什麼我會收到MySQL錯誤#1089:錯誤的前綴密鑰?
    為什麼我會收到MySQL錯誤#1089:錯誤的前綴密鑰?
    mySQL錯誤#1089:錯誤的前綴鍵錯誤descript [#1089-不正確的前綴鍵在嘗試在表中創建一個prefix鍵時會出現。前綴鍵旨在索引字符串列的特定前綴長度長度,可以更快地搜索這些前綴。 了解prefix keys `這將在整個Movie_ID列上創建標準主鍵。主密鑰對於唯一識...
    程式設計 發佈於2025-04-01
  • 如何有效地選擇熊貓數據框中的列?
    如何有效地選擇熊貓數據框中的列?
    在處理數據操作任務時,在Pandas DataFrames 中選擇列時,選擇特定列的必要條件是必要的。在Pandas中,選擇列的各種選項。 選項1:使用列名 如果已知列索引,請使用ILOC函數選擇它們。請注意,python索引基於零。 df1 = df.iloc [:,0:2]#使用索引0和1 ...
    程式設計 發佈於2025-04-01
  • 我可以將加密從McRypt遷移到OpenSSL,並使用OpenSSL遷移MCRYPT加密數據?
    我可以將加密從McRypt遷移到OpenSSL,並使用OpenSSL遷移MCRYPT加密數據?
    將我的加密庫從mcrypt升級到openssl 問題:是否可以將我的加密庫從McRypt升級到OpenSSL?如果是這樣,如何? 答案:是的,可以將您的Encryption庫從McRypt升級到OpenSSL。 可以使用openssl。 附加說明: [openssl_decrypt()函數要求...
    程式設計 發佈於2025-04-01
  • 為什麼PHP的DateTime :: Modify('+1個月')會產生意外的結果?
    為什麼PHP的DateTime :: Modify('+1個月')會產生意外的結果?
    使用php dateTime修改月份:發現預期的行為在使用PHP的DateTime類時,添加或減去幾個月可能並不總是會產生預期的結果。正如文檔所警告的那樣,“當心”這些操作的“不像看起來那樣直觀。 考慮文檔中給出的示例:這是內部發生的事情: 現在在3月3日添加另一個月,因為2月在2001年只有2...
    程式設計 發佈於2025-04-01

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

Copyright© 2022 湘ICP备2022001581号-3