Dotfiles简介:如何控制开发环境

by Mohammed Ajmal Siddiqui

由Mohammed Ajmal Siddiqui

Dotfiles简介:如何控制开发环境 (An introduction to Dotfiles: how to take control of your development environment)

Note: This is a very basic, introductory article. If you already know the fundamentals of dotfile management, I’d recommend you read my second article.

注意:这是一篇非常基础的入门文章。 如果您已经了解点文件管理的基础知识,建议您阅读第二篇文章

As developers, we strive to minimize the time we spend on redundant things, like setting up our environment, writing boilerplate code, and basically not doing anything that does not concern the fun part of coding - building new stuff.

作为开发人员,我们努力减少在冗余事情上花费的时间,例如设置环境,编写样板代码,并且基本上不做任何与编码有趣的部分无关的事情-构建新的东西。

In this context, imagine a perfect world where tiny commands carry out incredibly complex tasks tailored to your needs, where you could buy a new laptop today and install all the tools and packages you need and setup your development environment with nothing but a couple of terminal commands, and where everything is magic.

在这种情况下,请想象一个完美的世界,其中微小的命令可以执行根据您的需求量身定做的极其复杂的任务,您今天可以购买一台新笔记本电脑,安装所需的所有工具和软件包,并仅需几个终端即可设置开发环境命令,凡事都是魔术。

This digital fairyland can be made, and with ease. And there is a name for this magic: dotfiles.

可以轻松地制作这个数字仙境。 这个魔术有一个名字:dotfiles。

Without further ado, let’s unravel the secrets of the dotfiles!

事不宜迟,让我们来揭开点文件的秘密!

介绍 (Introduction)

Note: This article assumes that you’re working with a Unix-like operating system and it relies heavily on Unix terminal commands and shell scripting. If you’re not familiar with these, I recommend learning the basics and coming back here. Here’s a primer to shell scripting.

注意:本文假定您正在使用类似Unix的操作系统,并且它在很大程度上依赖Unix终端命令和Shell脚本。 如果您不熟悉这些内容,建议您学习基础知识,然后再回到这里。 这是 shell脚本入门。

In UNIX-like systems, a lot of configuration files and the like are preceded with a dot(.). These files are hidden by the OS by default, and even the ls command doesn’t reveal their presence (we’ll get to how to find these files in a bit). Since these files are preceded by a dot, they’re called dotfiles. Duh.

在类似UNIX的系统中,许多配置文件等都带有dot(。)。 这些文件默认情况下由操作系统隐藏,甚至ls命令也无法显示它们的存在(稍后我们将介绍如何查找这些文件)。 由于这些文件前面带有点,因此它们被称为点文件。 咄。

So how do we find these legendary files if they’re hidden by default? Pop open a terminal and do this:

那么,如果这些传奇文件默认情况下处于隐藏状态,我们如何找到它们? 弹出一个终端,然后执行以下操作:

Note: The “$” sign is not meant to be typed in the terminal. It represents the fact that the text after it is supposed to be typed in a terminal prompt.
注意:“ $”符号并非要在终端中键入。 它表示以下事实:应该在终端提示符下键入该文本。
$ cd ~$ ls -a

So what does this do?

那么,这是做什么的呢?

The first command ( cd ~ ) moves into the home directory (the “~” symbol represents the home directory). The home directory is where most of your config files are found. So we move there first.

第一个命令( cd ~ )移动到主目录(“〜”符号表示主目录)。 主目录是找到大多数配置文件的位置。 所以我们先搬到那里。

The second command lists the files and folders in the current directory. But there’s some magic here. The -a flag instructs the command to include hidden files in the list.

第二个命令列出当前目录中的文件和文件夹。 但是这里有些魔术。 -a标志指示命令将隐藏文件包括在列表中。

Bingo! We can now see the dotfiles!

答对了! 现在我们可以看到点文件了!

修改.bash_profile (Modifying the .bash_profile)

