2013-07-30 15 views
7

を削除?デフォルトでは、emacsは常に単語を削除します。のEmacs:私は<kbd>Altキー</kbd> + <kbd>D</kbd>または<kbd>Altキー</kbd> + <kbd>を押すとバック​​スペース</kbd>は、隣接する空白または単一の単語のいずれかを削除し、他の現代のエディタと同じように動作するようにEmacsを設定するにはどうすればよいの空白文字や単語

+0

私はこれらのホットキーに言及したときにあなたが念頭にいた最新のエディタを理解しようとしています...私は実際にあなたが記述する動作に遭遇していません。私が試したほとんどのことは、行全体を削除したり、文書の開かれている履歴をナビゲートしたりするなど、何もしません。だから、あなたが気にしていた空白の削除の種類は本当にわかりません。 '' M- \ ''のようなものですか?またはポイントの前後だけを削除する必要がありますか? –

+0

また、http://www.emacswiki.org/emacs/DeletingWhitespaceここには、あらゆる種類の空白削除を行うためのユーザー提出コードの大規模なコレクションがあります。 –

+0

@wvxvw申し訳ありません、それはAltキーでなければなりません。日食、ビジュアルスタジオ、崇高な文章のような編集者。 – woodings

答えて

4

これはおそらく以前に解決されている可能性がありますが、コードを探す代わりに、独自のコードを書くことができます。とても楽しい!

これは私がそれを行う方法です、それが助けてくれることを望みます。

(defun kill-whitespace-or-word() 
    (interactive) 
    (if (looking-at "[ \t\n]") 
     (let ((p (point))) 
     (re-search-forward "[^ \t\n]" nil :no-error) 
     (backward-char) 
     (kill-region p (point))) 
    (kill-word 1))) 

そして、キーにバインド:あなたはCC-モードベースのバッファを使用している場合

(global-set-key (kbd "M-d") 'kill-whitespace-or-word) 
+0

私はkill-whitespace-or-word-backward機能を主に探しています。単語を逆方向に削除しますが、ほとんどは\ nで停止します。私はこのコードをそれに適合させることができると思っていますが、私はelispでそれを理解するにはあまりにも無知です:/ – mtourne

1

、あなたはおそらくHungry Delete Modeマイナーモードを探しています。

は違いの感触を得るためにいくつかの場所でをDELETE C-CDELC-Cを試してみてください。

好きな人は、M-x c-toggle-hungry-stateを実行するか、空腹の削除機能を好みのバインディングにリバウンドするだけで、空腹の削除を標準のキーで機能させることができます。

あなたはまだあなたは、あなたがc-hungry-delete-forwardに似た何かを行うことができ、単語または空白を殺すか、単に一時的にc-delete-functionを再バインドし、それを呼び出す前方に行うための一つのキーをピギーバックする必要があると思う場合。

(defun c-hungry-delete-forward-word() 
    "Delete the following word or all following whitespace 
up to the next non-whitespace character. 
See also \\[c-hungry-delete-backwards]." 
    (interactive) 
    (let ((c-delete-function (function kill-word))) 
    (c-hungry-delete-forward))) 

詳細については(ccmode) Hungry WS Deletionをご覧ください。

9

TroughいつもEmacsを使っていますが、私は基本的な機能を変更することはできますが、効率の点ではほとんど効果がないと思いました。実際、私が数回それをした後、私はそれを後悔してそれを元に戻しました。これは常に真実ではなく、いくつかのキーバインドは本当に不快なものではありませんが、まれに有用なものですが、私はこれがキルワードの仕組みとは違うと思います。実際には、私は今すぐ実現しました:私はEclipseでキーバインドを試しましたが、私はEmacsスタイルのキーバインディングを永遠に使っています...

とにかく、 「その機能は、私は自分自身があなたが記述機能のようなものを必要と見つけることはありません:)それが本当に壊れていることを確認し、そしてなぜ多分ここにある:

  1. M-SPC

    1つのだけのスペースに単語間のスペースを削減します。これは、単語の間にポイントがあって、単語を区切る余分なスペースを削除したければ、私が使用していたものです。

  2. M- \は、すべての水平スペースを削除します。これにより、スペースで区切られた2つの単語が結合されます。何を達成しようとしていることのように "スパース" の書式設定のいくつかの種類がある場合

  3. M-Xalign-regexpはそれを行うために


int foo   = 42; 
unsigned int bar = 43; 

はそこです。私はちょうどそれがインデントされ、そして場合には、それはインデントがある場合を除き、ホワイトスペースの)長いその結果としての実行を持って起こることはありません

  1. は、TABは、通常、より良い、それを処理します。 b)結果として空白が長く続く場合でも、一度に1文字ずつ移動することはほとんどないので、いくつかの空白に囲まれた点を見つけることは難しいです。 ArtistモードやDotダイアグラムのようなものが気になりますが、コード編集中には発生しません。最後に

  2. 、あなたも、ちょうど任意のテキストファイルを編集し、単語間の水平方向のスペースを追加または削除したいとしましょう、しようとしている場合は...もう一度、あなたがそこにのMxalign-regexpはそれを行うためにだ、またはその時に数行があれば、四角形で動作するコマンドを使用できます。さて、Emacsはアドホックなタブを認識して、TABを打ったポイントの前の最後の行と一致するようにテキストを整列させようとします。

    最後に

