在软件开发领域,文本编辑器不仅仅是一个工具,更是程序员的延伸。如同武术家精确优雅地挥舞刀剑,程序员也熟练地驾驭他们的文本编辑器,以深思熟虑的意图切割文本行。请将本指南视为您的道场,一个磨练文本操作精湛技艺的地方。
我们将从导航和选择的基础知识开始,然后进行一系列实践练习。您将获得一些文本、一个转换该文本的目标,以及关于在哪里可以找到完成当前任务所需的文本操作命令的指导。练习的解决方案将以屏幕录制视频的形式提供,并附带所用命令的详细分解,让您可以跟随学习并加深理解。
让我们深入了解导航和选择的基础知识。
步法:光标导航和选择的基础
命令面板是您发现新命令和学习其关联键盘快捷键的最佳资源之一;使用 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 附带了一些极其有用的文本操作命令——其中许多在不同编程语言的命名约定之间进行转换时非常方便
editor: convert to lower camel case(转换为小驼峰命名)editor: convert to upper camel case(转换为大驼峰命名)editor: convert to lower case(转换为小写)editor: convert to upper case(转换为大写)editor: convert to kebab case(转换为烤串命名)editor: convert to snake case(转换为蛇形命名)editor: convert to title case(转换为标题大小写)editor: convert to opposite case(转换为相反大小写)editor: convert to sentence case(转换为句子大小写)editor: toggle case(切换大小写)editor: transpose(转置)
绿带挑战
目标
您正在编写一个出色的 Python 程序。您注意到您的 train_fighter 函数有一个局部变量 power_increase_factor,它是只读的,可以将其转换为全局常量。根据 Python 全局常量的约定,将 power_increase_factor 变量提升到全局作用域,并将其所有实例重命名为 POWER_INCREASE_FACTOR。
您的旅程提示
- 复习光标导航和选择命令。
- 在命令面板中搜索
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 中有许多可用的行操作命令。当您有一个列表时,排序和删除重复项是常见的任务,可以通过几次击键完成
editor: delete line(删除行)editor: duplicate line up(向上复制行)editor: duplicate line down(向下复制行)editor: join lines(合并行)editor: move line up(向上移动行)editor: move line down(向下移动行)editor: reverse lines(反转行)editor: shuffle lines(打乱行)editor: sort lines case insensitive(按行不区分大小写排序)editor: sort lines case sensitive(按行区分大小写排序)editor: unique lines case insensitive(按行不区分大小写去重)editor: unique lines case sensitive(按行区分大小写去重)
在 Zed 中有多种方法可以将多个光标部署到编辑器中;尝试以下命令——所有这些在不同场景下都很有用
search: select all matches(可配置缓冲区搜索过滤器)editor: select all matches(选择所有匹配项)editor: select next(选择下一个)editor: split selection into lines(将选择拆分为行)editor: add selection above(在上方添加选择)editor: add selection below(在下方添加选择)editor: undo selection(撤消选择)- 基于列的选择 -
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 列表,其中每个名字都大写,格式为 <姓>, <名>,并按字母顺序排序。
您的旅程提示
- 复习光标导航和选择命令。
- 复习行操作命令。
- 复习多光标命令。
# 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,它会将您的所有选择拉入一个紧凑的多缓冲区视图。从多缓冲区中,您可以更轻松地查看您的选择,进行编辑,并保存更改。
结论
文本转换既有趣又实用。这个过程可以通过寻找最少步骤的解决方案来“游戏化”(您可能已经为练习发现了比建议的更直接的解决方案)。然而,并非所有转换都像练习中那样清晰明了。例如,通常某些重复的编辑任务无法通过多个光标一蹴而就地完全解决,需要之后进行一些手动工作。在这种情况下,目标是找到最有效的文本状态,以最大限度地减少后续的手动操作。
我们希望您能够学习到一些新的技巧和技术,以充实您的文本操作工具库。请记住,练习越多,您就会越熟练。所以,请继续磨砺您的掌握!欢迎 与我们分享 您部署的任何有趣的文本操作!