”工欲善其事,必先利其器。“—孔子《论语.录灵公》
首页 > 编程 > JS:VSCode 技巧

JS:VSCode 技巧

发布于2024-07-30
浏览:376

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]删除
最新教程 更多>
  • 如何在 Node.js 中将 JSON 文件读入服务器内存?
    如何在 Node.js 中将 JSON 文件读入服务器内存?
    在 Node.js 中将 JSON 文件读入服务器内存为了增强服务器端代码性能,您可能需要读取 JSON 对象从文件到内存以便快速访问。以下是在 Node.js 中实现此目的的方法:同步方法:对于同步文件读取,请利用 fs(文件系统)中的 readFileSync() 方法模块。此方法将文件内容作为...
    编程 发布于2024-11-03
  • 人工智能可以提供帮助
    人工智能可以提供帮助
    我刚刚意识到人工智能对开发人员有很大帮助。它不会很快接管我们的工作,因为它仍然很愚蠢,但是,如果你像我一样正在学习编程,可以用作一个很好的工具。 我要求 ChatGpt 为我准备 50 个项目来帮助我掌握 JavaScript,它带来了令人惊叹的项目,我相信当我完成这些项目时,这些项目将使我成为 J...
    编程 发布于2024-11-03
  • Shadcn UI 套件 - 管理仪表板和网站模板
    Shadcn UI 套件 - 管理仪表板和网站模板
    Shadcn UI 套件是预先设计的多功能仪表板、网站模板和组件的综合集合。它超越了 Shadcn 的标准产品,为那些不仅仅需要基础知识的人提供更先进的设计和功能。 独特的仪表板模板 Shadcn UI Kit 提供了各种精心制作的仪表板模板。目前,有 7 个仪表板模板可用,随着时间...
    编程 发布于2024-11-03
  • 如何使用正则表达式捕获多行文本块?
    如何使用正则表达式捕获多行文本块?
    匹配多行文本块的正则表达式匹配跨多行的文本可能会给正则表达式构造带来挑战。考虑以下示例文本:some Varying TEXT DSJFKDAFJKDAFJDSAKFJADSFLKDLAFKDSAF [more of the above, ending with a newline] [yep, t...
    编程 发布于2024-11-03
  • 软件开发中结构良好的日志的力量
    软件开发中结构良好的日志的力量
    日志是了解应用程序底层发生的情况的关键。 简单地使用 console.log 打印所有值并不是最有效的日志记录方法。日志的用途不仅仅是显示数据,它们还可以帮助您诊断问题、跟踪系统行为以及了解与外部 API 或服务的交互。在您的应用程序在没有用户界面的情况下运行的情况下,例如在系统之间处理和传输数据的...
    编程 发布于2024-11-03
  • 如何在单个命令行命令中执行多行Python语句?
    如何在单个命令行命令中执行多行Python语句?
    在单个命令行命令中执行多行Python语句Python -c 选项允许单行循环执行,但在命令中导入模块可能会导致语法错误。要解决此问题,请考虑以下解决方案:使用 Echo 和管道:echo -e "import sys\nfor r in range(10): print 'rob'&qu...
    编程 发布于2024-11-03
  • 查找数组/列表中的重复元素
    查找数组/列表中的重复元素
    给定一个整数数组,找到所有重复的元素。 例子: 输入:[1,2,3,4,3,2,5] 输出:[2, 3] 暗示: 您可以使用 HashSet 来跟踪您已经看到的元素。如果某个元素已在集合中,则它是重复的。为了保留顺序,请使用 LinkedHashSet 来存储重复项。 使用 HashSet 的 Ja...
    编程 发布于2024-11-03
  • JavaScript 回调何时异步?
    JavaScript 回调何时异步?
    JavaScript 回调:是否异步?JavaScript 回调并非普遍异步。在某些场景下,例如您提供的 addOne 和 simpleMap 函数的示例,代码会同步运行。浏览器中的异步 JavaScript基于回调的 AJAX 函数jQuery 中通常是异步的,因为它们涉及 XHR (XMLHtt...
    编程 发布于2024-11-03
  • 以下是根据您提供的文章内容生成的英文问答类标题:

Why does `char` behave differently from integer types in template instantiation when comparing `char`, `signed char`, and `unsigned char`?
    以下是根据您提供的文章内容生成的英文问答类标题: Why does `char` behave differently from integer types in template instantiation when comparing `char`, `signed char`, and `unsigned char`?
    char、signed char 和 unsigned char 之间的行为差​​异下面的代码可以成功编译,但 char 的行为与整数类型不同。cout << getIsTrue< isX<int8>::ikIsX >() << endl; cou...
    编程 发布于2024-11-03
  • 如何在动态生成的下拉框中设置默认选择?
    如何在动态生成的下拉框中设置默认选择?
    确定下拉框中选定的项目使用 标签创建下拉列表时,您可以可能会遇到需要将特定选项设置为默认选择的情况。这在预填写表单或允许用户编辑其设置时特别有用。在您呈现的场景中, 标记是使用 PHP 动态生成的,并且您希望根据值存储在数据库中。实现此目的的方法如下:设置选定的属性要在下拉框中设置选定的项目,您需...
    编程 发布于2024-11-03
  • Tailwind CSS:自定义配置
    Tailwind CSS:自定义配置
    介绍 Tailwind CSS 是一种流行的开源 CSS 框架,近年来在 Web 开发人员中广受欢迎。它提供了一种独特的可定制方法来创建美观且现代的用户界面。 Tailwind CSS 区别于其他 CSS 框架的关键功能之一是它的可定制配置。在这篇文章中,我们将讨论 Tailwin...
    编程 发布于2024-11-03
  • 使用 jQuery
    使用 jQuery
    什么是 jQuery? jQuery 是一个快速的 Javascript 库,其功能齐全,旨在简化 HTML 文档遍历、操作、事件处理和动画等任务。 “少写多做” MDN 状态: jQuery使得编写多行代码和tsk变得更加简洁,甚至一行代码.. 使用 jQuery 处理事件 jQuery 的另一个...
    编程 发布于2024-11-03
  • CONCAT() 如何增强 MySQL 搜索功能以实现完整名称匹配?
    CONCAT() 如何增强 MySQL 搜索功能以实现完整名称匹配?
    WHERE 子句中使用 MySQL CONCAT() 函数进行高效搜索一种常见的数据库操作是跨多列搜索数据。然而,当分别使用名字和姓氏字段搜索姓名时,可能会存在一些限制,例如捕获不完整的匹配。为了克服这个问题,可以使用 MySQL CONCAT() 函数将列组合成一个用于搜索的单个字段。这提供了更加...
    编程 发布于2024-11-03
  • [每日套餐] ms
    [每日套餐] ms
    多发性硬化症 ms 将各种时间格式转换为毫秒,反之亦然。 /* time format to milliseconds */ ms('2 days') // 172800000 ms('1d') // 86400000 ms('10h') // 36000000...
    编程 发布于2024-11-03
  • 考虑将 WordPress 数据库从本地迁移到生产环境的注意事项
    考虑将 WordPress 数据库从本地迁移到生产环境的注意事项
    将数据库从本地环境迁移到生产服务器可能会让人感到不知所措,但如果采用正确的方法,则不必如此。在本指南中,我将介绍确保迁移过程顺利进行时需要考虑的关键点。 数据库迁移的关键注意事项 1。方案(协议) 示例:http 或 https 2.域名 本地示例:localhost、localho...
    编程 发布于2024-11-03

免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。

Copyright© 2022 湘ICP备2022001581号-3