在软件开发领域,文本编辑器不仅仅是一个工具,更是程序员的延伸。 就像武术家精准而优雅地挥舞剑一样,程序员也会带着明确的意图,使用他们的文本编辑器,在文本行中进行切割。 将本指南视为您的道场,在这里您将磨练文本操作的精湛技艺。
我们将从导航和选择的基础知识开始,然后再进行一系列的实际练习。 您将获得一些文本,转换该文本的目标,以及在哪里可以找到完成任务所需的文本操作命令的指导。 练习的解决方案将以屏幕截图视频的形式提供,同时提供所用命令的详细分解,使您可以跟随并加深您的理解。
让我们进入导航和选择的基础知识。
步法:光标导航和选择构建块
命令面板是您发现新命令并学习其相关键盘快捷键的最佳资源之一; 使用 cmd-shift-p
打开命令面板。 命令面板允许模糊搜索,因此您可以键入要查找的命令的几个字符,然后快速找到它。

在命令面板中,如果您搜索 editor: move
或 editor: select
,您会找到大量命令 - 用于跳转到文件开头、行尾、单词开头等的命令。您还会看到用于选择下一个单词、下一行、下一个段落等的命令。直接学习所有这些命令既令人畏惧又不必要。相反,我们将专注于学习光标导航和选择的构建块;这些块可以直观地组合以实现更复杂的移动或选择。
方向键
left
: 将光标向左移动right
: 将光标向右移动up
: 将光标向上移动down
: 将光标向下移动
修饰键
cmd
:用于指定跳转到编辑器中的某个“边缘”边界。 例如:cmd-left
跳转到当前行的开头,cmd-up
跳转到文件的开头。 你能猜到cmd-right
和cmd-down
做什么吗?alt
:用于指定在由“单词边界”分隔的文本之间跳转。“单词边界”通常在遇到非字母数字字符(例如空格或标点符号)时出现。例如:alt-left
跳转到当前单词的开头,alt-right
跳转到当前单词的结尾。alt
与ctrl
组合使用时,会在子词之间跳转,这对于在 camelCase 和 snake_case 变量的子词之间跳转很有用。shift
:修改移动以变为选择。 例如:shift-left
选择到左侧,shift-right
选择到右侧,而不是简单地移动。 Shift 可以与其他修饰符组合使用,例如cmd-shift-left
选择到行的开头。
现在您已经学习了光标导航和选择命令的构建块,让我们开始您的训练。 在解决挑战时,结合挖掘命令面板和基于上述构建块的直觉! 尽力避免使用鼠标!
白带挑战
目标
当您一丝不苟地审查队友的拉取请求时,您注意到 JSON 文件中的一个键与用于其他键的命名约定不同。 你的任务是统一密钥以使用相同的命名约定!
你的旅程提示
- 复习光标导航和选择命令。
- 在命令面板中搜索
convert case
。
{
"warrior_name": "Takamura Hajime",
"preferred_weapon": "Yari",
"martial_art_form": "Jujitsu",
"years_of_training": 5,
"isMasterLevelCertified": false,
"dojo_affiliated_region": "Echigo"
}
技巧
剖析
- 导航到
isMasterLevelCertified
的开头。 - 通过
editor: select to next word end
选择isMasterLevelCertified
。 - 运行
editor: convert to snake case
。
磨练你的技巧
Zed 带有许多极其有用的文本操作命令 - 当在不同编程语言的命名约定之间进行转换时,许多命令都非常方便
编辑器: 转换为小驼峰式命名
编辑器: 转换为大驼峰式命名
编辑器: 转换为小写
编辑器: 转换为大写
编辑器: 转换为短横线命名 (kebab case)
编辑器: 转换为蛇形命名 (snake case)
编辑器: 转换为标题式命名 (title case)
编辑器: 转换为相反的大小写
编辑器: 切换大小写
编辑器: 转置
绿带挑战
目标
你正在开发一个很棒的 Python 程序。你注意到你的 train_fighter
函数有一个局部变量 power_increase_factor
是只读的,可以变成一个全局常量。将 power_increase_factor
变量提升到全局作用域,并将它的所有实例重命名为 POWER_INCREASE_FACTOR
,符合 Python 全局常量的约定。
你的旅程提示
- 复习光标导航和选择命令。
- 在命令面板中搜索
move line
。 - 在命令面板中搜索
select all matches
。 - 在命令面板中搜索
convert case
。
def train_fighter(fighter_name, current_power):
power_increase_factor = 1.15
new_power = current_power * power_increase_factor
print(f"After arduous training, {fighter_name} has increased their power to {new_power:.2f}!")
return new_power
def display_fighter_stats(fighter_name, fighter_power):
print(f"{fighter_name}'s Current Power: {fighter_power}")
if __name__ == "__main__":
fighter_name = "Iron Fist"
initial_power = 100
display_fighter_stats(fighter_name, initial_power)
new_power_level = train_fighter(fighter_name, initial_power)
display_fighter_stats(fighter_name, new_power_level)
技巧
剖析
- 在文件开头添加 2 个空行。
- 将光标向下移动到
power_increase_factor...
行的开头。 - 通过
editor: move line up
将该行移动到文件开头。 - 通过
editor: tab prev
删除该行的缩进。 - 通过
editor: select to next word end
选择power_increase_factor
。 - 通过
editor: select all matches
选择power_increase_factor
的所有实例。 - 运行
editor: convert to upper snake case
。
磨练你的技巧
Zed 中有许多可用的行操作命令。 当你有一个列表时,排序项目和删除重复项是可以用几个击键完成的常见任务
编辑器: 删除行
编辑器: 向上复制行
编辑器: 向下复制行
编辑器: 连接行
编辑器: 向上移动行
编辑器: 向下移动行
编辑器: 反转行
编辑器: 随机排列行
编辑器: 不区分大小写排序行
编辑器: 区分大小写排序行
编辑器: 不区分大小写删除重复行
编辑器: 区分大小写删除重复行
在 Zed 中,有很多方法可以将多个光标部署到编辑器; 尝试以下命令 - 所有这些命令在不同的场景中都很有用
search: select all matches
(可以使用缓冲区搜索过滤器进行配置)编辑器: 选择所有匹配项
编辑器: 选择下一个
编辑器: 将选择拆分为行
编辑器: 在上方添加选择
编辑器: 在下方添加选择
编辑器: 撤消选择
- 基于列的选择 -
alt-shift-mouse-drag
棕带挑战
目标
你发现了一个古老的卷轴(一个 CSV 文件),其中列出了关于历史武术大师的信息。 CSV 文件有一些不一致之处,需要更正。 你必须清理 CSV 文件。 删除引号并将所有字符串大写。
你的旅程提示
- 复习光标导航和选择命令。
- 复习行操作命令。
- 复习多光标命令。
id,first_name,last_name,style,birth,death,country
1,"Miyamoto","Musashi","Swordsmanship",1584,1645,"japan"
2,yamamoto,tsunetomo,"Hagakure",1659,1719,"Japan"
3,"Ip","Man","wing Chun",1893,1972,"china"
4,morihei,ueshiba,Aikido,1883,1969,"Japan"
5,"Gichin","funakoshi","karate",1868,1957,"Japan"
技巧
剖析
- 导航到第一个
"
。 - 通过
editor: select right
选择"
。 - 通过
editor: select all matches
选择"
的所有实例。 - 通过
editor: delete
删除"
。 - 通过
editor: cancel
退出多光标模式。 - 通过
editor: select left
选择前面的,
。 - 选择
,
的所有实例。 我们不会在这里使用editor: select all matches
,因为我们不想选择分隔列名称的逗号。 相反,我们将按住editor: select next
的绑定并重复选择下一个匹配项。 请注意,如果我们选择的逗号太多,我们可以通过editor: undo selection
撤消选择。 - 通过
editor: move right
消除选择。 - 通过
editor: select to next word end
选择直接位于每个逗号前面的所有单词。 请注意,我们最终也会选择一些整数,但这在这种情况下无关紧要。 - 通过
editor: convert to title case
将所选文本转换为标题式命名。
黑带挑战
目标
从下面的文本中提取武术演员的姓名到一个 Markdown 列表,其中每个姓名都大写,并放入 <last>, <first>
格式,并按字母顺序排序。
你的旅程提示
- 复习光标导航和选择命令。
- 复习行操作命令。
- 复习多光标命令。
# Iconic Kung Fu Actors
## bruce lee
Bruce Lee's portrayal of "Lee" in the 1973 martial arts film _Enter the Dragon_ is legendary. This character showcased Lee's exceptional martial arts skills, his philosophy of Jeet Kune Do, and his incredible physical prowess. The film cemented Bruce Lee's status as a global icon and greatly influenced the perception of Asian and martial arts culture in the Western world.
## jackie chan
Jackie Chan brought a new dimension to the martial arts film genre with his role as Wong Fei-hung in the 1978 film _Drunken Master_. Chan's unique blend of impressive martial arts and comedic timing created a refreshing and influential approach to the genre, highlighting the traditional drunken fist fighting style and establishing Chan as a major star in both Asia and the West.
## jet li
In the 1991 film _Once Upon a Time in China_, Jet Li portrays Huo Yuanjia, a revered martial arts master and founder of the Chin Woo Athletic Association. Li's performance was widely praised for its combination of grace, agility, and martial arts expertise, offering a narrative that explored Chinese nationalism and the importance of martial arts in cultural identity.
## donnie yen
Donnie Yen's role as Ip Man, the Wing Chun grandmaster, and teacher of Bruce Lee, in the 2008 film _Ip Man_, is considered one of his best performances. Through this role, Yen was able to showcase the skill, philosophy, and humanity of Ip Man, portraying significant events in his life against the backdrop of the Sino-Japanese War. The film series has become a classic, highlighting the spirit of Wing Chun and its significance in martial arts history.
## michelle yeoh
Michelle Yeoh's portrayal of Yu Shu Lien in the 2000 film _Crouching Tiger, Hidden Dragon_ brought her international acclaim. Her character is a skilled warrior who navigates a complex plot of love, honor, and betrayal. Yeoh's performance, alongside her martial arts skill, played a significant role in the film's success, demonstrating the power and depth of female characters in the martial arts genre.
技巧
剖析
- 向下导航到第一个
##
标题。 - 通过
editor: select right
选择##
。 - 通过
editor: select all matches
选择##
的所有实例。 - 通过
editor: move right
消除选择。 - 通过
editor: select to end of line
选择到每行末尾。 - 通过
editor: copy
复制每个选择。 - 通过
editor: move to end
导航到文档末尾。 - 通过
editor: newline
插入一些新行。 - 通过
editor: paste
粘贴复制的文本。 - 通过
editor: select to beginning of line
选择最后一行。 - 通过多次使用
editor: select up
向上扩展选择范围到第一个名字。 - 运行
editor: split selection into lines
在每行末尾添加一个光标。 - 通过
editor: move to previous word start
跳转到每个名字之间的空格。 - 通过
editor: delete
删除空格。 - 通过
editor: select to next word end
选择所有姓氏。 - 通过
editor: copy
复制姓氏。 - 通过
editor: delete
删除姓氏。 - 通过
editor: move to beginning of line
跳回到每行开头。 - 通过
editor: paste
粘贴姓氏。 - 在每个名字之后添加
,
。 - 通过
editor: move to beginning of line
跳回到每行开头。 - 通过
editor: select to end of line
选择到每行末尾。 - 通过
editor: convert to title case
将所选文本转换为标题式命名。 - 通过
editor: move to beginning of line
跳回到每行开头。 - 在每个名字之前插入
- [ ]
。 - 通过
editor: move to end of line
跳转到每行末尾。 - 通过
editor: select to beginning of line
选择每一行。 - 通过
editor: sort lines case sensitive
对行进行排序。
审查选择
有时,您可能需要在进行转换之前审查所有选择,以确保您的某个选择没有捕获到不应该捕获的内容。 您当然可以滚动浏览当前文件来审查您的选择,但如果您的选择稀疏且分散在长文件中,这可能会很繁琐。 一个方便的操作是 editor: open selections in multibuffer
,它会将您的所有选择提取到一个紧凑的多缓冲区视图中。 从多缓冲区中,您可以更轻松地审查您的选择、进行编辑并保存您的更改。
结论
转换文本既有趣又实用。 通过寻求需要尽可能少步骤的解决方案,该过程可以“游戏化”(您可能已经发现了比建议的练习更直接的解决方案)。 但是,并非所有转换都像练习中那样清晰。 例如,通常一些重复的编辑任务无法通过多个光标一次性完全解决,从而留下一些手动工作需要执行。 在这些情况下,目标是为文本找到最有效的状态,从而最大限度地减少手动跟进。
我们希望您能够掌握一些新的技巧和技术,以添加到您的文本操作武器库中。 请记住,练习越多,您就越熟练。 所以,继续磨练你的技巧! 随时 与我们分享 您已部署的任何有趣的文本处理技巧!