[LINUX] How to grow dotfiles

Grow dotfiles!

dotfiles have an inseparable relationship with engineer life. I would like to introduce how to grow such dotfiles into the ideal shape.

What are dotfiles A general term for files that start with `.` (dot). On Unix-like OS, files starting with `.` are treated as ** hidden files **, and are not displayed unless options are specified for the` ls` command etc. [^ 1]. Since ancient times, there has been a culture of using this feature to place configuration files starting with `.` directly under your home directory. For example, if you execute `ls` in my home directory, the output will be as follows. ![dotfiles01.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/214526/90e5f04c-45c4-0c2a-3d8a-1f0b3596b43e.png) The result of executing with the `-a` option that also displays hidden files is as follows. ![dotfiles02.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/214526/11701cbd-88fa-541e-c092-83171267eb15.png) A large number of files starting with `.` are displayed. In short, these are dotfiles.

Let's manage with Lv.1 Git

As a first step, let's start by managing dotfiles with Git. There are various benefits such as history management and improved portability. (Also grow grass)

Create a repository called dotfiles and commit various dotfiles such as .bashrc under the repository. Use the ln command to the home directory [symbolic link](https://ja.wikipedia.org/wiki/%E3%82%BD%E3%83%95%E3%83%88%E3%83" % AA% E3% 83% B3% E3% 82% AF). ** By creating a symbolic link, files in the repository can be centrally managed, making it easy to update and update. ** **

.Example of creating a symbolic link in bashrc


#Creating a link
$ cd path/to/dotfiles
$ ln -s .bashrc ~/.bashrc

#Check the link
$ ls -l ~/.bashrc
lrwxrwxrwx 1 reireias reireias 59 June 8 22:30 /home/reireias/.bashrc -> /home/reireias/dev/src/github.com/reireias/dotfiles/.bashrc

There are quite a lot of dotfiles repositories published on GitHub. https://github.com/topics/dotfiles

** By the way, be careful not to disclose authentication information such as ʻAWS_ACCESS_KEY_ID` **

Let's make Lv.2 deploy easy

Creating a symbolic link is done only once for that environment, but as the number of target dotfiles increases, it is the nature of the engineer that he wants to make the work more efficient.

It can be implemented with a simple shell script. You can implement it with something other than a shell script, but shell scripts that can be used in most environments are often used because it is easier to install in a clean environment with fewer packages.

The following script will create symbolic links for all files starting with . directly under the dotfiles repository in your home directory.

install.sh


#!/bin/bash -e

IGNORE_PATTERN="^\.(git|travis)"

echo "Create dotfile links."
for dotfile in .??*; do
    [[ $dotfile =~ $IGNORE_PATTERN ]] && continue
    ln -snfv "$(pwd)/$dotfile" "$HOME/$dotfile"
done
echo "Success"

Let's write Lv.3 installation script

Some dotfiles may require you to install packages and libraries.

For example

--There is .tmux.conf, so I want to install tmux --A self-made function that uses the peco command is defined in .bashrc. --A particular plugin for vim depends on a python library

And so on.

Initially, you may have a list of dependent packages in the README. After all it is an engineer to automate.

For example, in the case of shell script, simply enumerating the commands as shown below will make a good installation script.

An example of an installation script


#!/bin/bash

brew install tmux peco neovim
pip install neovim

Be aware of the idempotency of Lv.4 installation scripts

Let's make the installation script conscious of idempotence. By ensuring that the same result is obtained no matter how many times it is executed, it is possible to execute the installation script for the existing environment even if a new process to be performed by the installation script is added.

A simple example is shown below.

#No idempotence
mv config /etc/hoge/config
echo "some setting" >> ~/.hogerc

#Idempotent
cp config /etc/hoge/config
if ! grep -q "^some setting$" ~/.hogerc; then
  echo "some setting" >> ~/.hogerc
fi

The larger the installation script, the more difficult it is to ensure equivalence with a shell script, so Ansible and [Chef]( Writing an installation script using a configuration management tool such as https://www.chef.io/) is also a good choice.

However, if you start using a tool other than shell script, you need to be careful because the procedure for installing in a clean environment will increase a little.

Let's turn Lv.5 CI

Both cats and scoops [CI](https://ja.wikipedia.org/wiki/%E7%B6%99%E7%B6%9A%E7%9A%84%E3%82%A4%E3%83%B3 % E3% 83% 86% E3% 82% B0% E3% 83% AC% E3% 83% BC% E3% 82% B7% E3% 83% A7% E3% 83% B3) It is a world. Let's also turn CI in dotfiles.

For scripts that install the latest version without specifying the version of the package or library to install, incompatible updates may cause the installation script to fail. It's a good idea to run the installation script and check if the files are placed properly.

Here's a very simple sample code on GitHub Actions.

yaml:.github/workflows/main.yml


---
name: main

on: [push]

jobs:
  main:
    runs-on: ubuntu-latest # or macOS-latest
    steps:
      - uses: actions/checkout@v1
      - name: install dotfiles
        run: bash install.sh
      - name: test
        run: # some tests

Let's split the Lv.6 file

As dotfiles grow steadily, .bashrc (.zshrc) and .vimrc become quite huge. Engineers usually consider a huge file to be evil due to conditional reflection, so I want to divide it somehow.

Fortunately, .bashrc and .vimrc have a function to read another file, so it is easy to divide.

For example, you can read another file from .bashrc as follows.

.bashrc


source ${HOME}/.bash/keybind.sh
source ${HOME}/.bash/alias.sh

Let's maintain the Lv.7 README

dotfiles also grow when seen by humans (?) Develop a README and make it a cool dotfiles repository.

Personally, if the following is described, I want to refer to it ~~ plagiarism ~~.

--Target editors, shells, tools --Terminal screenshot --Concept --Dependent packages and libraries

Extra edition Let's respond to various environments

If you are a rare person who uses both Linux and Mac OS ~~ metamorphosis ~~, you may be happy if you prepare settings and installation scripts for both environments. CI should also be executed in both Linux and Mac OS environments by utilizing matrix build etc.

By the way, recently, I'm in trouble because there is no way to write Linux apt and Mac OS brew in Ansible without using branching by when.

at the end

So what about your dotfiles? I will post it because it seems to be said. Don't expect that much.

[^ 1]: Unix-like hidden file mechanism was a bad habit born from the developer Poka

Recommended Posts

How to grow dotfiles
How to use xml.etree.ElementTree
How to use Python-shell
How to use tf.data
How to use virtualenv
Scraping 2 How to scrape
How to use Seaboan
How to use image-match
How to use shogun
How to install Python
How to read PyPI
How to install pip
How to use Virtualenv
How to use numpy.vectorize
How to update easy_install
How to install archlinux
How to use pytest_report_header
How to restart gunicorn
How to virtual host
How to debug selenium
How to use partial
How to use Bio.Phylo
How to read JSON
How to use SymPy
How to use x-means
How to use WikiExtractor.py
How to update Spyder
How to install BayesOpt
How to use virtualenv
How to use Matplotlib
How to use iptables
How to use numpy
How to use TokyoTechFes2015
How to use venv
How to use Pyenv
How to use list []
How to use python-kabusapi
"How to count Fukashigi"
How to install Nbextensions
How to use OptParse
How to use return
How to install Prover9
How to use dotenv
How to operate NumPy
How to use pyenv-virtualenv
How to use Go.mod
How to use imutils
How to use import
How to estimate kernel density
How to use Qt Designer
[IPython] How to Share IPython Notebook
How to install Python [Windows]
How to use search sorted
python3: How to use bottle (2)
Understand how to use django-filter
XPath Basics (2) -How to write XPath
How to use the generator
Tabpy 1.0 (2020-01 version) How to install
How to change Jupyter layout
How to call a function
How to update Python Tkinter to 8.6