Tuesday, March 22, 2022
Saturday, November 06, 2021
UTF8 content in Git bare clone encoding 改編碼看UTF8文件
Recently found a git repo of 13,3xx ancient chinese books in utf8 cn. // 萬多本中文古書
(可用chrome 看,可轉為繁體字;或者下載做電字書 用 calibre 做epub 放ebook reader 看 :D )
The repo just too big, multiple giga bytes.
Either download the zip file of the repo (if not care about future updates), or, can do a *minimum* clone:
$ git clone --bare --depth 1 git://....
# list all files in a bare repo
$ git ls-tree --full-tree -r --name-only HEAD
By default, git will print non-ASCII file names in quoted octal notation, i.e. "\nnn\nnn...".
# show utf8 characters (ref: https://stackoverflow.com/questions/22827239/how-to-make-git-properly-display-utf-8-encoded-pathnames-in-the-console-window)
$ git config core.quotepath off
list file again, now show utf8 okay
# to view one file content, say, on master branch
$ git show master:/path/to/file
optionally convert to zh_TW 繁體
$ git show master:/path/to/file | cconv -f utf8-cn -t utf8-tw
Sunday, October 31, 2021
Manjaro linux: syntax/source highlighing/coloring in vim/bash terminal
if you are a text tool user like me, syntax coloring or source code highlighting can be helpful.
# 1. use GUI add/remove software to install 2 packages:
syntax-highlighting
source-highlight
# 2. edit ~/.bashrc and add below
# (if other linux distro, use other path of src-hilite-lesspipe.sh)
export LESSOPEN="| /usr/bin/src-hilite-lesspipe.sh %s"
export LESS=" -R "
alias less='less -m -N -g -i -J --underline-special --SILENT'
# 3. edit ~/.vimrc and add
if &t_Co > 2 || has("gui_running")
syntax on
set hlsearch
endif
* * *
# to test
open a terminal
$ ls
$ less test.cpp
$ less test.py
$ vim test.py
Monday, October 11, 2021
中文檔在bash出亂碼,可能unicode須轉成utf-8
mac osx 與 linux bash 普遍支援 UTF-8 編碼,為 1~4 個bytes不定長度(variable-length) characters。例如ascii 是 1byte,中文字兩bytes。
unicode 係 定長編碼 (fixed-length),unicode 文字檔一般在檔頭加上 Byte order mark (BOM, 字節順序標記)。這種檔在GUI 文件編輯器開無問題,但在 bash 會出亂碼,不能用cat, sed 等命令。
例如有字幕ssa檔
00000000: fffe 5b00 5300 6300 7200 6900 7000 7400 ..[.S.c.r.i.p.t.
00000010: 2000 4900 6e00 6600 6f00 5d00 0d00 0a00 .I.n.f.o.].....
檔頭32bytes 為BOM,標記含義可參考 https://zh.wikipedia.org/wiki/位元組順序記號。
解決辦法 -- 強大轉碼(convert text encoding)工具:
$ iconv -f unicode -t utf-8 in.txt > out.txt
* * *
順帶一提,如果要刪除每行末 ^M ,可用 sed
s/^M//g
注意^M 是一個 character,輸入方法是 ctrl-v ctrl-m。
Sunday, September 26, 2021
Search document content in Mac & Linux
If there's one feature I like most about Mac, it must be the spotlight search 'summoned' via the keyboard shortcut: command-space.
It searches not only by filenames, but also the content text; not only in text files, but also in RTF, DOC, etc. It makes folder boundaries disappear, and file names unimportant. The files indexing is automatically done in background.
In Windows, about 10 years ago, there was a Google desktop search sidebar tool, but discontinued.
In Linux, we have multiple built-in tools, eg. updatedb, then locate pattern for file names; or ascii content by grep -HERin pattern path; or by more search criteria, 'find' command
Ubuntu has its all-in-one search works somewhat similarly.
For other distro's like mxlinux, an option maybe a tool: search monkey. To install:
sudo apt update
sudo apt upgrade
sudo apt install searchmonkey
For usage, 3 different ways:
(A) bottom-left menu, type search. prefilled search location $HOME
(B) terminal command line:
$ searchmonkey
or
$ searchmonkey -d directory -f filename_regex -t text_regex
eg.
searchmonkey -d /path/to/directory -f txt -t howto
(C) the handy way, configure in thunar preference i.e. the explorer
file preference > customized action
select 'find files here'
origin default is catfish
replace it with
searchmonkey -d %f
then setup keyboard shortcut for this action.
Note that it's real time search, not from pre-processed database. Therefore, searching in a big folder takes long time.
How to Split a Subdirectory to a New Git Repository and Keep the History
(modified from: https://ao.ms/how-to-split-a-subdirectory-to-a-new-git-repository-and-keep-the-history/)
Say, original local path is ~/allMyCode
1) origin repo to check out the branch with all the subdirectory history, eg. master
git checkout master
2) clone a copy
git clone ~/allMyCode ~/newSubset
cd ~/newSubset
3) in new clone, remove all but the sub directory (below is a one-liner)
git filter-branch --prune-empty --subdirectory-filter relative/path/to/subdirectory current_branch_name
4) if the new clone is a branch other than 'master', make it the new 'master'
git branch -m master
5) remove untracked leftovers, such as ignored/hidden files/folders (WARNING: make sure you have backup)
git clean -fdx
6) fix remote repo URL // assume new repo empty
git remote set-url origin {newRepoURL}
7) push to new repo
git push -u origin master
Saturday, August 07, 2021
Git clone to replace a dead repo
A network machine hosting the git repo was dead.
The checkout still exists in local.
Setup a new machine, planning to use it as the new host for repo.
ssh user@remote
cd /path/to/allprojects/root
git init --bare new_proj.git
exit
# at local
git remote set-url origin user@remote:/path/to/allprojects/root/new_proj.git
git push --all origin
Saturday, July 31, 2021
Sunday, April 25, 2021
Sunday, June 28, 2020
bash variable string operations
$ dummy=ABCDEFGHIJ
$ echo ${#dummy} # string length '#'
10
$ echo ${dummy:0:2} # slice w/ offset and [length]
AB
$ echo ${dummy:5} # default length: all
FGHIJ
$ echo ${dummy:-3} # no space before '-', not working
ABCDEFGHIJ
$ echo ${dummy: -3} # MUST have space, it matters
HIJ
$ echo ${dummy: -7:4}
DEFG
# pipe to other commands
$ echo $dummy | tail -c 3 # character, last 3 including '\n'
IJ
$ echo $dummy | cut -c2-5 # 'cut' to specify character positions
BCDE
$ echo $dummy | cut -c2,4-5
BDE
$ echo $dummy | grep -o ...$ # last 3 any characters
HIJ
$ echo $dummy | egrep -o '.{1,3}$'
HIJ
Labels: bash, egrep, length, script, slice, string, tail, variable
Sunday, May 24, 2020
Sunday, May 17, 2020
Wednesday, April 01, 2020
linux (eg. ubuntu) format USB and check health status
usb drive doesn't have health data.
First, find the drive device using df or lsblk
Eg. unmount the device sdc1
$ umount /dev/sdc1
Format usb drive to FAT32 on linux
Saturday, November 24, 2018
Saturday, October 08, 2016
Highlight syntax in 'less' command on source code. Colorful!!
sudo apt-get install source-highlight
# osx
brew install source-highlight
# osx only, for compatibility i have,
# if the file 'src-hilite-lesspipe.sh' not included, write it without line number:
# add the following into .bashrc
# example print-outs
# c code:
# python code
sweet. isn't it?
Labels: bash, less, linux, osx, syntax-coloring, syntax-detection