Usually, the first file that most people modify when they enter the world of dotfiles is the .bash_profile or the .bashrc. And for good reason. This file is loaded when you start your terminal, and its commands are executed at terminal startup.

通常,大多数人进入dotfiles世界时修改的第一个文件是.bash_profile.bashrc 。 并且有充分的理由。 启动终端时将加载此文件,并且在终端启动时将执行其命令。

One reason why you might want to modify your .bash_profile is to customize the look of your terminal (to be specific, your terminal prompt). This is an art and a science in itself and probably should have an entire book dedicated to it, so I won’t cover this topic much in this article. You can get started with customizing your prompt with this article.

您可能想要修改.bash_profile原因之一是自定义终端的外观(具体来说,是终端提示)。 这本身就是一门艺术和一门科学,可能应该有一整本专门针对它的书,因此在本文中我不会过多地讨论这个主题。 你可以开始使用自定义提示与文章。

Instead, let’s look at two common shell constructs that are perhaps among the most important and useful parts of dotfiles: aliases and functions.

取而代之的是,让我们看一下可能是点文件最重要和最有用的部分的两种常见的shell构造:别名和函数。

别名 (Aliases)

Aliases are simply short names/acronyms that you can assign to a longer sequence of commands in order to reduce how long you take to type it and thus increase your speed. For example, almost every developer uses git. Anyone who uses the git CLI (and let’s face it - you should be using the git CLI), probably has used long commands like these:

别名只是简称/缩写,您可以将其分配给较长的命令序列,以减少键入命令所需的时间,从而提高速度。 例如,几乎每个开发人员都使用git。 使用git CLI的任何人(让我们面对现实-您应该使用git CLI)可能已经使用了以下长命令:

// commit changes$ git commit -m "some changes"
// push changes to the master branch of origin$ git push origin master

These commands are quite a bit to type. If you think they’re not, you will change your mind after you start using aliases.

这些命令要键入很多。 如果您认为它们不是,则在开始使用别名后您会改变主意。

Type the following in your shell prompt:

在您的shell提示中输入以下内容:

$ alias gpom='git push origin master'

Now when you type gpom, git push origin master is executed! You’ve gone from 4 words to 4 letters! ?

现在,当您键入gpomgit push origin master执行git push origin master ! 您已经从4个单词变成了4个字母 ! ?

But there’s a problem. Close your terminal, restart it, and try gpom again. Your alias is gone! This is because the alias is defined for the current terminal session.

但是有一个问题。 关闭您的终端,重新启动,然后再次尝试gpom 。 您的别名不见了! 这是因为别名是为当前终端会话定义的。

So how do we get around this and make our aliases stick?

那么我们如何解决这个问题并保持别名不变呢?

Remember we talked about a file whose commands are executed when a terminal is started? Bingo!

还记得我们谈论过的文件,该文件的命令在终端启动时执行吗? 答对了!

Add the following line to your .bash_profile or .bashrc and save it:

.bash_profile下行添加到.bash_profile.bashrc并保存:

alias gpom='git push origin master'

Now, whenever you start a bash terminal, the above alias is created. Life is already starting to get awesome!

现在,无论何时启动bash终端,都会创建上述别名。 生活已经开始变得很棒!

Note: You can use the nano text editor to edit your text files. When in the home directory, type nano .bash_profile to open the file using nano, make your changes, and save the file by hitting Ctrl+X and then y when prompted. Vim is another text editor you can use.

注意:您可以使用nano文本编辑器来编辑文本文件。 在主目录中时,键入nano .bash_profile以使用nano打开文件,进行更改,然后按Ctrl+X ,然后在出现提示时Ctrl+X y ,以保存文件。 Vim是可以使用的另一个文本编辑器。

Since aliases essentially replace the full command, you can aliases as part of a common multi command CLI tool like git to make all its commands easier. Just add this to your .bash_profile:

