2016年9月3日 星期六

xfce4-terminal 游標顏色問題

最近又改回用 Xfce 桌面環境 (desktop environment),
也把 gnome-terminal 換成 xfce4-terminal,
基本使用上都沒有什麼問題,也多了可以改 tab 名稱的實用功能,
可是預設的游標樣式 (cursor theme) 卻是固定顏色 (預設是綠色),
不會隨著文字顏色而改變,在使用 Vim 時相當不方便,
文字會被游標的顏色吃掉。

解決方法:
只要在編輯 「~/.config/xfce4/terminal/terminalrc」,
加上「ColorCursor=」,
就可以隨著文字顏色而改變游標顏色。

env:
    os: ubuntu 16.04.1 LTS
    xfce4-terminal: 0.6.3

參考資料:
How to force xfce-terminal to use inverse cursor colours

2016年9月2日 星期五

~/.tmux.conf

# 讓<Esc>不要延遲
set -s escape-time 0

# 讓powerline在tmux下可以顯示顏色
set -g default-terminal "screen-256color"
#set -g default-terminal "xterm-256color"

# 開啟window rename功能
set-option -g allow-rename

set -g status-justify left
set -g status-interval 15 # 15 sec refresh
set -g display-time 3000
set -g status-bg colour236
set -g status-fg white
set-window-option -g window-status-current-fg black
set-window-option -g window-status-current-bg yellow
set-window-option -g window-status-current-attr default # bright
set -g status-left-length 15
set -g status-right-length 55
#set -g status-left "#[fg=white,bg=blue] > #I #W < #[default] |" # 0:bash
#set -g status-left "#[fg=white,bg=blue] > #S < #[default] |" # session-name

# 改變日期格式
#set -g status-right "#[fg=red,bright][ #[fg=cyan]#H #[fg=red]]#[default] #[fg=yellow,bright]- %Y-%m-%d #[fg=green]%H:%M #[default]#[fg=magenta,bright](load: #(cat /proc/loadavg | cut -d \" \" -f 1,2,3))#[default]"
set -g status-right "#[fg=colour255][#[default]#[fg=colour34]#H#[default]#[fg=colour255]]#[default]#[default] #[fg=colour39](#(cat /proc/loadavg | cut -d \" \" -f 1,2,3))#[default]#[fg=white] | #[default]#[fg=yellow]%Y-%m-%d #[default]#[fg=yellow, bright]%H:%M #[default]"

# 開啟window number rename功能 not work
#set-option -g renumber-windows on

# 關閉rename window功能
#set-option -g allow-rename off

# Make mouse useful in copy mode
#setw -g mode-mouse on

# Mouse support for scrolling
#set -g mouse on
#set -g mode-mouse on
#set -g mouse-resize-pane on
#set -g mouse-select-pane on
#set -g mouse-select-window on

#set-option -g mouse on

#set-option -g -q mouse on

2015年11月23日 星期一

Vundle: Vim's plugin manager

之前安裝Vim的plugin都是透過Vim的官網下載,
這樣有個問題,就是如果plugin有更新,
你可能不知道,需要留心追蹤,
而如果安裝的plugin一多,
要一個一個追蹤與更新想必是一件相當累人的工作。
所以,Vim的plugin manager就應運而生,
有一個比較早期plugin manager名為pathogen
而網路上的評價比較好的則是Vundle
由於剛進入Vim plugin manager的世界,沒有包袱的我當然就是往Vundle邁進啦!
首先當然是安裝Vundle:
Debian有一個名為Vim-pathogen的套件,不過沒有Vunble,
但可以透過Vundle的GitHub上的文件一步一步來安裝,
首先,必須先安裝git:
Debian/Ubuntu:

apt-get install git


Fedora (22+):

dnf install git

接著下載並至於指定位置:

git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim



之後在~/.vimrc的開頭輸入:

set nocompatible              " be iMproved, required
filetype off                  " required

" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" alternatively, pass a path where Vundle should install plugins
"call vundle#begin('~/some/path/here')

" let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'

" The following are examples of different formats supported.
" Keep Plugin commands between vundle#begin/end.

" All of your Plugins must be added before the following line
call vundle#end()            " required
filetype plugin indent on    " required



再來,進入Vim並在normal mode中輸入:

:PluginInstall

或者,不進入Vim,直接在command-line下輸入:


vim +PluginInstall +qall

就會把Vundle的環境架好。

