テキストが現在折り返されているすべてのポイントに自動的に改行を挿入する方法はありますか? この操作の後、行は折り返されませんが、見た目は同じになるはずです。
答え1
これ用のプラグインを作成します。ツール » 新しいプラグイン…次のスクリプトを入力します。
import sublime, sublime_plugin
class WrapLinesExCommand(sublime_plugin.TextCommand):
def run(self, edit):
wrap_column = 0
if self.view.settings().get('word_wrap') == False:
# wrapping is disabled, do nothing
return
if self.view.settings().get('wrap_width') == 0:
# compute wrap column from viewport width
wrap_column = int(self.view.viewport_extent()[0] / self.view.em_width())
else:
wrap_column = self.view.settings().get('wrap_width')
e = self.view.begin_edit()
rewrap(self.view, e, wrap_column)
self.view.end_edit(e)
def rewrap(v, e, column):
# 0-indexed current line
current_line_no = 0
# RHS expression is line count, can change whenever we create a new one
while current_line_no < v.rowcol(v.size())[0] + 1:
# where current line drawing starts
current_line_coords = v.text_to_layout(v.text_point(current_line_no, 0))
# rightmost character drawn in current viewport
textpos = v.layout_to_text((v.em_width() * (column), current_line_coords[1]))
# physical line boundaries as absolute text positions
current_line = v.line(textpos)
if textpos < current_line.b:
# the current line spans multiple rows, so insert a newline at the wrap column
textpos = v.layout_to_text((v.em_width() * (column), current_line_coords[1]))
next_line_indent = v.text_to_layout(textpos+1)[0]
# TODO why -1?
next_line_indent_chars = int(next_line_indent/(v.em_width()))-1
# determine how to indent the following line based on how wide the wrapping indents and what the current tab/spaces settings are
if v.settings().get('translate_tabs_to_spaces') and v.settings().get('use_tab_stops'):
next_line_indent_chars = next_line_indent_chars / v.settings().get('tab_size')
next_line_indent_string = '\t' * next_line_indent_chars
else:
next_line_indent_string = ' ' * next_line_indent_chars
# insert newline and spacing at wrap column (sublime hides actual line endings from editor, therefore it's always LF)
v.insert(e, textpos, '\n' + next_line_indent_string)
else:
# only continue to the next line if we didn't edit the current line
current_line_no = current_line_no + 1
例えば、wrap_lines_ex_command.py
デフォルトの(User
)ディレクトリに名前を付けて保存します。
メニューバーからアクセスできるようにするには、パッケージを参照…メニュー項目をクリックし、User
フォルダに移動して、Main.sublime-menu
説明に従って編集(必要に応じて作成)します。この答えたとえば次のようなテキストが含まれます。
[
{
"id": "edit",
"children":
[
{"id": "wrap"},
{"command": "wrap_lines_ex", "caption": "Wrap All Lines"}
]
}
]
スクリーンショット
前に:
後:
もちろん、この場合、コメントもラップされているため、コードは機能しなくなります。しかし、それは質問に応じた設計としての動作です。
答え2
数年後、このような用途の既成パッケージ (プラグイン) が登場しました。これらは、(ウィンドウに表示されている現在の折り返しと一致するように) 要求を完全に満たすものではないかもしれませんが、設定で折り返す列を設定できます。
サブライムラッププラス
インストール
- 開ける
Sublime Text 2 or 3
。 command-shift-p
(Mac OS X) またはctrl-shift-p
(Windows)を押してを開きCommand Palette
、「インストール」と入力して、 のオプションを選択しますInstall Package Control
。- を再度開き
Command Palette
、再度「インストール」と入力して、オプションを選択しますInstall a Package
。 - 入力を開始して選択します
sublime-wrap-text
。
使用法
- 問題のテキストを選択します。
command+alt+q
(Mac OS X) またはalt+q
(Windows)を押します。
詳しい使用方法や設定方法については、GitHub ページを参照してください。
デモ
前に
後(テキスト全体をハイライトして、Alt+Q を押しました)
もう一つの類似パッケージは崇高なラップステートメント
私自身はまだ試していませんが、よろしければ試してみてください。
答え3
コメントとして追加するにはアカウントが若すぎる
が、2021年のアップデートを追加したいMarredCheeseの素晴らしい2017年の回答上記にあります。
MarredCheese が言及した最初のパッケージをインストールするには:
GitHub: https://github.com/ehuss/Sublime-Wrap-Plus
崇高なパッケージ名: ラッププラス
ステップ1。まだ動作します。元の投稿から貼り付けます:
- サブライムを開く
ステップ2。まだ動作します。元の投稿から貼り付けます:
- commandshiftp(Mac OS X) またはctrlshiftp(Windows)を押してを開き
Command Palette
、「インストール」と入力して、 のオプションを選択しますInstall Package Control
。
ステップ3。若干不正確です。
構文が変更された場合、2021 年に機能するものは次のとおりです。
ステップ3. (オリジナル):
- を開き
Command Palette again
、もう一度「インストール」と入力して、オプションを選択しますInstall a Package
。
ステップ 3. (2021 年版に改訂):
- 代わりに、
Install a Package
次のいずれかを入力します。Install Package
またはPackage Control: Install Package
- 注: 「パッケージ コントロール: 高度なインストール パッケージ」は便利です
が、このレシピには必要ありません。
ステップ4。動作しなくなりました。
ステップ 4. (オリジナル):
- 入力を開始して選択します
sublime-wrap-text
。
ステップ 4. (2021 年版に改訂):
Wrap Plus
インストールパッケージのドロップダウンに入力します
(https://github.com/ehuss/Sublime-Wrap-Plusこのエントリの詳細をご覧ください)
MarredCheese が回答の下部で言及した 2 番目のパッケージをインストールするには:
GitHub: https://github.com/shagabutdinov/sublime-wrap-statement
Sublime パッケージ名:ラップステートメント
前のセクションと同じ手順に従いますが、ステップ3。WrapStatement
代わりに(スペースなしで)入力して
ください。(https://github.com/shagabutdinov/sublime-wrap-statementInstall Package
ドロップダウンのこのエントリの詳細で
答え4
現時点では、この機能は Sublime Text 2 の設定には含まれていないようです (Default/Preferences.sublime-settings で確認できます)。"line_padding_bottom": 4
すべての行の読みやすさを向上させるために、(各行の下のピクセル数が 4 の場合) のような設定オプションを使用することは可能ですが、行が折り返されているかどうかに応じて異なる行パディングを選択的に適用することはできません。
機能リクエストを送信することもできますSublime Text 2のフォーラム実装が合理的であれば、私もこの機能を高く評価すると思います。