由于别名从本质上代替了完整命令,因此您可以将别名作为常见的多命令CLI工具(如git)的一部分使用,以简化其所有命令。 只需将其添加到您的.bash_profile

alias g='git'

And you can type “g” instead of “git” wherever you want to use “git”. Sweet!

您可以在要使用“ git”的任何地方键入“ g”而不是“ git”。 甜!

Here are a few common aliases you might wanna use:

以下是您可能要使用的一些常用别名:

alias home='cd ~'alias ..='cd ..'alias '?=man'# Git CLI aliasesalias g='git'alias gi='git init'alias gra='git remote add'alias gs='git status'...# Aliases for NPMalias nr='npm run'alias ni='npm install'alias nid='npm install -D'...
功能 (Functions)

Aliases can go a long way in improving our workflow, but there’s one thing they can’t do: work with arguments.

别名可以大大改善我们的工作流程,但它们不能做的一件事:使用参数。

Let’s say you were tired of executing two commands to make a new directory and cd into it:

假设您已经厌倦了执行两个命令来创建一个新目录并将其放入cd

$ mkdir new_folder$ cd new_folder

And you wanted to make an alias for this. But you can’t, since both mkdir and cd take arguments, and you can’t pass arguments to aliases.

您想为此做一个别名。 但是您不能这样做,因为mkdircd接受参数,并且不能将参数传递给别名。

So what now? Remember, there’s a super common programming construct that takes arguments? Yup, functions! Shell scripts can have functions that can take arguments. Awesome! If you’re a little rusty with functions in shell scripts, here’s a little reminder.

所以现在怎么办? 还记得吗,有一个超级通用的编程构造接受参数? 是的,功能! Shell脚本可以具有可以带参数的函数。 太棒了! 如果您对Shell脚本中的函数有些生疏, 这里有个提醒。

You can turn the above sequence into a shell function like this (this example was taken from the dotfiles of mathiasbynens, who has some of the most popular dotfiles around. Other people with excellent dotfiles to refer to are listed and linked to at the end of the article):

您可以将上面的序列转换成这样的shell函数(此示例摘自mathiasbynens的点文件 ,后者周围有一些最受欢迎的点文件。其他人引用的优秀点文件已列出并链接到该文件的末尾)。文章):

# Create a new directory and enter itfunction mkd() {     mkdir -p "$@" && cd "$_";}

Again, you can put this in your .bash_profile and the function will be accessible during any terminal session.

同样,您可以将其放在.bash_profile并且在任何终端会话期间都可以访问该函数。

Note: You’ll have to restart your terminal for any changes to your .bash_profile to take effect. If this is a chore, run source .bash_profile to add your changes to the current terminal session. Even better, in the spirit of dotfiles, make an alias like alias reload='source .bash_profile'!

注意:您必须重新启动终端, .bash_profile任何更改才能生效。 如果这很麻烦,请运行source .bash_profile将更改添加到当前终端会话中。 更好的是,本着dotfile的精神,创建一个别名,例如alias reload='source .bash_profile'

点文件和共享 (Dotfiles and Sharing)

Why do people commit their development environments — their dotfiles — to version control? Why do they put it up on GitHub for everyone to see? Same reason as always: to track how your dotfiles evolve over time and, most importantly, to share your dotfiles and inspire other people.

人们为什么将其开发环境(其点文件)提交给版本控制? 他们为什么将它放在GitHub上以供所有人查看? 一如既往的理由:跟踪您的点文件随时间变化的方式,最重要的是, 共享您的点文件并启发其他人

If you look at any mature dotfiles repo, you’ll realize that there are always snippets taken from other dotfiles repos and the like. They may even have multiple contributors and maintainers. We share dotfiles to collectively help each other build better environments and workflows.

如果您查看任何成熟的dotfiles存储库,您会发现总是有从其他dotfiles存储库等中获取的摘要。 他们甚至可能有多个贡献者和维护者。 我们共享点文件以共同帮助彼此构建更好的环境和工作流程。

