在Windows上安装tmux

2018 Apr 11

Windows上的Git BASH提供了大部分常用的Linux命令行工具,比如grep、sed等,但是并没有提供tmux。 实际上Git for Windows提供了包管理(package management)功能,

Git for Windows is based on MSYS2 which bundles Arch Linux’ Pacman tool for dependency management.

借助pacman,Git for Windows可以安装额外的命令行工具,比如tmux。 但是,在Git BASH里,pacman并没有默认开启

This is intended. We do not ship pacman with Git for Windows. If you are interested in a fully fledged package manager maintained environment you have to give the Git for Windows SDK a try.

需要安装Git for Windows SDK来开启pacman。 安装好之后,打开Git SDK(和Git Bash一样,是一个终端模拟器),

$ pacman -Ss tmux

会找到两个包,

msys/tmux 2.6-1
	A terminal multiplexer
msys/tmux-git 2.5.94.g73b9328c-1 
	A terminal multiplexer
$ pacman -S msys/tmux-git

安装的时候可能会报下面的错误,

$ pacman -S msys/tmux
warning: database file for 'git-for-windows-mingw32' does not exist
error: failed to prepare transaction (could not find database)

打开/etc/pacman.conf文件,注释掉下面的行即可,

#[git-for-windows]
#Server = https://wingit.blob.core.windows.net/x86-64

#[git-for-windows-mingw32]
#Server = https://wingit.blob.core.windows.net/i686

安装好之后,就可以在Windows上(Git SDK)使用tmux了。

tmux on Windows

pacman的用法可参见Git for Windows的Wiki

环境:Windows 10

(如果发现某些程序,比如ssh,报错,可以尝试用pacman -Syu升级所有package。)

CLI

Bash中的Meta键

2017 Apr 11

在Bash中输入命令时,可以使用一些快捷键。 比如输入Ctrl+a可以使光标回到行首。 Bash是用Readline库来处理输入的,实际上这些快捷键是Readline的快捷键。 在man bash的“Readline Key Bindings”章节可以看到详细的快捷键。

除了用Ctrl组合的快捷键,还有用Meta组合的快捷键。 比如Meta+f,可以使光标前进一个word。

实际上,现代的键盘并没有“Meta键”。只有一些古老的键盘上有“Meta键”。

meta key

(By Retro-Computing Society of Rhode Island - Own work, CC BY-SA 3.0, Link

现在一般用Alt键来作为“Meta键”。 比如macOS自带的“终端”程序,就是这样(“终端”->“偏好设置”->“描述文件”->“键盘”->“将Option键做为Meta键”)。

对于iTerm2(Build 3.0.15),如果要用Alt键作为“Meta键”则需要如下配置,

iTerm2->Preferences->Profiles->Keys->Left option key acts as +Esc

For most users, selecting “+Esc” here is the right choice. The “Meta” option sets the high bit of the input character, and is not compatible with modern systems.

CLI

rig,生成随机地址信息的命令行工具

2017 Apr 7

有个命令行工具叫做rig(Random Identity Generator),可以用来生成假的地址信息(美国)。

Rig is a utility that will piece together a random first name, last name, street number and address, along with a geographically consistant (ie, they all match the same area) city, state, ZIP code, and area code.

$ rig
Fred Mcdaniel
318 Plymth Terr
Plainfield, NJ  07061
(908) xxx-xxxx

OS X可以用Homebrew安装,

$ brew install rig

如果没有装命令行工具,也可以直接访问API,https://helloacm.com/api/rig/。 API有rate limit。

$ curl https://helloacm.com/api/rig/
"Ron Suarez\n538 Buncaneer Dr\nIrving, TX  75061\n(903) xxx-xxxx\n"

$ curl https://helloacm.com/api/rig/?c=2
"Kari Carson\n241 Second St\nRome, GA  30161\n(404) xxx-xxxx\n\nTheron Webster\n478 Spring County Blvd\nMiami, FL  33152\n(305) xxx-xxxx\n"
CLI

Here Documents

2016 Oct 28

A here document is a special-purpose code block. It uses a form of I/O redirection to feed a command list to an interactive program or a command, such as ftp, cat, or the ex text editor.

除了interactive program,一些non-interactive program也支持heredoc。vi也支持heredoc。

Choose a limit string sufficiently unusual that it will not occur anywhere in the command list and confuse matters.

The closing limit string, on the final line of a here document, must start in the first character position. There can be no leading whitespace. Trailing whitespace after the limit string likewise causes unexpected behavior. The whitespace prevents the limit string from being recognized.

The - option to mark a here document limit string («-LimitString) suppresses leading tabs (but not spaces) in the output. This may be useful in making a script more readable.

limit string的前后都不要加空格。否则会遇到类似unexpected end of file之类的错误。 如果在脚本里用heredoc,为了缩进可以加上-选项(<<-LimitString),可以省略行开头的tab键。

For those tasks too complex for a here document, consider using the expect scripting language, which was specifically designed for feeding input into interactive programs.

太复杂的交互可以考虑用expect脚本来实现。

CLI

用sed直接修改原文件

2016 Oct 28

sed提供了-i选项可以“就地”(in-place)修改原始文件。

-i extension
Edit files in-place, saving backups with the specified extension. If a zero-length extension is given, no backup will be saved. It is not recommended
to give a zero-length extension when in-place editing files, as you risk corruption or partial content in situations where disk space is exhausted, etc.

sed -i .bak -e 's/foo/bar/' pom.xml # 备份原始文件
sed -i '' -e 's/foo/bar/' pom.xml   # 不做备份
# sed -i -e 's/foo/bar/' pom.xml    # OS X下有“bug”,会生成名为pom.xml-e的备份文件
CLI

网易云音乐的缓存路径

2016 Oct 16

通过lsof | grep -e "[[:digit:]]\+w" | grep -i netease命令可以找到网易云音乐的缓存目录。

CLI

Next →