Skip to content

Commit 1db4d72

Browse files
committed
new blog zsh, fix date
1 parent 5843605 commit 1db4d72

5 files changed

+205
-281
lines changed

_posts/2015-12-16-cowthink-vader.md renamed to _posts/2015-12-14-cowthink-vader.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ image:
88
feature: starwars.jpg
99
comments: true
1010
share: true
11-
date: 2015-12-16 12:00:00
11+
date: 2015-12-14 17:00:00
1212
---
1313

1414
## เปลี่ยน terminal เป็นสไตล์สตาร์วอร์

_posts/2015-12-15-latex-on-atom.md renamed to _posts/2015-12-14-latex-on-atom.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ image:
88
feature: latex-atom.jpg
99
comments: true
1010
share: true
11-
date: 2015-12-15 10:00:00
11+
date: 2015-12-14 12:00:00
1212
---
1313

1414
สวัสดี บล็อกนี้จะค่อนข้าง geek เล็กน้อยถึงปานกลาง แต่อาจจะมีประโยชน์ (มั้ง) ต่อผู้ที่สนใจหรือกำลังอยากลองอะไรแปลกใหม่ จริงๆ คืออยากทำเหมือนเป็นบันทึกไว้ว่าเคยลองทำอะไรแล้วต้องทำยังไง เผื่อวันนึงลืมจะได้ย้อนกลับมาดูได้ แต่ก็นั่นแหละ ขี้เกียจ.. กว่าจะได้เขียนอีกก็หลายเดือนผ่านมา

_posts/2015-12-15-moving-to-zsh.md

