Ever on a beautiful day, our computer suddenly died. And then we have to reinstall a new operating system, all software and settings for old programs are lost? We spend hours (maybe a whole day) just sitting and reinstalling those programs, then fumbling for the settings again? If you have encountered such cases, DOTFILES is the solution for this case.
1. Concepts
We can understand that Dotfiles is simply a collection of all files with dots at the beginning. However, those files contain the configs of a particular program or system.
These files appear a lot in everyday programming work. Set the environment variable then set in ~ / .bashrc, set ignore git then add gitignore file, … The name “Dotfiles” also starts from the fact that these files always have a “.” at the beginning. It will be hidden by default when we open the folder.
Why should I use Dotfiles?
Even if we don’t know Dotfiles, we can still use a normal computer, so there is no need for Dotfiles. Moreover, even if we have to reinstall the computer, we still have plenty of time to be able to reset the computer, is it necessary to know Dotfiles?
The answer is no. However, Dotfile will bring you a lot of interesting things:
- Help automate everything possible
- Create your own config
- Help learn new things from other people’s configs
In programming there is 1 principle DRY – Don’t repeat yourself – Never repeat something. So make everything automatic. Never re-type config lines, software installation commands, … while we can make it so much simpler. Dotfiles makes it easy to backup, restore or synchronize settings between devices.
In addition, Dotfiles is the place for us to unleash our creativity. We can put in it our stylish configs. Self-installing, key mapping, and adding extend functions will create programs in accordance with our routine and purpose of using the program. This will give you the feeling of being able to control everything as you like, creating a machine for yourself.
During the installation process, we will learn a lot more about how an application will run, learn interesting configs from other people’s dotfiles repo, how to spot problems. problems and solutions, … Solving problems for yourself is a great way to learn new things
2. Structure
Below is an example of the structure of Dotfiles. We will learn about each of its ingredients
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | . ├── git │ ├── .gitconfig │ └── .gitignore_global ├── install.sh ├── osxdefaults.sh ├── runcom │ ├── .bash_profile │ └── .inputrc └── system ├── .alias ├── .env ├── .function ├── .path └── .prompt |
.bash_profile
In the Bash shell, this file will be loaded before the Terminal loads its environment, which will contain all of our config, command lines. In this file, we can change various settings such as terminal commands, add aliases to frequently used functions …
.inputrc
Actions on input, key press will be saved in this file. For example
1 2 3 4 5 6 7 8 9 10 | # Phím Tab sẽ tự động hoàn thành không quan tâm đến chữ hoa hay chữ thường của tên file set completion-ignore-case on # List tất cả các tên trong trường hợp có nhiều case set show-all-if-ambiguous on # Chuyển giữa các match với Shift-Tab "e[Z": menu-complete # Filter lịch sử search "e[A": history-search-backward "e[B": history-search-forward |
.alias
This file will define shorcuts for the commands. For example:
1 2 3 4 5 6 7 8 | alias l="ls -la" alias ld="ls -ld */" alias ..="cd .." alias ...="cd ../.." alias ....="cd ../../.." alias cleanupds="find . -type f -name '*.DS_Store' -ls -delete" |
.functions
If the statement is too complex to create an alias, we can define a function. A function can take parameters at will.
1 2 3 4 5 6 7 8 9 | # Function tạo mới một thư mục và truy nhập vào đường dẫn thư mục đó function mk() { mkdir -p " <a class="__cf_email__" href="/cdn-cgi/l/email-protection">[email protected]</a> " && cd " <a class="__cf_email__" href="/cdn-cgi/l/email-protection">[email protected]</a> " } # Function mở một page dưới dạng pdf function manpdf() { man -t "${1}" | open -f -a /Applications/Preview.app/ } |
.env
Contains variables usable for other files
1 2 3 4 5 6 7 8 9 | export PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:$DOTFILES_DIR/bin" export EDITOR="subl -w" export CLICOLOR=1 export LSCOLORS=gxfxcxdxbxegedabagacad # highlight match khi dùng grep export GREP_OPTIONS='—color=auto' # Tự động sửa sai path name khi dùng cd shopt -s cdspell |
Other files
There are many other files that can be used in dotfiles such as:
- .gitconfig for Git
- .vimrc for Vim
3. How to customize Dotfiles
Read dotfiles written from others
Each person’s dotfiles have many interesting and useful configs, specific to their work and usage habits. We can read files from others to learn which config is right for us. For example a file is written:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | FZF_TMUX_HEIGHT=20 # Open file with Vim v() { local file file=$(fzf --query="$1") && vim "$file" } # cd to folder fd() { local dir dir=$(find ${1:-*} -path '*/.*' -prune -o -type d -print 2> /dev/null | fzf +m) && cd "$dir" } # show all hidden folder to 'cd' fda() { local dir dir=$(find ${1:-.} -type d 2> /dev/null | fzf +m) && cd "$dir" } # Search in history fh() { eval $(history | fzf +s | sed 's/ *[0-9]* *//') } # Kill a process fk() { ps -ef | sed 1d | fzf -m | awk '{print $2}' | xargs kill -${1:-9} } # Checkout a branch fbr() { local branches branch branches=$(git branch) && branch=$(echo "$branches" | fzf +s +m) && git checkout $(echo "$branch" | sed "s/.* //") } # Checkout a commit fco() { local commits commit commits=$(git log --pretty=oneline --abbrev-commit --reverse) && commit=$(echo "$commits" | fzf +s +m -e) && git checkout $(echo "$commit" | sed "s/ .*//") } # Search tags ft() { local line [ -e tags ] && line=$(grep -v "^!" tags | cut -f1-3 | cut -c1-80 | fzf --nth=1) && $EDITOR $(cut -f2 <<< "$line") } # fq1 [QUERY] # - Immediately select the file when there's only one match. # If not, start the fuzzy finder as usual. fq1() { local lines lines=$(fzf --filter="$1" --no-sort) if [ -z "$lines" ]; then return 1 elif [ $(wc -l <<< "$lines") -eq 1 ]; then echo "$lines" else echo "$lines" | fzf --query="$1" fi } # fe [QUERY] # - Open the selected file with the default editor # (Bypass fuzzy finder when there's only one match) fe() { local file file=$(fq1 "$1") && ${EDITOR:-vim} "$file" } |
In this file there are many interesting functions, we can easily execute a command without doing any lengthy operations. All of the above functions will be called as a zsh alias.
Write other configs
We can completely write the config for ourselves
For example, the code for today’s weather check in Hanoi:
1 2 3 4 5 | weather() { local CITY=${1:-Hanoi} curl -4 "wttr.in/$CITY" } |
or quickly translate 1 word by google if you install translate-shell :
1 2 3 | # Translate with google alias t="trans -b :vi" |
or quick lookup on stackoverfolow from terminal with howdoi
1 2 3 | # How do I .... alias how="howdoi" |
Thus, the article has learned the basics of Dotfiles and some ways to use it. Hopefully the article will help everyone to create their own repo Dotfiles, best serve their work and their habits of using computers. See you!
Reference
https://medium.com/@webprolific/getting-started-with-dotfiles-43c3602fd789#.hjk1r1vls
https://www.freecodecamp.org/news/dive-into-dotfiles-part-1-e4eb1003cff6/