27 Jun 2014
·
·
Updated February 07, 2016
Helper
$ ~ $ home user directory
$ ~+ # same as pwd
$ ~- # previous directory
$ ls -1 $PWD/*.txt # list all .txt with full path
$ cd - # cd to previous path
$ export EDITOR=vim # set vim as default editor
$ groups #list group
$ getent group <group_name> #list members of group
$ top #check performance
$ du -sh <folder> # summary disk usage of folder
$ df -h # disk free
$ chsh - <user> # change default ssh user
$ printenv # list environment variables
$ \<cmd> # run command by avoid alias
Command
nl #number line
xargs <cmd> #change input to arguments, use with pipe
sed #stream editor
pbcopy < <file> # OSX: copy file into clipboard
Where command
$ hash # list command and its path
$ type -a <cmd> # tell all path of commamd
$ which <cmd> # tell default path of command
$ cmd > <file> # print output to file (overwrite)
$ cmd >> <file> # print output to file (append)
$ cmd 2> <file> # print error to file
$ cmd &> <file> # print output & error to file
$ cmd < <file> # use file as input
$ cmd << <file> # use file as input
$ cmd <<< <any> #use anyas input (here string not compute any tag)
# Redirection output wiht sudo
$ echo "hello" | sudo tee out > /dev/null # eq. echo "hello" > out
# echo "hello" | sudo tee -a out > /dev/null # eq echo "hello" > out
$ jobs # list suspended jobs
$ fg # put the top suspended job in the foreground
$ fg %1 # put the suspended job id 1 in the foreground
Ctrl-z # Suspend a foreground job
$ bg # run suspended job in background
$ kill # kill job
Crontab Time schedule
$ crontab -e #edit crontab profile
# */5 * * * * cmd
# run cmd every five minutes
$ grep CRON /var/log/syslog #see crontablog (debian)
Zsh History expansion
should also work with bash.
$ man zshexpn # manual for look in-depth
$ history # tell history command that keep in ~/.bash_history
$ history -c # clear history
! # previous that can combine with command and argument
$ !! # call previous command
$ !<str> # call last command that start with str
$ !?<str>[?] # call last command containing str
$ !<hist_number> # call command in nth of history files
^ # first argument
$ # last argument
:n # nth argument
:m-n # mth to nth argument
* # all argument
$ <space><cmd> # call command without keep history
should also work with bash.
$ bindkey -v # vi mode
$ bindkey -e # emac mode
Ctrl + a # moves the cursor to begin of line
Ctrl + e # moves the cursor to end of line
Ctrl + u # delete current to begin of line
Ctrl + k # delete until end of line
Ctrl + l # clear
Ctrl + w # delete word backwords
Ctrl + d # delete char (moves forward)
Ctrl + f # move char foreward
Ctrl + b # move char backward
Meta + f # move word foreward
Meata + b # move word backward
Ctrl + y # yank last delete word
Ctrl + t # transpoes two characters
Esc + t # transpoes two words
Meta is your Alt key, normally.
For Mac OSX user need to enable it, Open Terminal > Preferences > Settings > Keyboard, and enable Use option as meta key.
$ diff --brief <file1> <file2>
$ export TZ=Asia/Bangkok date
$ Ctrl+R grep Ctrl+R Ctrl+R
# only zsh
$ setopt noclobber # prevents overwriting an existing file.
$ setopt clobber # disable
$ unsetopt RM_STAR_SILENT # ask before rm with star (*)
$ setopt RM_STAR_WAIT # wait 10 sec before exec rm with star
# copy/paste for linux machines (Mac style)
# sudo apt-get install xclip
$ alias pbcopy='xclip -selection clipboard'
$ alias pbpaste='xclip -selection clipboard -o'
$ alias pbselect='xclip -selection primary -o'
Bash
Shell/Bash Script
#!/bin/bash
VAR = "hello"
if [ "$VAR" = "hello" ]; then # use = to equal string not ==
echo $VAR
else
echo "fasle"
fi
$ ./script #run with using shebang(#) in script ex. #!/usr/bin/python
$ . script #run with shell (work in related bash)
$ source script #run with shell (work in both bash and csh)
$ echo 'rm() { mkdir -p /tmp/rm; /bin/mv "$@" /tmp/rm; }' >> .zshrc
cmd1 && cmd2 # cmd 2 run only if cmd1 successful
cmd1 ; cmd2 # cmd 2 run whether failed or not
Parallel shell script
- Parallel batch processing in the shell
- Bash script processing commands in parallel
process1 &
process2 &
process3 &
process4 &
wait # wait until process1, 2, 3, 4 done
process5 &
process6 &
process7 &
process8 &
wait # wait until process5, 6, 7, 8 done
- Parallelize Bash Script
#!/bin/bash
nproc=$(($(nproc)-1)) # number of core cpu - 1
# waitproc - wait until have core slots
waitproc () {
jobcnt=($(jobs -p))
until [ ${#jobcnt[@]} -lt $nproc ]; do
sleep 0.05
jobcnt=($(job -p))
done
if [ $? -eq 0 ]
do
echo "hello"
done
#!/bin/bash
array=( one two three )
for i in "${array[@]}"
do
echo $i
done
$ dirname "/home/user/dir/file.py"
/home/user/dir
$ basename "/home/user/dir/file.py"
file.py
last reboot | less
last -x|grep shutdown | head -1
Zsh
Bash References:
- Functions
- Bash shell expansion
- bash-tricks
- check command exitsts
- High-speed Bash
ETC
$ less +F log/production.log
Reference
- learning the shell
- HowTo: Add Jobs To cron Under Linux or UNIX?
- how to set cron job to run every 5 mins?
- CronHowto
- Checking UNIX Server Performance
- How do I use sudo to redirect output to a location I don’t have permission to write to?
- UNIX Disk Usage Command Examples
- Unix as IDE: Introduction
- หัดใช้ Command-line Interface เถอะ
- Pimp my command line
- The Art of Command Line
- Command Line Power User
- Here Strings
- what is the find command, and how do I use it to search
- Totally Tooling Mini Tip: Command-line Keyboard Shortcuts
- 10 examples of paste command usage in Linux
- Bang! Previous Command Hotkeys - Linux CLI BASH ZSH
22 Jun 2014
·
Recover lost partition
I used TestDisk to recover my lost NTFS partition that I formatted by accidentally. You can use TestDisk by follow TestDisk Step by Step. After recovered I can see my NTFS partition that was formatted but can not access it.
Repair damaged partition
I got my lost partition back but can’t access it. So I use ‘chkdsk /r’ command on Windows to repair damaged partition. Finally after 12 hours, I can got lost partition back and accessible it normally.
16 Jun 2014
·
·
Updated December 26, 2015
This blog created by Jekyll that is ruby scripts to transforms plain text into static html website and blogs and hosted on Github
Page that free hosting support Jekyll.This blog use Lanyon theme that based on
Poole a minimal style of Jekyll.
Set up github page:
- Follow setup guilde of Github page
for more information about jekyll on github look at
```
http://help.github.com/articles/using-jekyll-with-pages
``` 2. Clone project from [Lanyon](http://lanyon.getpoole.com)
```
git clone https://github.com/poole/lanyon.git
```
- Configs jekyll in
_config.yml
```yaml
#setup
title: botbotbot
tagline: "'s blog"
description: "botbotbot's blog"
url: http://ibotdotout.github.io
paginate: 5
```
- Make tags.md in root directory copy code from
```
https://github.com/LanyonM/lanyonm.github.io/blob/master/tags.html
```
- Let blog it.
Add Feed in Jekyll
- Jekyll Feed plugin - Recommend
- Atom Feed in Jekyll
- RSS Feed in Jekyll
- RSS for Jekyll blogs
References:
- http://joshualande.com/jekyll-github-pages-poole/
- Table not render when use Redcarpet in Jekyll GitHub Pages?
- Jekyll-Plugins
- Jekyll Stater
- Adding Disqus Comment to your Jekyll
- How can I get backtick fenced code blocks working inside lists (with kramdown)?
09 Jun 2014
·
Tutorial
Enter to Vim
vim . # open with file browse
vim -c "h quickref | only"
vim -O < file1 > < file2 > ... # open in verticle
vim -o < file1 > < file2 > ... # open in honzital
Normal Mode
Save, Exit
Keys |
Description |
ZZ |
save and quit |
ZQ |
save without quit |
Movements
Keys |
Description |
w[ord] |
{letters, numbers, underscore} |
W[ORD] |
{non-blank characters} |
g{motion} |
work with display line ex. gj down one line, g$ go to end of display line |
f{char} |
move to next char |
t{char} |
move to before next char |
; |
repeat last search |
, |
repeat last search backward |
Jump
Keys |
Description |
% |
jump between open/close parentheses |
<C-o>, <C-i> |
toggle jump position |
{num}|(pipe) |
Go to Column |
<number>G |
go to line number |
Visual
Keys |
Description |
v{motion} |
visual mode select in line |
V{motion} |
visual mode select line |
<C-v>{motion}{cIA} |
change/insert/append in multiple lines |
<C-v>{motion}: |
command with multiple lines |
Indent
Keys |
Description |
== |
Auto Indent |
= |
Auto Indent in selected on visual mode |
J |
join the current line and next line together |
Edit
Keys |
Description |
cw |
change word |
gu{motion}, gU{motion} |
lower/upper letter |
Delete
Keys |
Description |
daw |
delete a word |
das |
delete a sentence |
dap |
delete a paragraph |
dW |
delete until space |
dG |
delete until end of file |
d$ |
delete until last of line |
dbw , <C-w> in insert mode |
delete back one word |
d^ , <C-u> in insert mode |
delete back to start of line |
Undo
Keys |
Description |
u |
redo |
<C-r> |
Redo |
Search
Keys |
Description |
\<word>\c |
search word with ignore case |
Repeat
Keys |
Description |
. |
repeat last action (not include any move action) |
ETC
Keys |
Description |
* |
highlight word |
<C-a>,<C-x> |
addition and subtraction on numbers that also find next number automatically if current cursor not a number |
<C-I> |
Tab |
<C-[> |
Esc |
<C-M> |
Enter |
<C-H> |
Backspace |
In Insert Mode
Keys |
Description |
<C-r>=55*10<Cr> in insert mode |
calculate can print it |
Windows Managements
Windows Split
Keys |
Description |
<C-ws> |
Split windows |
<C-wv> |
Split windows vertically |
<C-wq> |
Quit windows |
<C-ww> |
switch between windows |
You split navigations with
nnoremap <C-J> <C-W><C-J>
nnoremap <C-K> <C-W><C-K>
nnoremap <C-L> <C-W><C-L>
nnoremap <C-H> <C-W><C-H>
Tab
Command |
Description |
:tabe[dit] {filename} |
open file in new tab use gt, gT to move to next, previous tab |
:tabc[lose] |
close all tabs |
:tabo[nly] |
keep current tab and closing all others |
Vim spell
Command |
Description |
:set spell |
enable spell checker |
[s, ]s |
jump to miss spelling work |
z= |
fix miss spelling word |
zg |
add current word into spell dictionary |
zw |
remove current word from spell dictionary |
Repositioning the screen
Command |
Description |
zt |
Move current line to top screen |
zz |
Move current line to mid screen |
zb |
Move current line to bottom screen |
Keys |
Description |
<C-f> |
scroll down one screen |
<C-b> |
scroll up one screen |
<C-d> |
scroll down half screen |
<C-u> |
scroll up half screen |
Text object <need to combine with cmd>
Keys |
Description |
iw/aw |
inside/around word |
is/as |
inside/around sentences |
ip/ap |
inside/around paragraph |
it/at |
inside/around tag ex. html tag |
i” |
inside double quote |
i) |
inside parents |
Register
# update vim version
$ brew install vim
$ brew install reattach-to-user-namespace
Command |
Description |
:reg[ister] |
check buffer register |
”<register>p |
paste buffer |
”<register>yy |
yank to buffer |
”*yy |
yank to system clipboard |
Vim Script
- autocmd group
Open vim without plugin no settings from your .vimrc
Open vim without plugin
Reference:
- A vim Tutorial and Primer
- วิธีใช้ Vi และ Vim
- How to Use Vim’s Spell checker
- Vi/Vim Cheat Sheet
- Vim Cheat Sheet Graphical
- Vim & Tmux & System Clipboard
- vim.rc & vim plugin (video th)
- John Crepezzi’s Vim (video)
- Vi and Vim Macro Tutorial: How To Record and Play
- VIM Grammar-Jaa-LGyagRA
- Vim tips: Folding fun
- Learn Vimscript the Hard Way
- Vim as an IDE
- How to jump directly to a column number in Vim
17.Coming Home to Vim
- Vim-LanguageTool
- Vim Tips (KKLUG)
- Vim multiline editing like in sublimetext?
- A Good Vimrc
- THE VIM PRESENTATION
- Vim Cast
- vim-galore - Everything you need to know about Vim.
- Converting tabs to spaces