有没有办法从剪贴板粘贴与光标位置对齐的文本:
function myFunction() {
console.log('a');
<<PASTING HERE>>
}
默认情况下我得到:
function myFunction() {
console.log('a');
console.log('b');
console.log('c');
console.log('d');
}
但我希望看到最后两行也与两个空格对齐。 (当然,我可以选择文本并按制表符对齐它,但这是额外的操作)
最佳答案
基本上,您需要对新粘贴的代码进行缩进。我多年来一直使用以下代码:
(defvar yank-indent-modes '(emacs-lisp-mode lisp-mode
c-mode c++-mode js2-mode
tcl-mode sql-mode
perl-mode cperl-mode
java-mode jde-mode
lisp-interaction-mode
LaTeX-mode TeX-mode
go-mode cuda-mode
scheme-mode clojure-mode)
"Modes in which to indent regions that are yanked (or yank-popped)")
(defadvice yank (after indent-region activate)
"If current mode is one of 'yank-indent-modes, indent yanked text (with prefix arg don't indent)."
(if (member major-mode yank-indent-modes)
(let ((mark-even-if-inactive t))
(indent-region (region-beginning) (region-end) nil))))
此代码扩展了绑定(bind)到 C-y
的标准 yank
命令,并且如果当前模式位于 yank-indent- 中定义的模式列表中,模式
,然后它将在粘贴的代码片段上执行indent-region
。
附注您可能还需要在 yank-pop
命令上添加相同的 defadvice
。
关于emacs - 在 emacs 中粘贴与光标对齐的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67700693/