ホームページ > 記事 > ウェブフロントエンド > JS: VSCode のヒント
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:
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.
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.
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."
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!
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.
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();.
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.
There are a few must-have extensions for Node.js developers:
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.
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.
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.
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.
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.
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.
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.
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 } }
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.
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.
Mastering keyboard shortcuts can significantly boost your productivity. Here are a few more handy ones:
Explore these extensions to further enhance your productivity:
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.
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.
For front-end developers, CSS Peek allows you to view CSS definitions directly in your HTML file by hovering over class names or IDs.
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.
Depending on your tech stack, there are specific snippets and extensions you might find useful:
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.
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.
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.
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.
集中する必要がある場合、Zen モードは気を散らすことのないコーディング環境を提供します。 Ctrl + K Z (Mac の場合は Cmd + K Z) を押して、Zen モードに入ります。これにより、すべてのツールバーとパネルが非表示になり、コードだけが残ります。
次のような機能を使用してコードベースを効率的にナビゲートします。
プロジェクト マネージャー拡張機能は、複数のプロジェクトの管理と切り替えに役立ちます。現在のプロジェクトを保存し、最近のプロジェクトをリストし、それらをすばやく切り替えることができます。
コード スペル チェッカー拡張機能を使用して、コードやコメント内の恥ずかしいタイプミスを回避します。スペルミスを強調表示し、提案を提供して、ドキュメント名と変数名をよりプロフェッショナルなものにします。
Git Graph 拡張機能を使用して、リポジトリのコミット履歴を視覚化します。ブランチ、コミット、マージがグラフィカルに表示されるため、プロジェクトのフローを理解しやすくなります。
VS Code には強力な差分機能があります。ファイルを選択して Ctrl + D (Mac の場合は Cmd + D) を押すと、ファイルを比較したり変更を表示したりできます。これは、マージ競合をコミットまたは解決する前に変更を確認するのに役立ちます。
CodeMetrics 拡張機能は、コードの複雑さのメトリクスを提供し、リファクタリングが必要な領域を特定するのに役立ちます。関数とクラスの認知的な複雑さを示し、クリーンで効率的なコードの維持が容易になります。
ワークフローに合わせてキーバインドをカスタマイズできます。 Ctrl + K Ctrl + S (または Mac の場合は Cmd + K Cmd + S) でキーバインド エディターを開き、既存のショートカットを変更するか、新しいショートカットを作成します。
VS Code は、強力なファイルおよびコード ナビゲーション機能を提供します。
これらの拡張機能は、HTML および JSX 開発に特に役立ちます。自動終了タグは入力時にタグを自動的に閉じますが、自動タグ名変更は開始タグと終了タグの間の変更を同期します。
特定のプロジェクトのニーズに合わせた拡張機能をインストールします。たとえば、Python プロジェクトに取り組んでいる場合、Python 拡張機能は、IntelliSense、lint、デバッグなどの包括的なツール セットを提供します。 Java プロジェクトの場合は、Java Extension Pack を検討してください。
これらのヒントは、しばらくの間あなたを忙しくさせ、VS Code のセットアップを大幅に強化するでしょう。これらの機能をすべて試して、コーディングを楽しんでください!
以上がJS: VSCode のヒントの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。