在 XCode 中操作文本,移动一行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1380214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Manipulating text in XCode, moving one line
提问by Jeffrey Harris
In emacs I have various functions to manipulate text. Now that I'm using xcode, I suppose I could make emacs my default editor, but I want to browse obj-c objects and such, so I'd rather just implement my most used text manipulation commands for xcode.
在 emacs 中,我有各种操作文本的功能。现在我正在使用 xcode,我想我可以将 emacs 设为我的默认编辑器,但我想浏览 obj-c 对象等,所以我宁愿只为 xcode 实现我最常用的文本操作命令。
First on my list, I'd like a command that moves the text of the current line up/down one line, keeping the cursor on the current line.
首先在我的列表中,我想要一个命令将当前行的文本向上/向下移动一行,将光标保持在当前行上。
In emacs this is:
在 emacs 中,这是:
(defun move-one-line-downward ()
"Move current line downward once."
(interactive)
(forward-line)
(transpose-lines 1)
(forward-line -1))
I'd be happiest if I could write a script in Python that would do the equivalent in XCode, but as far as I can tell, I need to talk to AppleScript to do this.
如果我能用 Python 编写一个可以在 XCode 中执行等效操作的脚本,我会很高兴,但据我所知,我需要与 AppleScript 交谈才能做到这一点。
Can someone walk me through how to do this with XCode?
有人可以指导我如何使用 XCode 做到这一点吗?
回答by cescofry
Xcode 4 has a new set of command for moving the line where the cursor is or the selected text with command
+ option
+ [
or ]
的Xcode 4具有用于移动光标所在行或所选择的文本与一组新的命令的command
+ option
+[
或]
??[
or ??]
??[
或者 ??]
回答by adurdin
What you're wanting to do can be achieved through Xcode's “User Scripts”—and it helpfully comes with a pair of scripts that almost do what you want. (I'm using Xcode 3.2 here, but I think these were also there in 3.1)
您可以通过 Xcode 的“用户脚本”实现您想要做的事情——它附带了一对几乎可以做您想做的脚本。(我在这里使用 Xcode 3.2,但我认为这些也在 3.1 中)
Look in /Developer/Library/Xcode/User Scripts/
; there are two applescripts there, Move Line Up.scpt
and Move Line Down.scpt
. Here's what's in Move Line Up
:
进去看看/Developer/Library/Xcode/User Scripts/
;那里有两个applescripts,Move Line Up.scpt
和Move Line Down.scpt
。这是里面的内容Move Line Up
:
(*
To edit this script, choose Save As... and save it in your home directory, then re-add it to the User Scripts list.
*)
using terms from application "Xcode"
tell first text document
set {startLine, endLine} to selected paragraph range
if startLine > 1 then
set theText to (paragraphs startLine through endLine)
set theText to (theText as string)
delete (paragraphs startLine through endLine)
make new paragraph at beginning of paragraph (startLine - 1) with data theText
set selected paragraph range to {startLine - 1, endLine - 1}
else
beep 1
end if
end tell
end using terms from
These almost do what you want, except they select the whole line afterwards; I'm no applescript expert, but you probably want to store the selected character range
. Have a look at the Xcode scripting dictionary (in AppleScript Editor, File -> Open Dictionary -> Xcode) to find out the types of objects you can manipulate.
这些几乎可以做你想做的,除了他们之后选择整行;我不是 Applescript 专家,但您可能想将selected character range
. 查看 Xcode 脚本字典(在 AppleScript 编辑器中,文件 -> 打开字典 -> Xcode)以找出您可以操作的对象类型。
You can add your own scripts to Xcode with the “Edit User Scripts”menu item in the script menu; and assign shortcut keys to the scripts in that window also, by double-clicking in the right-hand column beside the entry for the script menu item.
您可以使用脚本菜单中的“编辑用户脚本”菜单项将自己的脚本添加到 Xcode;并通过双击脚本菜单项条目旁边的右侧列,为该窗口中的脚本分配快捷键。
You can also use shell scripts (perl, python, bash, whatever) in the User Scripts menu, but these process only the selection or the whole file, so might be a bit heavyweight for moving a single line up or down.
您还可以在 User Scripts 菜单中使用 shell 脚本(perl、python、bash 等),但这些脚本仅处理选择或整个文件,因此向上或向下移动单行可能有点重量级。
See all the docuemntation on User Scripts here: http://developer.apple.com/mac/library/documentation/DeveloperTools/Conceptual/XcodeWorkspace/310-User_Scrips/user_scripts.html
在此处查看有关用户脚本的所有文档:http: //developer.apple.com/mac/library/documentation/DeveloperTools/Conceptual/XcodeWorkspace/310-User_Scrips/user_scripts.html
I hope this helps!
我希望这有帮助!
回答by Brian Postow
Also, you can hook up emacs (or vi, or bbedit, or ed, I imagine) to xcode, so that they will talk to each other. When you tell xcode to open a file it will call emacsclient, etc. You can even get emacs to tell xcode where to put a breakpoint (I don't remember how, but I asked a question here a while back, and it works great)
此外,您可以将 emacs(或 vi、bbedit 或 ed,我想)连接到 xcode,以便它们相互通信。当你告诉 xcode 打开一个文件时,它会调用 emacsclient 等。你甚至可以让 emacs 告诉 xcode 在哪里放置断点(我不记得是怎么做的,但我不久前在这里问了一个问题,它工作得很好)
回答by stefanB
I think your question is more generic that Xcode/emacs. Most IDE can't do stuff that editors can do and vice versa.
我认为您的问题比 Xcode/emacs 更通用。大多数 IDE 不能做编辑器可以做的事情,反之亦然。
What I do is to use Xcode for 'simple' stuff and compile/debug. When I want to do lot's of coding (hm ... text editing) I open multiple files in one Vim (no offense to emacs ... vim is my editor of choice) and use all the vim tricks available.
我所做的是将 Xcode 用于“简单”的东西并进行编译/调试。当我想进行大量编码(嗯...文本编辑)时,我会在一个 Vim 中打开多个文件(无意冒犯 emacs...vim 是我选择的编辑器)并使用所有可用的 vim 技巧。
I follow the same process when I need to do lots of coding on Windows (Visual Studio). I have VS opened just for compiling/debugging but I do most coding in separate Vim window.
当我需要在 Windows (Visual Studio) 上进行大量编码时,我会遵循相同的过程。我打开 VS 只是为了编译/调试,但我在单独的 Vim 窗口中完成大部分编码。
I don't need to do any of that on Unix because I just open file in Vim and run Makefile from within Vim, jumping directly to errors and so on (:mak) ...
我不需要在 Unix 上做任何这些,因为我只是在 Vim 中打开文件并从 Vim 中运行 Makefile,直接跳转到错误等等 (:mak) ...
I'm sure that there are some extremely clever things to get Xcode do stuff that are already in Vim and Vim to do stuff that Xcode can do but I'm focusing on coding so not much time to play around.
我确信有一些非常聪明的事情可以让 Xcode 做 Vim 中已经存在的东西,而 Vim 可以做一些 Xcode 可以做的事情,但我专注于编码,所以没有太多时间来玩。