完成了Vundle的安裝與設置,接下來就是來安裝一些常用的plugins。
只要在上述~/.vimrccall vundle#begin()call vundle#end()之間,
插入像是:

Plugin 'scrooloose/nerdtree'

其中,引號內的字串表示:


此plugin在GitHub上的帳號/plugin的名稱

如果plugin沒有在GitHub上面,也可以有其他對應方式:

Plugin 'tpope/vim-fugitive'
" plugin from http://vim-scripts.org/vim/scripts.html
Plugin 'L9'
" Git plugin not hosted on GitHub
Plugin 'git://git.wincent.com/command-t.git'
" git repos on your local machine (i.e. when working on your own plugin)
Plugin 'file:///home/gmarik/path/to/plugin'
" The sparkup vim script is in a subdirectory of this repo called vim.
" Pass the path to set the runtimepath properly.
Plugin 'rstacruz/sparkup', {'rtp': 'vim/'}
" Avoid a name conflict with L9
Plugin 'user/L9', {'name': 'newL9'}

之後進入Vim的normal mode輸入:


:PluginInstall

就會幫你把NerdTree這個好用的plugin給裝好了!

常用的Vundle指令除了:PluginInstall,還有:

:PluginList " 列出所有plugin :PluginInstall " 安裝plugin :PluginUpdate " 更新plugin :PluginClean " 清除未使用套件 :PluginSearch xxx " 搜尋plugin xxx

想要移除plugin,只要在~/.vimrc中移除該套件(Plugin xxx/xxx),

接著,在Vim normal mode下輸入:

:PluginClean

就可以移除該plugin了!

注意

最新版的的Vundle安裝plugin時是在~/.vimrc裡加入:

Plugin 'xxx/xxx'

而之前版本是使用:


Bundle 'xxx/xxx'

不過目前(2015/10)最新版可以向下相容。


參考資料:
Vundle
Arch Linux - 為 Vim 安裝套件管理工具 : Vundle ( Install Vundle for Vim on Arch Linux )

tmux基本用法

最近認識到一個好用的terminal multiplexer,名叫tmux
據說比GNU Screen還要強大,
我沒有用過GNU Screen,所以在選擇上面當然選一個評價好也比較新的,
也就是tmux!
首先就是把tmux安裝好:
Debian/Ubuntu:



sudo apt-get install tmux
Fedora(version 22):



sudo dnf install tmux
安裝完成後,只要在terminal下輸入tmux,就會進入tmux的環境:
tmux.png

