---
title: "ย้าย .zshrc ไปยัง .config folder"
description: "วิธีย้าย .zshrc ไปไว้ใน ~/.config/zsh บน macos เพื่อให้ backup ขึ้น git ได้ง่าย พร้อมกัน .zsh_history หลุด"
canonical_url: "https://iamthana.com/blogs/2025-12-28-zshrc-config-folder"
---

> Index of every page on this site: [llms.txt](https://iamthana.com/llms.txt)

## วิธีการตั้งค่า

Note วิธีย้าย .zshrc เข้าไปยัง .config folder บน macos เพื่อให้ง่ายต่อการ backup

```sh
# Create new folder
mkdir -p ~/.config/zsh

# Move config
mv ~/.zshrc ~/.config/zsh/.zshrc
mv ~/.zsh_history ~/.config/zsh/.zsh_history
```

จากนั้นแก้ไขไฟล์ .zshenv เพื่อเปลี่ยน working directory

```sh
nano ~/.zshenv

export ZDOTDIR="$HOME/.config/zsh"
```

ลองปิดเปิดใหม่เพื่อทดสอบว่า config ถูก load ถูกต้องมั้ย (ปิด terminal ไปเลยนะครับ)

## ป้องกันไม่ให้เอา .zsh_history ขึ้น git

แนะนำให้สร้าง .gitignore แล้วเพิ่ม

```sh
# Ignore everything in zsh...
zsh/*
# ...Except .zshrc
!zsh/.zshrc
```

เพื่อให้ backup เฉพาะส่วนที่เป็น config ไว้เท่านั้นครับ เนื่องจากใน .zsh_history อาจจะมี secret ต่าง ๆ อยู่ได้

## ใช้งานร่วมกับ gitleak + pre-commit

เพื่อเพิ่มความปลอดภัยอีกชั้น แนะนำให้ติด pre-commit hook เพื่อดักจับ secret (เช่น API Key, Token ต่าง ๆ) ก่อนที่เราจะเผลอ commit ลงไปใน git ครับ

เริ่มจากติดตั้ง `pre-commit` (หากยังไม่มี)

```sh
brew install pre-commit
```

จากนั้นสร้างไฟล์ .pre-commit-config.yaml ไว้ที่ root directory ของโปรเจกต์ และกำหนด version ของ gitleaks เป็น v8.30.0

```sh [.pre-commit-config.yaml]
repos:
  - repo: https://github.com/gitleaks/gitleaks
    rev: v8.30.0
    hooks:
      - id: gitleaks
```

สั่ง install hook เข้าไปใน git directory

```sh
pre-commit install
```

ผลลัพธ์: หลังจากนี้ทุกครั้งที่เราพิมพ์คำสั่ง git commit ตัว pre-commit จะทำการเรียก Gitleaks มาสแกนไฟล์ที่เรากำลังจะ commit ให้โดยอัตโนมัติ ถ้าเจอ secret มันจะแจ้ง error และหยุดการ commit ให้ทันทีครับ

*เพิ่มเติม (Optional)*
ถ้าอยากให้บทความสมบูรณ์ขึ้น อาจจะปิดท้ายด้วยคำสั่งสำหรับ Test ดูครับ ทดสอบการทำงาน ลองสร้างไฟล์ที่มี Mock Secret ดูครับ

```sh
wget https://raw.githubusercontent.com/trufflesecurity/test_keys/refs/heads/main/keys
git add keys
git commit -m "test secret"
```

![ตัวอย่าง](https://iamthana.com/images/2025-12-28-zshrc-config-folder/gitleak-demo.jpg)

## Sitemap

See the full [sitemap](https://iamthana.com/sitemap.md) for all pages.