、何らかの理由で、私は、私はそうのようにそれを行うだろう、私は本当にあなたが記述を正確に何をするのに必要な:)測深できない場合:BACKSPACE \ KM-を(それ代わりに「K」のいずれかの他の重要なことができます - 私はそれについて考えることが怠惰だ場合、それは、または:)を入力する高速ですので、それは、ちょうどあなたの指の下にある:M-SPCMfのMbのCw - おそらく多くのように聞こえるかもしれませんが、これらはあなたが覚えているコマンドですとにかく時間のすべてを使用しているので、スピードの点であなたを妨げることはありません。

7
(defvar movement-syntax-table 
    (let ((st (make-syntax-table))) 
    ;; ` default = punctuation 
    ;; ' default = punctuation 
    ;; , default = punctuation 
    ;; ; default = punctuation 
    (modify-syntax-entry ?{ "." st) ;; { = punctuation 
    (modify-syntax-entry ?} "." st) ;; } = punctuation 
    (modify-syntax-entry ?\" "." st) ;; " = punctuation 
    (modify-syntax-entry ?\\ "_" st) ;; \ = symbol 
    (modify-syntax-entry ?\$ "_" st) ;; $ = symbol 
    (modify-syntax-entry ?\% "_" st) ;; % = symbol 
    st) 
    "Syntax table used while executing custom movement functions.") 

(defun delete-word-or-whitespace (&optional arg) 
"http://stackoverflow.com/a/20456861/2112489" 
(interactive "P") 
    (with-syntax-table movement-syntax-table 
    (let* (
     beg 
     end 
     (word-regexp "\\sw") 
     (punctuation-regexp "\\s.") 
     (symbol-regexp "\\s_\\|\\s(\\|\\s)")) 
     (cond 
     ;; Condition # 1 
     ;; right of cursor = word or punctuation or symbol 
     ((or 
      (save-excursion (< 0 (skip-syntax-forward "w"))) 
      (save-excursion (< 0 (skip-syntax-forward "."))) 
      (save-excursion (< 0 (skip-syntax-forward "_()")))) 
      ;; Condition #1 -- Step 1 of 2 
      (cond 
      ;; right of cursor = word 
      ((save-excursion (< 0 (skip-syntax-forward "w"))) 
       (skip-syntax-forward "w") 
       (setq end (point)) 
       (while (looking-back word-regexp) 
       (backward-char)) 
       (setq beg (point)) 
       (delete-region beg end)) 
      ;; right of cursor = punctuation 
      ((save-excursion (< 0 (skip-syntax-forward "."))) 
       (skip-syntax-forward ".") 
       (setq end (point)) 
       (while (looking-back punctuation-regexp) 
       (backward-char)) 
       (setq beg (point)) 
       (delete-region beg end)) 
      ;; right of cursor = symbol 
      ((save-excursion (< 0 (skip-syntax-forward "_()"))) 
       (skip-syntax-forward "_()") 
       (setq end (point)) 
       (while (looking-back symbol-regexp) 
       (backward-char)) 
       (setq beg (point)) 
       (delete-region beg end))) 
      ;; Condition #1 -- Step 2 of 2 
      (cond 
      ;; right of cursor = whitespace 
      ;; left of cursor = not word/not symbol/not punctuation = whitespace or bol 
      ((and 
       (save-excursion (< 0 (skip-chars-forward "\s\t"))) 
       (not (save-excursion (> 0 (skip-syntax-backward "w")))) 
       (not (save-excursion (> 0 (skip-syntax-backward ".")))) 
       (not (save-excursion (> 0 (skip-syntax-backward "_()"))))) 
       (setq beg (point)) 
       (skip-chars-forward "\s\t") 
       (setq end (point)) 
       (delete-region beg end)) 
      ;; right of cursor = whitespace 
      ;; left of cursor = word or symbol or punctuation 
      ((and 
       (save-excursion (< 0 (skip-chars-forward "\s\t"))) 
       (or 
        (save-excursion (> 0 (skip-syntax-backward "w"))) 
        (save-excursion (> 0 (skip-syntax-backward "."))) 
        (save-excursion (> 0 (skip-syntax-backward "_()"))))) 
       (fixup-whitespace)))) 
     ;; Condition # 2 
     ;; right of cursor = whitespace 
     ;; left of cursor = bol | left of cursor = whitespace | right of cursor = whitespace + eol 
     ((and 
      (save-excursion (< 0 (skip-chars-forward "\s\t"))) 
      (or 
       (bolp) 
       (save-excursion (> 0 (skip-chars-backward "\s\t"))) 
       (save-excursion (< 0 (skip-chars-forward "\s\t")) (eolp)))) 
      (setq beg (point)) 
      (skip-chars-forward "\s\t") 
      (setq end (point)) 
      (delete-region beg end)) 
     ;; Condition # 3 
     ;; right of cursor = whitespace or eol 
     ;; left of cursor = word or symbol or punctuation 
     ;; not bol + word or symbol or punctuation 
     ;; not bol + whitespace + word or symbol or punctuation 
     ((and 
      (or (save-excursion (< 0 (skip-chars-forward "\s\t"))) (eolp)) 
      (or 
       (save-excursion (> 0 (skip-syntax-backward "w"))) 
       (save-excursion (> 0 (skip-syntax-backward "."))) 
       (save-excursion (> 0 (skip-syntax-backward "_()")))) 
      (not (save-excursion (> 0 (skip-syntax-backward "w")) (bolp))) 
      (not (save-excursion (> 0 (skip-syntax-backward ".")) (bolp))) 
      (not (save-excursion (> 0 (skip-syntax-backward "_()")) (bolp))) 
      (not (save-excursion (and (> 0 (skip-syntax-backward "w")) (> 0 (skip-chars-backward "\s\t")) (bolp)))) 
      (not (save-excursion (and (> 0 (skip-syntax-backward ".")) (> 0 (skip-chars-backward "\s\t")) (bolp)))) 
      (not (save-excursion (and (> 0 (skip-syntax-backward "_()")) (> 0 (skip-chars-backward "\s\t")) (bolp))))) 
      (setq end (point)) 
      (cond 
      ((save-excursion (> 0 (skip-syntax-backward "w"))) 
       (while (looking-back word-regexp) 
       (backward-char))) 
      ((save-excursion (> 0 (skip-syntax-backward "."))) 
       (while (looking-back punctuation-regexp) 
       (backward-char))) 
      ((save-excursion (> 0 (skip-syntax-backward "_()"))) 
       (while (looking-back symbol-regexp) 
       (backward-char)))) 
      (setq beg (point)) 
      (when (save-excursion (> 0 (skip-chars-backward "\s\t"))) 
      (skip-chars-backward "\s\t") 
      (setq beg (point))) 
      (delete-region beg end) 
      (skip-chars-forward "\s\t")) 
     ;; Condition # 4 
     ;; not bol = eol 
     ;; left of cursor = bol + word or symbol or punctuation | bol + whitespace + word or symbol or punctuation 
     ((and 
      (not (and (bolp) (eolp))) 
      (or 
       (save-excursion (> 0 (skip-syntax-backward "w")) (bolp)) 
       (save-excursion (> 0 (skip-syntax-backward ".")) (bolp)) 
       (save-excursion (> 0 (skip-syntax-backward "_()")) (bolp)) 
       (save-excursion (and (> 0 (skip-syntax-backward "w")) (> 0 (skip-chars-backward "\s\t")) (bolp))) 
       (save-excursion (and (> 0 (skip-syntax-backward ".")) (> 0 (skip-chars-backward "\s\t")) (bolp))) 
       (save-excursion (and (> 0 (skip-syntax-backward "_()")) (> 0 (skip-chars-backward "\s\t")) (bolp))))) 
      (skip-chars-forward "\s\t") 
      (setq end (point)) 
      (setq beg (point-at-bol)) 
      (delete-region beg end)) 
     ;; Condition # 5 
     ;; point = eol 
     ;; not an empty line 
     ;; whitespace to the left of eol 
     ((and 
      (not (and (bolp) (eolp))) 
      (eolp) 
      (save-excursion (> 0 (skip-chars-backward "\s\t")))) 
      (setq end (point)) 
      (skip-chars-backward "\s\t") 
      (setq beg (point)) 
      (delete-region beg end)) 
     ;; Condition # 6 
     ;; point = not eob 
     ;; point = bolp and eolp 
     ;; universal argument = C-u = '(4) 
     ((and 
      (not (eobp)) 
      (and (bolp) (eolp)) 
      (equal arg '(4))) 
      (delete-forward-char 1)))))) 
+0

私は一般的に機能が好きですが、それはかなり普遍的ではないことがわかりました。つまり、1行目を削除すると、前の行を削除するために折り返さずに、単に「停止」します –

+0

行末に空白がある場合、行頭には文字の削除が開始されませんすべて? –

+0

@Leo Ufimtsev - 私は、行末の空白を扱うための条件を追加しました。ポイントがラインの終わりにあり、空白が左にあるときは、すべての空白を左に削除します。これは、左の空白が削除される唯一の状況です。この機能を改善してくれてありがとう。私は、現在の段落が削除された後、行の前後を削除するために関数の範囲を増やす必要があるかどうかについていくつか考えたいと思います。私が躊躇する理由は、自分自身を含む多くのユーザーが、機能を繰り返すことで過度に怒ってしまうことがあるということです。 – lawlist

関連する問題

 関連する問題