(底下多了條綠線) tmux的透過指令操作,常用指令如下:
(大寫C表示Ctrl)
C+b c: 開新視窗
C+b n or <space>: 切換到下一個視窗
C+b P: 切換到上一個視窗
(以下為視窗分割功能)
C+b ": 水平分割視窗
C+b %: 垂直分割視窗
C+b: <page up> or <page down>: 切換分割視窗
C+b C<方向鍵上> or C<方向鍵下>: 分割視窗大小調整
C+b C+<方向鍵左> or C+<方向鍵右>: 分割視窗大小調整
C+b <space>: 重新佈置分割的視窗
C+b !: 取消所有分割視窗
C+b [: 進入copy mode,可以用滑鼠捲動
C+b ,: 更改視窗名稱
C+b .: 更改視窗編號與排序
C+b l: 移至前一個選擇的視窗
C+b {: 將目前的視窗前移
C+b }: 將目前的視窗後移
C+b ?: 顯示所有指令
C+d: 將目前的 Tmux Session 丟到背景去
exit: 跳出當前分割視窗
想知道其他指令,可以在terminal下輸入:



man tmux


其中我覺得session的儲存功能相當方便,
可以把當下的工作環境完整儲存起來,包括ssh的連線;
只要在tmux環境下輸入<Ctrl> + d,就可儲存並且離開tmux。
tmux可以儲存多個session,每儲存一個session都會給一個session id,
可以在terminal下輸入:



tmux ls
列出主機上所有的tmux session與相關資訊,
要恢復session,只要在terminal下輸入:



tmux a -t 0
a是attach的簡寫,0表示session的id。
如果要自訂化設定,設定檔放在:



~/.tmux.conf
預設沒有這個檔案,需要自己創立。
目前在tmux下使用Vim的時候,按下<ESC>會有延遲的現象,
上網找到了一個解決方法
只要在~/.tmux.conf中設定:



set -s escape-time 0
就不會有延遲現象。
另外,碰到的問題還有在tmux下Vim的外掛powerline無法顯示顏色的問題,
在~/.tmux.conf下設定:



set -g default-terminal "screen-256color"
顏色就正常了。

而在tmux 2.1之前的版本,可以通過設定~/.tmux.conf,
不用使用C+b [就可以直接透過滑鼠捲動,可是目前(2015/10)最新的2.1版本已經拿掉這個功能。
參考資料:
Tmux 教學 + Screen 到 Tmux 的無痛轉換
tmux教學

Debian設定lighttpd + PHP

version info:
lighttpd: 1.4.35-4
OS: Debian GNU/Linux 8.2 (jessie)
PHP: 5.6.13

首先,假設已經安裝好PHP,

step 1:

安裝「lighttpd」和「php5-cgi」:
sudo apt-get install lighttpd php5-cgi

step 2:

enable fastcgi:
sudo lighty-enable-mod fastcgi 
sudo lighty-enable-mod fastcgi-php

step 3:

啟動lighttpd service並載入fastcgi:
sudo /etc/init.d/lighttpd enable
sudo /etc/init.d/lighttpd start
sudo /etc/init.d/lighttpd reload
Debian的lighttpd設定檔放在:
/etc/lighttpd/lighttpd.conf
參考資料:
Lighttpd+PHP
其他資源:
Arch Linuxwiki

ssh 檔案傳送指令

openssh: 6.9

基本構型:


ssh 目標檔案或資料夾 目的地的路徑

將檔案從本機複製到遠端主機:


$scp filename user@remote:/target_path/
filename:檔案名稱(包含路徑)。
user:遠端使用者名稱。
remote:遠端主機名稱(也可以是IP address)。
target_path:檔案要放置的位置(遠端的位置)。

由遠端主機複製檔案至本機:


$scp user@remote:/filename target_path/
filename:檔案名稱(必須包含絕對路徑)。
user:遠端使用者名稱。
remote:遠端主機名稱(也可以是IP address)。
target_path:檔案要放置的位置(本機位置)。

資料夾或多個檔案傳送:


#由遠端主機複製資料夾至本機
$scp -r user@remote:/folder_name/ filename2 target_path/

#由遠端主機複製file1及file2至本機
$scp user@remote:/\{file1, file2\} filename2 target_path/
folder_name:遠端主機的資料夾名稱。

使用非預設port(預設為22):


$scp -P 222 user@remote:/filename target_path/
參考資料:
10 Examples : Copying Files over SSH
凍仁的筆記: scp - 藉由 ssh 的遠端檔案傳輸指令

Python3 字串或程式碼太長

Python3: 3.4.3

寫Python的人應該都知道,Python利用縮排來定義程式碼區塊,
縮排規則比大多數的程式語言還要嚴格,
如果遇到一個很長的字串,例如:
string = 'aaaaaaaaaaaaaaaaaaa bbbbbbbbbbbb cccccccccc ddddddddddddddddddd'
可以利用括號來換行:
string = ('aaaaaaaaaaaaaaaaaaa'
    'bbbbbbbbbbbb '
    'cccccccccc '
    'ddddddddddddddddddd')
括號會把每個字串銜接起來。
如果是程式碼太長也可以使用一樣的方法:
    number = (a * b * c * 
        d * e * f * g * h)

Python3 三元運算式

Python3: 3.4.3

在C語言(或其他C-like)中,時常運用三元運算式(Ternary Operator),
通常是這樣寫:
min = (a <= b) ? a : b;
而在Python3中,則是:
min = a if a <= b else b

簡單自定Vim的顏色

雖然現在在terminal下用的顏色樣式是peachpuff
位置在:

/usr/share/vim/vim74/colors/peachpuff.vim
但如果未來想要自訂其他顏色樣式,
可以直接在normal mode或家目錄下的.vimrc輸入highlight這個指令:

highlight Comment ctermbg=DarkGray
上述的指令表示:「在terminal中所有註解(Comment)的背景變成黑灰色」。
ctermbg表示文字的背景,另有ctermfg表示文字的顏色。
除了改註解的顏色外,還有:

Comment, Constant, Normal, NonText, Special, Cursor...等等。
而顏色可以改成:

Black, DarkBlue, DarkGreen, DarkCyan, DarkRed, DarkMagenta, Brown, 
DarkYellow, LightGray, LightGrey, Gray(Grey), Blue, LightBlue, Green, 
LightGreen, Cyan, LightCyan, Red, LightRed, Magenta, LightMagenta, 
Yellow, LightYellow, White...等等。
除了在terminal,在GUI(ex: GVim)中的顏色可以把ctermfg, ctermbg改成guifg, guibg,並且可以使用RGB的顏色標記來標記顏色:

#表示在GUI界面中所有「Constant」的顏色改為白色(#FFFFFF)
highlight Constant guifg=#FFFFFF
參考資料:
How to control/configure vim colors

Linux crontab設定筆記

編輯crontab:


crontab -e
查看crontab:


crontab -l
移除所有已設定的工作排程:



crontab -r
查看、編輯或移除其他使用者的crontab:



crontab -u user -l
crontab -u user -e
crontab -u user -r


編輯crontab




0 3 * * * * command
意思:每個月的每週的每天的3:00執行command
用一個空白分隔分別表示:
(分鐘) (小時) (日期) (月份) (週) (指令)
範圍:



(0-59) (0-23) (1-31) (1-12) (0-7) (指令)
以一個空白分隔。其中,表示「週」的0或7都可以表示星期日。
範圍除了填入數字,也可以填入以下符號:
*(星號):表示任時間。
範例:表示每天的12:00都執行command



0 12 * * * command
,(逗號):表示可設置多個時段。
範例:表示每天的0:00和12:00都執行command



0 0,12 * * * command
-(減號):表示範圍。
範例:表示每天的0:00至12:00每個小時都執行command



0 0-12 * * * command
/(斜線):表示時間間隔。
範例:表示每天的每隔兩個小時都執行command



0 */2 * * * command

2014年5月3日 星期六

Python3 連結MySQL(MariaDB, Pernona)

Version info:
Python: 3.4.0
MySQL: 5.5.37
OS: Ubuntu Linux 14.04

首先,下載並安裝Python3-mysql.connector套件。

sudo apt-get install python3-mysql.connector
範例程式:

import mysql.connector
#輸入DB的使用者名稱,使用者密碼,主機名稱與DB名稱。
cnx = mysql.connector.connect(user='user_name', password='user_password', host='localhost',database='db_name')
cursor = cnx.cursor()
#設定編碼。
cursor.execute('set names utf8')

query = "SELECT * from a_table"
cursor.execute(query)
#抓取資料。
data = cursor.fetchall()
#完成,回傳一個tuple型態的資料。
print(data)
#下面的程式片段是要讓程式回傳一個list,其中每一筆資料使用dict格式,也就是key-vaule的形式,像是:result[0]['abc']表示第一筆且column名稱為「abc」的資料。
column_name = cursor.column_names
result = list()
temp = dict()

for i in range(len(data)):
    for j in range(len(column_name)):
        temp[column_name[j]] = data[i][j]
    result.append(temp)
    temp = dict()
print(result)

#關閉與資料庫的連結
cnx.close()


2015/09/27
PyMySQL用法也跟mysql.connector一樣。
參考資料:
How to connect Python programs to MariaDB

2014年4月5日 星期六

php:isset(), null, empty(), is_null(), ===null, == null的差別

如果

$a = null;

isset($a)會傳回False

is_null($a)會傳回Ture

$a === null 會傳回True

$a == null 會傳回True

empty($a)會傳回True

----------------------------------------

$a = ture;

isset($a)會傳回True

is_null($a)會傳回False

$a === null 會傳回False

$a == null 會傳回False

empty($a)會傳回False

----------------------------------------

$a = false;


isset($a)會傳回True

is_null($a)會傳回False

$a === null 會傳回False

$a == null 會傳回True

empty($a)會傳回True

----------------------------------------

$a = 0;

isset($a)會傳回True

is_null($a)會傳回False

$a === null 會傳回False

$a == null 會傳回True

empty($a)會傳回True

----------------------------------------

$a = 1;

isset($a)會傳回True

is_null($a)會傳回False

$a === null 會傳回False

$a == null 會傳回False

empty($a)會傳回False

----------------------------------------

$a = \0;

isset($a)會傳回True

is_null($a)會傳回False

$a === null 會傳回False

$a == null 會傳回False

empty($a)會傳回False

----------------------------------------

$a 沒有被設定


isset($a)會傳回True

is_null($a)會傳回False

$a === null 會傳回False

$a == null 會傳回False

empty($a)會傳回False

----------------------------------------

$a = "";


isset($a)會傳回True

is_null($a)會傳回False

$a === null 會傳回False

$a == null 會傳回True

empty($a)會傳回True

----------------------------------------

$a = [];

isset($a)會傳回True

is_null($a)會傳回False

$a === null 會傳回False

$a == null 會傳回True

empty($a)會傳回True

----------------------------------------

圖表:













參考資料:

http://stackoverflow.com/questions/8236354/php-is-null-or-empty

Vim透過行號(line number)複製或刪除

在Normal mode下,輸入:

:23, 25 co 30

表示把23到25行之間(包括23和25行)的內容複製到第30行後,

也就是31行,

原本在31行的文字(或程式碼...)就會往後排序。

也可只輸入一個數字代表只複製一行:

:23 co 30



輸入:

:23, 25 del

表示刪除23到25行(包括23和25行)的內容,

原本在23至25行底下的內容會往上移。

Ubuntu更換開機畫面(Boot Splash Screen)

這邊只更換已經存在的開機畫面,

如果要替換自行下載的開機畫面,

可以參考:

http://askubuntu.com/questions/173329/what-alternatives-are-available-to-replace-the-purple-boot-splash-screen


step1:

開啟terminal並輸入:

sudo update-alternatives --config default.plymouth
 
step2:
 
輸入欲更換開機畫面的號碼,並按下[ENTER]
 
step3:
 
更新設定,在terminal下輸入:
 
sudo update-initramfs -u
 
 
Finished!! 下次開機時就會更換畫面了!

在Gnome 3接收藍牙傳輸的檔案

Gnome版本:3.8.4


Gnome3中要接收藍牙發送來的檔案跟我的理解不太一樣,

並不是在「設定值」> 「Bluetooth」裡面設定,

而是透過「gnome-file-share-properties」這隻程式設定的。


參考資料:http://evilshit.wordpress.com/2013/03/24/how-to-receive-files-via-bluetooth-on-gnome3/

2014年3月13日 星期四

在Linux下載Youtube影片的字幕

先至Google2SRT下載檔案,

解壓縮後用Terminal開啟資料夾,

確定系統有安裝Java後,

執行run.sh,指令如下:

source ./run.sh

或者

sh ./run.sh

上面兩個指令擇一就OK了,

執行成功會跳出操作視窗,

操作方法就不再贅述了,

可以參考下面的連結。


參考資料:http://www.techbang.com/posts/10043-how-to-download-youtube-movies-subtitles-pchome-198-drj

2014年3月9日 星期日

php顯示錯誤訊息

可以透過函式:error_reporting()和ini_set()來完成,

用法如下:

------------------------------------------------

ini_set('display_errors', '1');

error_reporting(E_ALL);

------------------------------------------------

第一行為設定php的設定檔,

把「display_errors」這個選項設定為開啟(1)。

第二行是回報錯誤訊息的種類。

其中,error_reporting()的參數「E_ALL」表示顯示所有錯誤訊息,

其他種類的錯誤訊息可以參考:

 http://www.php.net/manual/en/errorfunc.constants.php

參考資料:

ini_set():

http://www.php.net/manual/en/function.ini-set.php

error_reporing():

http://php.net/manual/en/function.error-reporting.php

2014年3月8日 星期六

Vim: 英文字母的大小寫轉換

以前轉換英文字母的大小寫都是透過人工的方式,

一不小心就會打錯字,

之後上網找方法,看到了Vim官網上的教學:

在Vim的normal mode下選取文字,並鍵入:

gu」:大寫轉成小寫,原本是小寫的維持小寫。

gU」:小寫轉成大寫,原本是大寫的維持大寫。

g~」:大寫的部份轉成小寫,小寫的部份轉成大寫。


參考資料:http://vim.wikia.com/wiki/Switching_case_of_characters

Vim: 多行插入空白

在用Vim Coding時常常會需要換行與縮排,

有時會遇到要縮排n行,

通常會透過「CTRL + v」來選取空白,

接著貼上於要縮排的區段的前面,

可是如果要縮排的對象是在每行開頭的位置,

就無法預先選取空白了。

如果遇到這樣的問題,可以不用一行一行的操作,

可以透過「CTRL + v」先選取要空白或Tab的部份,

接著鍵入「I」,

此時會跳至「CTRL + v」所選取的第一行,

輸入空白或Tab,接著隨意移動游標,

剛剛選取的每一行就會自動重複第一行的動作。


2014年2月15日 星期六