+203
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
---
2+
author: Tulakanr
3+
layout: post
4+
title: "[geek] บันทึกการย้ายมาสู่ Zsh"
5+
description: "จาก bash สู่ Zsh อยากย้ายก็ย้ายดื้อๆ"
6+
tags: [zsh, terminal, bash, oh-my-zsh, geek]
7+
image:
8+
feature: blog-zsh.jpg
9+
comments: true
10+
share: true
11+
date: 2015-12-15 12:00:00
12+
---
13+
14+
สวัสดีทุกคน บล็อกใหม่มาอีกละแถมเป็นบล็อก geek อีกตะหาก จริงๆ ก็ไม่ได้ตั้งใจจะเขียน แต่เนื่องจากไปขุดเจอ [Gist](https://gist.github.com/bluenex/9880dc376b9adada792d) ที่เคยเขียนไว้ก็เลยนึกขึ้นได้ว่าเอามาแปลงบล็อกด้วยดีกว่า เผื่อมีใครมาเจอแล้วสนใจอยากลองหรือมาแลกเปลี่ยนกันมันก็น่าจะหาเจอง่ายกว่าทิ้งไว้ใน Gist แบบนั้น ว่าแล้วก็เริ่มเลยดีกว่า
15+
16+
# ย้ายมาสู่ Zsh
17+
หลังจากใช้ bash และ default terminal มาตั้งแต่เริ่มใช้แมคก็เริ่มรู้สึกอยากลองอะไรใหม่ๆ โดยเฉพาะหลังจากได้มารู้จักกับ [tmux (A Terminal Multiplexer)](https://tmux.github.io/) แล้วต้องตั้งค่าหลายขั้นตอนเพื่อใช้เม้าส์ (ก่อนที่ OS X 10.11 จะมาถึง) ก็เลยได้ฤกษ์ย้ายมาใช้ [iTerm2](https://www.iterm2.com/) และคิดว่าไหนๆ ก็ไหนๆ แล้ว อยากลองใช้ Zsh ดูพอดี ย้ายมันทั้งคู่เลยละกัน ก็เลยจัดการย้ายทั้ง terminal ทั้ง shell พร้อมๆ กันซะเลย หลังจากย้ายเสร็จก็ได้จดขั้นตอนไว้ใน Gist แล้วก็เอามาแปลงเป็นบล็อกนี้ ซึ่งมันก็ไม่มีอะไรมากไปกว่าการเป็นบันทึกของสิ่งต่างๆ ที่ได้ค้นหามาแล้วก็เอามาปรับแต่งใช้เอง จริงๆ แล้วเคยเขียน [Gist ทำนองนี้](https://gist.github.com/bluenex/801bfeb9eb308e1ad786)แต่เป็นเวอร์ชันของ bash ไว้ด้วย
18+
19+
### ทำไมต้อง Zsh ล่ะ?
20+
เอาจริงๆ ก็ไม่มีเหตุผลที่ชัดเจน แต่จำได้ว่าตอนนั้นได้ยินเกี่ยวกับ Zsh กับ [fish shell](http://fishshell.com/) พร้อมๆ กัน เลยนึกอยากลองขึ้นมา แต่ fish shell ค่อนข้างจะปรับแต่งยากเล็กน้อยเนื่องจากโครงสร้างสคริปต์ที่แตกต่างจาก bash ปกติ กอปรกับไอเราก็ขี้เกียจเรียนรู้มากขนาดนั้น ก็เลยตัดสินใจลองย้ายมา Zsh ด้วยเหตุผลหลักๆ ที่ว่ามันมีธีมให้เลือกหรือเขียนเองก็ได้ แล้วก็ autocompletion ที่ทำได้ยืดหยุ่นกว่า bash แต่ก็นั่นแหละ จริงๆ คืออยากลองของใหม่นั่นเอง
21+
22+
พูดถึงในมุมมองของเราไปแล้ว ก็ขอเอาลิงค์เกี่ยวกับว่าทำไมถึงเลือกที่จะใช้ Zsh และจะทำยังไงถ้าอยากย้ายมาฝากด้วย ก็ขอให้สนุกกับการทดลองนะ
23+
24+
- [Why Zsh is Cooler than Your Shell](http://www.slideshare.net/jaguardesignstudio/why-zsh-is-cooler-than-your-shell-16194692)
25+
- [A Beautifully Productive Terminal Experience](http://mikebuss.com/2014/02/02/a-beautiful-productive-terminal-experience/)
26+
- [Zsh is your friend](http://mikegrouchy.com/blog/2012/01/zsh-is-your-friend.html)
27+
28+
ส่วนอันนี้คือปลั๊กอินของ Zsh ก็มีสองตัวที่ดังๆ คือ **Oh-My-Zsh** กับ **Prezto** แต่อันนี้เราเองก็ยังคงใช้ Oh-My-Zsh อยู่ ยังไม่ได้ย้ายแต่อย่างใด
29+
30+
- [Ditching Oh-my-zsh for Prezto](http://linhmtran168.github.io/blog/2013/12/15/ditching-oh-my-zsh-for-prezto/)
31+
32+
# เริ่มติดตั้ง
33+
เกริ่นยาวอีกละ ไปเริ่มติดตั้งเลยละกัน ในส่วนนี้ก็เป็นการบันทึกการติดตั้งของเราเอง ซึ่งก็มีหลายอย่างที่ไม่ได้จำเป็นเช่น **tmux**, [**spf13**](https://github.com/spf13/spf13-vim) หรือ [**youtube-dl**](https://github.com/rg3/youtube-dl) แต่ก็ไม่เป็นไร อยากข้ามขั้นไหนก็ข้ามโลด แล้วก็สมมติว่ามี [Homebrew](http://brew.sh/) อยู่แล้วด้วยเนาะ ถ้าไม่มีก็จิ้มตามลิงค์ไปดูวิธีติดตั้งได้
34+
35+
### Dependencies
36+
- **Zsh -** จริงๆ มีติดมากับแมคอยู่แล้ว แต่จะดีกว่าถ้าลงเวอร์ชันใหม่
37+
- **Oh-My-Zsh -** ปลั๊กอินสำหรับ Zsh แต่ผู้พัฒนาเค้าเรียกว่า Zsh configuration framework น่ะนะ
38+
- **tmux -** ก็เอ่อไม่รู้จะอธิบายยังไง แต่มันคือ terminal multiplexer ไม่รู้จะบอกประโยชน์ยังไง แต่ใช้แล้วก็สะดวกดี
39+
- **reattach-to-user-namespace -** อันนี้เพื่อให้ `tmux` สามารถก็อปปี้ไปลงคลิปบอร์ดได้ บน Linux ไม่ต้องลงนะ
40+
- **vim -** text editor on terminal ก็มีติดมากับแมคเช่นกัน แต่ลงใหม่สดใหม่กว่า
41+
- **spf13 -** อันนี้คือ distribution of vim plugins ข้ามๆ ไปก็ได้ ปัญหาเยอะเหมือนกัน ถึงเราจะยังใช้อยู่ก็เถอะ
42+
- **youtube-dl -** อันนี้แถม เป็น command line app เขียนด้วย python ไว้โหลด youtube (ไม่ควรเผยแพร่ไฟล์ที่โหลดมาไม่ว่ากรณีใดๆ นะ)
43+
44+
### Installation
45+
{% highlight bash %}
46+
brew install zsh # Zsh
47+
sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)" # Oh-My-Zsh
48+
brew install tmux # tmux
49+
brew install reattach-to-user-namespace # tmux patch
50+
brew install vim # editor
51+
curl http://j.mp/spf13-vim3 -L -o - | sh # spf13
52+
brew install youtube-dl # add-on
53+
{% endhighlight %}
54+
55+
# การปรับแต่ง
56+
หลักๆ แล้วไฟล์ที่จะต้องใช้ในการปรับแต่งก็จะอยู่ใน home directory ( `~` ) หลักๆ ที่เราใช้ก็มีสามอันนี้
57+
58+
- `~/.zshrc`
59+
- `~/.custom-bash`
60+
- `~/.tmux.conf`
61+
62+
### `.zshrc`
63+
`.zshrc` นี่ก็เทียบได้กับ `.bashrc` คือจะโดนเรียกทุกครั้งที่เปิดหน้าต่างใหม่ของ terminal อย่างไรก็ตามบนแมคก็มี[ข้อยกเว้น](http://www.joshstaiger.org/archives/2005/07/bash_profile_vs.html)อยู่ ถ้าสนใจก็ลองไปอ่านดูได้ ในส่วนของการตั้งค่า เนื่องจากเราได้ทำการติดตั้ง **Oh-My-Zsh** ด้วย จึงทำให้เราไม่ต้องทำอะไรกับมันมากนัก เพราะพวกการเซ็ต path หรือตั้งค่าอื่นๆ ที่จะทำให้ไฟล์รกเราจะไปทำ[ที่อื่น](http://stackoverflow.com/questions/7464677/can-i-create-my-own-bashrc-file-something-like-xyzrc-and-then-import-it-from) แทน แล้วค่อย `source` ไฟล์นั้นใน `.zshrc`
64+
65+
สรุปก็คือ ใน `.zshrc` เราทำแค่สองอย่างคือใส่[ปลั๊กอิน](https://github.com/robbyrussell/oh-my-zsh/wiki/Plugins)ของ **Oh-My-Zsh** แล้วก็ `source` ไฟล์ดอท (dot file)
66+
67+
{% highlight bash %}
68+
# ประมาณบรรทัดที่ 52 เราใช้สามอันนี้ อยากใส่อะไรก็ลองไปอ่านดูนะ แปะลิงค์ไว้ตรงคำว่าปลั๊กอิน
69+
plugins=(web-search chucknorris tmuxinator)
70+
71+
# ที่ท้ายสุดของไฟล์ จะให้ source ไฟล์ดอทของเรา
72+
source ~/.custom-bash
73+
{% endhighlight %}
74+
75+
### `.custom-bash`
76+
อันนี้เป็นคอลเล็กชันของเราเอง ก็ตัดๆ ออกไปบ้าง เหลือไว้แค่บางอันเพราะมันไร้สาระเกิน
77+
78+
{% highlight bash %}
79+
# Export
80+
export PATH="/usr/local/bin:$PATH"
81+
export EDITOR='vim'
82+
83+
# Alias
84+
## github page - jekyll serve
85+
alias ghpjks='bundle exec jekyll serve'
86+
87+
## source .bash_profile
88+
alias srcbpf='source ~/.bash_profile'
89+
alias srcz='source ~/.zshrc'
90+
91+
## hide & show files
92+
alias unhideFiles='defaults write com.apple.finder AppleShowAllFiles YES; killall Finder /System/Library/CoreServices/Finder.app'
93+
alias hideFiles='defaults write com.apple.finder AppleShowAllFiles NO; killall Finder /System/Library/CoreServices/Finder.app'
94+
95+
## command
96+
#alias l='ls -tlcras'
97+
alias s='du -sh'
98+
alias mux='tmuxinator'
99+
100+
##########
101+
102+
# App shortcuts
103+
## Safari
104+
safari() {
105+
open -a Safari.app "$1"
106+
}
107+
108+
## Chrome
109+
chrome() {
110+
open -a "Google Chrome.app" "$1"
111+
}
112+
113+
## VS Code
114+
code () {
115+
VSCODE_CWD="$PWD" open -n -b "com.microsoft.VSCode" --args $* ;
116+
}
117+
118+
##########
119+
120+
# Some useful stuffs
121+
## Song Trimmer
122+
## trimmer <song> <start(sec)> <duration(sec)>
123+
trimmer() {
124+
ffmpeg -i "$1" -ss "$2" -t "$3" -acodec copy cut_"$1"
125+
}
126+
127+
##########
128+
{% endhighlight %}
129+
130+
### `.tmux.conf`
131+
สุดท้ายเป็นการตั้งค่า tmux ซึ่งใครไม่ได้ลงก็ไม่ต้องสนใจตรงนี้เลยก็ได้ โดยปกติ tmux จะต้องกดคีย์ผสมโดยจะมีปุ่มนำที่เรียกว่า prefix แล้วก็ปุ่มตามเป็นคำสั่ง ซึ่งค่าเริ่มต้นมันต้องกด `C-b` หรือ `control-b` แต่ขี้เกียจกดสองปุ่มไง ก็เลยย้ายไปไว้ที่ปุ่ม <code>`</code> แต่จริงๆ แล้วเราจะตั้งเป็นอะไรก็ได้ ไม่ได้มีข้อจำกัดอะไร ใครถนัดอะไรก็ตั้งตามนั้นได้โลด
132+
133+
คีย์ผสมพวกนี้ก็เกิดจากการตั้งขึ้นเอง โดยรวมๆ ยำๆ มาจากลิงค์ด้านล่าง ถ้าตั้งใหม่แล้วลืมว่าคีย์ไหนทำอะไรก็สามารถเช็คได้จากใน **tmux** โดยกด <code>`</code> + <code>?</code>
134+
135+
#### ที่มาของการตั้งค่า tmux
136+
137+
- [Making the clipboard work between iTerm2, tmux, vim and OS X](http://evertpot.com/osx-tmux-vim-copy-paste-clipboard/)
138+
- [Getting tmux to copy a buffer to the clipboard](http://unix.stackexchange.com/questions/15715/getting-tmux-to-copy-a-buffer-to-the-clipboard)
139+
- [Enable Mouse Support in Tmux on OS X](http://www.davidverhasselt.com/enable-mouse-support-in-tmux-on-os-x/)
140+
- [Increased Developer Productivity with Tmux, Part 2: ~/.tmux.conf](http://minimul.com/increased-developer-productivity-with-tmux-part-2.html)
141+
- [TMUX – The Terminal Multiplexer (Part 2)](http://blog.hawkhost.com/2010/07/02/tmux-%E2%80%93-the-terminal-multiplexer-part-2/)
142+
143+
{% highlight bash %}
144+
## BASIC STUFFS
145+
# use zsh as default shell
146+
set-option -g default-shell /bin/zsh
147+
148+
# using ` as prefix
149+
unbind C-b
150+
set-option -g prefix `
151+
bind ` send-prefix
152+
153+
# change split keys
154+
unbind %
155+
bind - split-window -v
156+
unbind '"'
157+
bind | split-window -h
158+
159+
# easily source .tmux.conf
160+
unbind r
161+
bind-key r source-file ~/.tmux.conf \; \
162+
display-message "source-file done"
163+
164+
#count windows and panes from 1
165+
set -g base-index 1
166+
setw -g pane-base-index 1
167+
168+
## MOUSE
169+
# tmux 2.1+
170+
set-option -g mouse on
171+
# tmux 1.8-
172+
# set -g mode-mouse on
173+
# set -g mouse-select-pane on
174+
# set -g mouse-resize-pane on
175+
# set -g mouse-select-window on
176+
177+
## COLOR
178+
set -g default-terminal "screen-256color"
179+
180+
## PASTE BUFFER TO CLIPBOARD
181+
# Copy-paste integration
182+
set-option -g default-command "reattach-to-user-namespace -l zsh"
183+
184+
# Use vim keybindings in copy mode
185+
setw -g mode-keys vi
186+
187+
# Setup 'v' to begin selection as in Vim
188+
bind-key -t vi-copy v begin-selection
189+
bind-key -t vi-copy y copy-pipe "reattach-to-user-namespace pbcopy"
190+
191+
# Update default binding of `Enter` to also use copy-pipe
192+
unbind -t vi-copy Enter
193+
bind-key -t vi-copy Enter copy-pipe "reattach-to-user-namespace pbcopy"
194+
195+
# Bind ']' to use pbpaste
196+
bind ] run "reattach-to-user-namespace pbpaste | tmux load-buffer - && tmux paste-buffer"
197+
198+
# Kill session
199+
bind k confirm-before -p "kill-session #S? (y/n)" kill-session
200+
{% endhighlight %}
201+
202+
# สรุป
203+
ก็หมดละสำหรับการตั้งค่าของเรา ซึ่งอันนี้เป็นเวอร์ชันเมื่อสามเดือนก่อนแล้วก็มีตัดๆ อะไรบางอย่างทิ้งไปบ้าง แต่ก็น่าจะยังใช้ได้เหมือนเดิม ถ้ามีอะไรจะแนะนำหรือแลกเปลี่ยนก็ทิ้งคอมเม้นต์ไว้ได้นะครับ สุดท้ายนี้ขอฝาก [Gist ของ `youtube-dl`](https://gist.github.com/bluenex/40496729bc721d7b4be0) ไว้หน่อยเผื่อจะมีประโยชน์ (ล่ะมั้ง) :p

0 commit comments

Comments
 (0)