プログラミングやドキュメント編集をしていて、こんな経験はありませんか?
「長いコメント行が画面からはみ出して、横にスクロールしないと全部見えない」
「コードレビューをするときに、横スクロールが面倒で効率が悪い」
「長い文字列や配列の定義が見づらくて困っている」
これらの問題を解決してくれるのが、VS Codeの**テキスト折り返し(ワードラップ)**機能です。
この機能を使うことで、長い行を画面幅に合わせて自動的に次の行に折り返して表示でき、横スクロールなしで全体を確認できるようになります。
この記事では、VS Codeでテキスト折り返しを効率的に切り替える方法を基本から応用まで、初心者にもわかりやすく解説します。
テキスト折り返し(ワードラップ)とは
基本的な概念
テキスト折り返しは、長い行を画面の表示幅に合わせて複数行に分割して表示する機能です。
折り返しオフの状態
// 長い行の例(画面幅を超える)
const longVariableName = "これは非常に長い文字列で、通常の画面幅を大幅に超えてしまい、横スクロールが必要になってしまう例です。";
問題点:
- 行の全体を見るために横スクロールが必要
- コードの構造を把握しにくい
- レビュー作業が非効率
- モバイルデバイスでの表示が困難
折り返しオンの状態
// 同じ行が画面幅で折り返される
const longVariableName = "これは非常に長い文字列で、通常の画面幅を大幅に超えて
しまい、横スクロールが必要になってしまう例です。";
利点:
- 横スクロール不要で全体が見える
- 画面を有効活用できる
- 読みやすさが向上
- 複数ファイルの並列表示時に便利
折り返し機能が活用される場面
コードレビュー時
# レビュー対象のコード
def process_user_data(user_id, email, first_name, last_name, phone_number, address, city, postal_code, country):
"""
ユーザーデータを処理する関数です。非常に多くのパラメータを受け取り、それらを適切に処理してデータベースに保存します。
"""
# 処理内容...
折り返し機能により、長い関数定義やコメントが見やすくなります。
ドキュメント編集時
# 長いMarkdownテキストの例
これは技術文書の例です。詳細な説明や手順を記述する際には、一行が非常に長くなることがあり、折り返し機能を使用することで読みやすさが大幅に向上します。特に、複数の概念を一つの段落で説明する場合や、詳細な設定手順を記述する際に有効です。
設定ファイルの編集時
{
"description": "これは設定ファイルの例です。長い説明文や複雑な設定値が含まれている場合、折り返し機能を有効にすることで、設定内容を一目で確認できるようになります。",
"configuration": {
"longPropertyName": "非常に長い値が設定される場合があります..."
}
}
折り返し切り替えの基本操作
デフォルトショートカットの確認
VS Codeでは、折り返し機能に関して以下のような状況があります。
標準設定の現状
多くのVS Codeインストールでは、以下のショートカットが設定されています:
Windows/Linux:
Alt + Z
macOS:
Option + Z
ただし、このショートカットは環境や拡張機能によって異なる場合があります。
ショートカットが設定されていない場合
一部の環境では、折り返し専用のショートカットが設定されていない場合があります。その場合は以下の方法で操作できます。
コマンドパレットからの操作
基本的な手順
- コマンドパレットを開く
- Windows/Linux:
Ctrl + Shift + P
- macOS:
Cmd + Shift + P
- Windows/Linux:
- コマンドを入力
Toggle Word Wrap
または日本語環境では折り返しの切り替え
- Enter キーで実行
より効率的な操作
# 部分入力でも検索できる
"toggle word" → Toggle Word Wrap
"wrap" → Toggle Word Wrap
"折り返し" → 折り返しの切り替え
ステータスバーからの操作
ステータスバーでの表示
VS Codeの下部ステータスバーで折り返し状態を確認できます:
- 折り返しオフ: 何も表示されない、または「Word Wrap Off」
- 折り返しオン: 「Word Wrap On」または折り返しアイコン
ステータスバーからの切り替え
- ステータスバーの折り返し表示部分をクリック
- 状態が切り替わる
- エディタの表示が即座に更新される
メニューからの操作
表示メニューからのアクセス
- メニューバーの「表示」をクリック
- **「折り返しの切り替え」**または「Toggle Word Wrap」を選択
- 即座に状態が切り替わる
カスタムショートカットの設定方法
キーボードショートカットの設定画面
設定画面を開く方法
方法1: ショートカットキー
- Windows/Linux:
Ctrl + K, Ctrl + S
- macOS:
Cmd + K, Cmd + S
方法2: メニューから
- 「ファイル」→「基本設定」→「キーボードショートカット」
- macOSでは「Code」→「基本設定」→「キーボードショートカット」
ショートカットの検索と設定
# 検索ボックスに入力
Toggle Word Wrap
# 表示された項目をダブルクリックして新しいキーを設定
例: Alt + W
例: Ctrl + Alt + Z
例: F2
推奨ショートカットキーの組み合わせ
一般的で覚えやすい組み合わせ
Alt + W # W = Word Wrap
Ctrl + Alt + W # より確実に独立
F9 # ファンクションキー
Ctrl + L # L = Line wrap
避けるべきキー組み合わせ
Ctrl + C, Ctrl + V # 基本操作と重複
Ctrl + S, Ctrl + O # 保存・開くと重複
F5, F12 # デバッグと重複
Alt + Tab # システムと重複
settings.json での詳細設定
キーバインド設定ファイル
// keybindings.json
[
{
"key": "alt+w",
"command": "editor.action.toggleWordWrap",
"when": "editorTextFocus"
},
{
"key": "ctrl+alt+w",
"command": "editor.action.toggleWordWrap",
"when": "!editorTextFocus"
}
]
条件付きキーバインド
[
{
"key": "alt+z",
"command": "editor.action.toggleWordWrap",
"when": "editorLangId == 'markdown'"
},
{
"key": "f9",
"command": "editor.action.toggleWordWrap",
"when": "editorLangId == 'plaintext'"
}
]
折り返し設定の詳細カスタマイズ
基本的な設定オプション
editor.wordWrap の設定値
{
// 折り返しを無効
"editor.wordWrap": "off",
// 常に折り返しを有効
"editor.wordWrap": "on",
// エディタ幅に合わせて折り返し
"editor.wordWrap": "bounded",
// より詳細な制御
"editor.wordWrap": "wordWrapColumn"
}
各設定値の詳細
“off” (無効)
// 長い行はそのまま表示、横スクロールが必要
const veryLongVariableName = "This is a very long string that will extend beyond the editor width and require horizontal scrolling to view completely";
“on” (有効)
// エディタ幅で自動折り返し
const veryLongVariableName = "This is a very long string that will wrap
automatically at the editor width boundary";
“bounded” (制限付き)
// エディタ幅と wordWrapColumn の小さい方で折り返し
const veryLongVariableName = "This will wrap at either editor width or
the specified column, whichever is smaller";
折り返し位置の詳細制御
文字数での制御
{
"editor.wordWrap": "wordWrapColumn",
"editor.wordWrapColumn": 80,
"editor.rulers": [80, 120]
}
この設定により、80文字で強制的に折り返されます。
言語別の設定
{
// Markdown ファイルでは常に折り返し
"[markdown]": {
"editor.wordWrap": "on",
"editor.wordWrapColumn": 100
},
// Python ファイルでは80文字で折り返し
"[python]": {
"editor.wordWrap": "wordWrapColumn",
"editor.wordWrapColumn": 80
},
// JavaScriptファイルでは120文字で折り返し
"[javascript]": {
"editor.wordWrap": "wordWrapColumn",
"editor.wordWrapColumn": 120
}
}
高度な折り返し設定
折り返し時のインデント制御
{
"editor.wrappingIndent": "none", // インデントなし
"editor.wrappingIndent": "same", // 同じインデント
"editor.wrappingIndent": "indent", // 追加インデント
"editor.wrappingIndent": "deepIndent" // 深いインデント
}
視覚的な折り返し設定
{
// 折り返し記号の表示
"editor.wrappingStrategy": "simple", // シンプルな折り返し
"editor.wrappingStrategy": "advanced", // 高度な折り返し
// 行番号の表示制御
"editor.lineNumbers": "on",
"editor.lineNumbers": "relative",
"editor.lineNumbers": "interval"
}
実用的な活用シーン
プログラミング言語別の最適設定
JavaScript/TypeScript開発
{
"[javascript]": {
"editor.wordWrap": "wordWrapColumn",
"editor.wordWrapColumn": 120,
"editor.rulers": [80, 120]
},
"[typescript]": {
"editor.wordWrap": "wordWrapColumn",
"editor.wordWrapColumn": 120,
"editor.rulers": [80, 120]
}
}
理由: モダンJavaScriptでは120文字程度が読みやすさとのバランスが良い
Python開発
{
"[python]": {
"editor.wordWrap": "wordWrapColumn",
"editor.wordWrapColumn": 79,
"editor.rulers": [79, 88]
}
}
理由: PEP 8ガイドラインでは79文字が推奨
Markdown/ドキュメント
{
"[markdown]": {
"editor.wordWrap": "on",
"editor.wrappingIndent": "same",
"editor.lineNumbers": "off"
}
}
理由: 文書では自然な折り返しが読みやすさを向上
チーム開発での統一設定
プロジェクト設定ファイル
// .vscode/settings.json
{
"editor.wordWrap": "wordWrapColumn",
"editor.wordWrapColumn": 100,
"editor.rulers": [80, 100],
"editor.wrappingIndent": "same",
// 言語別設定
"[markdown]": {
"editor.wordWrap": "on"
},
"[python]": {
"editor.wordWrapColumn": 79
}
}
EditorConfig との連携
# .editorconfig
root = true
[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
[*.{js,ts}]
max_line_length = 120
[*.py]
max_line_length = 79
[*.md]
trim_trailing_whitespace = false
max_line_length = off
コードレビューでの活用
レビュー用の一時的設定
{
// レビュー時に見やすくする設定
"editor.wordWrap": "on",
"editor.minimap.enabled": false,
"editor.lineNumbers": "on",
"diffEditor.wordWrap": "on"
}
Git差分表示での折り返し
{
// 差分エディタでの折り返し設定
"diffEditor.wordWrap": "on",
"diffEditor.ignoreTrimWhitespace": false,
"git.decorations.enabled": true
}
よくある問題と解決方法
ショートカットが動作しない場合
問題1: キーバインドの競合
症状: Alt + Z
を押しても反応しない
確認方法:
- キーボードショートカット設定を開く
- 検索欄に
Alt+Z
と入力 - 複数のコマンドに割り当てられていないか確認
解決方法:
// 競合を解決するキーバインド設定
[
{
"key": "alt+z",
"command": "-other.conflicting.command" // 他のコマンドを無効化
},
{
"key": "alt+z",
"command": "editor.action.toggleWordWrap"
}
]
問題2: 拡張機能による干渉
症状: 拡張機能インストール後にショートカットが効かない
解決手順:
- 拡張機能を一時的に無効化
- ショートカットの動作確認
- 干渉する拡張機能を特定
- 拡張機能の設定を調整または代替キーを設定
折り返し表示の問題
問題3: コードの可読性が低下
症状: 折り返しによりコードが読みにくくなる
// 問題のある折り返し例
const result = someVeryLongFunctionName(parameter1, parameter2,
parameter3, parameter4);
解決方法:
{
"editor.wrappingIndent": "indent", // 適切なインデント
"editor.wordWrapColumn": 100 // 適切な幅設定
}
// 改善された表示
const result = someVeryLongFunctionName(parameter1, parameter2,
parameter3, parameter4);
問題4: パフォーマンスの低下
症状: 大きなファイルで折り返しが重い
解決方法:
{
"editor.wrappingStrategy": "simple", // シンプルな算出
"editor.wordWrap": "bounded", // 制限付き折り返し
"editor.wordWrapColumn": 120, // 適度な幅
"files.associations": {
"*.log": "plaintext" // 重いファイルの関連付け
}
}
特定ファイル種別での問題
問題5: JSON/XML ファイルの折り返し
症状: 構造化ファイルが見づらい
// 問題のある表示
{"very_long_property_name": "very_long_value_that_wraps_and_becomes_difficult_to_read", "another_property": "value"}
解決方法:
{
"[json]": {
"editor.wordWrap": "off", // JSONは折り返しなし
"editor.formatOnSave": true // 自動整形で解決
},
"[xml]": {
"editor.wordWrap": "on",
"editor.wordWrapColumn": 120
}
}
高度な活用テクニック
マルチカーソルとの組み合わせ
複数行の同時編集
// 複数の長い行を同時に編集
const config1 = "very long configuration string that needs to be modified";
const config2 = "another very long configuration string that needs to be modified";
const config3 = "yet another very long configuration string that needs to be modified";
- 折り返し機能を有効化
Alt
を押しながらクリックでマルチカーソル- 各行を同時に編集
分割エディタでの活用
エディタ分割時の設定
{
// 分割時の自動調整
"editor.wordWrap": "bounded",
"editor.wordWrapColumn": 80,
"workbench.editor.splitInGroupLayout": "horizontal"
}
比較表示での折り返し
{
"diffEditor.wordWrap": "on",
"diffEditor.renderSideBySide": true,
"merge-conflict.autoNavigateNextConflict.enabled": true
}
拡張機能との連携
Prettier との組み合わせ
{
"editor.formatOnSave": true,
"prettier.printWidth": 80,
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.wordWrapColumn": 80
}
}
ESLint との連携
{
"eslint.rules.customizations": [
{
"rule": "max-len",
"severity": "warn"
}
],
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
}
}
まとめ
VS Codeのテキスト折り返し機能は、コーディングとドキュメント編集の効率を大幅に向上させる重要な機能です。
重要なポイント
- 基本操作:
Alt + Z
またはコマンドパレットで簡単切り替え - カスタム設定: 個人の好みに合わせたショートカット設定
- 詳細制御: 言語別・プロジェクト別の最適化設定
- 問題解決: 競合や性能問題への適切な対処
効果的な活用方法
- 状況別使い分け: コードレビュー時とコーディング時での切り替え
- チーム統一: プロジェクト設定での一貫性確保
- 言語最適化: プログラミング言語ごとの適切な設定
- パフォーマンス考慮: 大きなファイルでの効率的な設定
コメント