This also allows people to use features of version control to make each others’ dotfiles better. One example of this is using the GitHub Issue Tracker to discuss issues and improvements.

人们还可以使用版本控制的功能来改善彼此的点文件。 其中一个示例是使用GitHub Issue Tracker讨论问题和改进。

I was inspired to work on my dotfiles from the dotfiles of pradyunsg, who has an impressive dotfiles repo of his own.

启发我从pradyunsg的dotfiles中处理我的dotfiles, pradyunsg自己拥有一个令人印象深刻的dotfiles回购。

My own dotfiles are fairly basic and very immature right now, and they’ll get better over time. But this also means that beginners in the world of dotfiles will be less intimidated when they check the repo out.

我自己的dotfile现在还很基本,还很不成熟,随着时间的推移它们会变得更好。 但这也意味着,点文件世界中的初学者在检查回购协议时将不会受到威胁。

Like many other people, I’ve added some support for customization of my dotfiles, so it might be a good idea for people who are new to the idea of dotfiles to fork the repo and try making it their own. More details in the repository. Do check them out and give me feedback!

像许多其他人一样,我添加了一些对自定义dotfile的支持,因此对于那些不熟悉dotfile的人来说,最好将仓库分叉并尝试制作自己的仓库。 在存储库中有更多详细信息 。 请检查一下并给我反馈!

Here’s a list of a people whose dotfiles are much more expansive and might inspire you:

以下是其点文件扩展得多并可能启发您的人们的列表:

结论 (Conclusion)

These are the very fundamentals of creating your development environment using dotfiles. However, there is a lot more to this, which we’ll continue looking at in the next article in this series.

这些是使用点文件创建开发环境的基础。 但是,这还有很多,我们将在本系列的下一篇文章中继续讨论

Some of the topics we’ll look at in the next article in the series are:

在本系列的下一篇文章中,我们将讨论一些主题:

  • Creating an environment to organize, track and painlessly work with dotfiles

    创建一个组织,跟踪和轻松处理点文件的环境
  • Splitting our dotfiles to make managing them easier and more scalable (notice it is dotfiles, not dotfile)

    拆分我们的点文件,使它们的管理更加轻松和可扩展(注意,这是点文件 ,而不是点文件 )

  • Writing scripts to setup (bootstrap) a new system with our dotfiles

    编写脚本以使用我们的dotfile设置(引导)新系统
  • Making our dotfiles easy to share by adding support for customization

    通过添加对自定义的支持,使我们的点文件易于共享

That concludes the first part of the series on dotfiles! Here’s a link to the next one.

到此结束了关于点文件系列的第一部分! 这是下一个的链接。

I loved the idea of dotfiles so much that it inspired me to create a basic dotfile management framework - autodot. The framework is in its infancy, so I’m looking for enthusiastic people who can give me feedback for the framework, contribute to it by telling me about bugs and making feature requests, and contribute to the code and documentation. Do take some time out for this! :)

我非常喜欢点文件的想法,以至于启发了我创建一个基本的点文件管理框架-autodot 。 该框架尚处于起步阶段,因此我正在寻找热心的人,他们可以为我提供有关该框架的反馈,通过告诉我有关错误和提出功能要求的方式为框架做出贡献,并为代码和文档做出贡献。 为此花点时间! :)

ajmalsiddiqui/autodotautodot - A dotfile management system that makes sharing your dotfiles easy while keeping you in the loop.github.com

ajmalsiddiqui / autodot autodot-一个点文件管理系统,可以轻松共享您的点文件,同时保持循环。 github.com

Also, connect with me on GitHub and LinkedIn.

另外,在GitHubLinkedIn上与我联系。

Good luck and Happy Coding! :)

祝您好运,编码愉快! :)

翻译自: https://www.freecodecamp.org/news/dive-into-dotfiles-part-1-e4eb1003cff6/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值