I work on a remote terminal most of the time. This article collects some tips from my experience.
bash
multiple line editing
Sometimes a complex command consists of multiple lines and you need to edit them before executing it. The lines may be in any format, e.g. json.
- use quotes
|
|
- Here Documents
|
|
- edit via vim
Above ways are inconvenient, because you could not jump back and forth the lines to edit.
Now you need vim.
|
|
Just press C-x C-e
to enter vim, and bash would create a temp file to edit, and when you save
and exit vim, bash will execute the file content.
You could even paste an string in bash and then press C-x C-e
,
bash would pass that string to vim as initial content and let you continue editing.
history customization
|
|
Process Substitution
Some command requires file arguments only, then process substitution is the solution.
For example, diff two curl responses:
|
|
alias completion
Normally we prefer short alias instead of long command, but bash doesn’t support
command completion for alias. Then you could use complete-alias
plugin.
|
|
For example, alias kubectl
as k
.
~/.bashrc
|
|
long string literal
- Use concatenation
|
|
- Use backward slash
Note that spaces from the line beginning will be included in the string.
|
|
tmux
tmux is a brilliant terminal tool you have to use. With tmux, you could parallel your works in one terminal.
Most useful features:
- multiple tmux sessions, each tmux session contains multiple windows
- tmux session runs in background
- no worry about ssh connection gets broken and you could resume where you were
- run server programs in the foreground but left them continue running after detaching
- shared with other user to watch or edit the screen together
attach last session
It’s useful when your ssh connection is broken, you login again and try to attach last session you were in.
|
|
set session name
Set a meaningful session name: C-b $
, e.g. dev
.
You could use this name to attach later.
|
|
session jump
Show all tmux session/window in tree view and jump to any arbitary window.
Setup shortcut:
~/.tmux.conf
unbind t
bind t choose-tree
Press C-b t
to show the windows tree:
(0) - dev: 8 windows (attached)
(1) ├─> 0: bash* (1 panes) "foo"
(2) ├─> 1: bash (1 panes) "foo"
(3) ├─> 2: bash (1 panes) "foo"
(4) ├─> 3: bash (1 panes) "foo"
(5) ├─> 4: bash- (1 panes) "foo"
(6) ├─> 5: vim (1 panes) "foo"
(7) ├─> 6: bash (1 panes) "foo"
(8) └─> 7: bash (1 panes) "foo"
(9) - ops: 1 windows
(M-a) └─> 0: bash* (1 panes) "foo"
Each window shown in the tree has a key:
- for
0-9
, press the number key will jump to the corresponding window - for
M-*
, pressAlt+*
will jump to the corresponding window
jump between two last windows
It’s useful to swtich between your last two windows: C-b l
Tmux Resurrect
With this tmux plugin, you could resume everything after reboot or shutdown, e.g. power off.
https://github.com/tmux-plugins/tmux-resurrect
tmux-resurrect saves all the little details from your tmux environment so it can be completely restored after a system restart (or when you feel like it). No configuration is required. You should feel like you never quit tmux.
fzf
Command history search in bash has no fuzzy search and no history list. fzf
could help you there.
Install fzf
:
|
|
Edit ~/.bashrc
:
|
|
Press C-r
to search history interactively in fuzzy way.
zoxide
The builtin cd
of bash is very inconvenient, it has no history and fuzzy jump.
I prefer the tool zoxide
, which could improve your work productivity.
https://github.com/ajeetdsouza/zoxide
It remembers which directories you use most frequently, so you can “jump” to them in just a few keystrokes.
Example:
|
|
ssh
socks5 proxy sever
|
|
Then use 127.0.0.1:10000
as your proxy address, and the traffic would go through the safe ssh connection.
port forwarding
Sometimes you need to let two machines access to each other, but for security, you could not export ports except ssh port. In this case, you could use port forwarding.
For example, you need to access foo
web server from the same port 1984
but with
different IP address bindings, because you need to simulate sending different cookies.
|
|
jump host
Some servers could only be accessed in internal network.
You could use gateway server as jump host, then no need to login gateway first and run
ssh
from there. Another benefit is you don’t have to store credential, e.g. private keys
in the gateway machine, and everything works just like you login the target machine directly.
~/.ssh/config
Host gateway
...
Host foobar
HostName internal.machine
User foobar
IdentityFile ~/.ssh/foobar.pem
ProxyJump gateway
git
Configure ~/.gitconfig
to improve work productivity.
- commit history tree
git tree
|
|
- exclude some files changes temporarily
|
|
- make diff fancy, e.g. hightlight different parts of two lines
|
|
- credential cache, then no need to input access token for a period
|
|
find
Sometimes you need to check which files you modified within some period.
For example, backup all json files modified in last hour:
|
|
less
less
is a necessary tool to view a file.
tail -f
Press F
to watch new changes of the file (e.g. log file) from the tail constantly.
- filter lines
Press &<pattern>
to show lines only match the pattern.
Press &!<pattern>
to show lines not matching the pattern.
jq
jq
is a brilliant tool to analyze json in the terminal.
https://stedolan.github.io/jq/manual/
Prettify JSON
|
|
Analyze JSON
- Analyze file
jq '.[] | select(.color=="yellow" and .price>=0.5)' fruits.json
- Analyze command output
For example, we use kcadmin.sh
to do keycloak provisioning.
We need to determine one client’s id and use this id to configure its profile.
Output from kcadm.sh
:
|
|
Use jq to get the desired id:
|
|
Merge JSON
Example1:
|
|
Example2:
|
|