内容简介:Some useful “tricks” from myI found theDefine directory shortcuts with
Some useful “tricks” from my ~/.zshrc
; ( full version here
). Not all
of this is “copy paste ready”, but it should give you some inspiration to build
your own stuff :-)
I found the User’s Guide to ZSH very helpful when learning about zsh. It hasn’t been updated in a while and isn’t even finished, but found it’s quite well-written and useful.
Directory shortcuts
Define directory shortcuts with hash -d
so you can use cd ~x
and vim
~x/file
instead of cd /very/long/and/often/accessed/path
. Some examples:
# Directory shortcuts hash -d pack=$HOME/.cache/vim/pack/plugins/start hash -d vim=/usr/share/vim/vim82 hash -d d=$HOME/code/arp242.net/_drafts hash -d p=$HOME/code/arp242.net/_posts hash -d go=/usr/lib/go/src hash -d c=$HOME/code hash -d gc=$HOME/code/goatcounter
If you put %~
in your PROMPT
then the short version will show up there, too:
$ PROMPT='%~ ' $~ cd ~/.cache/vim/pack/plugins/start ~/.cache/vim/pack/plugins/start$ hash -d pack=$HOME/.cache/vim/pack/plugins/start ~pack$
Filter history completion with what you typed
Make up and down arrow take what’s typed on the commandline in to account. E.g.
if you type ls
and press up it will only find history entries that start with ls
:
autoload -Uz up-line-or-beginning-search down-line-or-beginning-search zle -N up-line-or-beginning-search zle -N down-line-or-beginning-search bindkey '^[[A' up-line-or-beginning-search # Arrow up bindkey '^[OA' up-line-or-beginning-search bindkey '^[[B' down-line-or-beginning-search # Arrow down bindkey '^[OB' down-line-or-beginning-search
I use this a lot, and is the #1 thing I miss if it’s not available.
Easier PATH
Many systems link /bin
to /usr/bin
, and storing all of those in PATH isn’t
too useful. Some helper functiosn to prepend or append to PATH
which also
checks if the path exists so it’s easier to write a portable zshrc:
typeset -U path # No duplicates path=() _prepath() { for dir in "$@"; do [[ -L "$dir" ]] && dir=$(/usr/bin/readlink -f "$dir") [[ ! -d "$dir" ]] && return path=("$dir" $path[@]) done } _postpath() { for dir in "$@"; do [[ -L "$dir" ]] && dir=$(/usr/bin/readlink -f "$dir") [[ ! -d "$dir" ]] && return path=($path[@] "$dir") done } _prepath /bin /sbin /usr/bin /usr/sbin /usr/games _prepath /usr/pkg/bin /usr/pkg/sbin # NetBSD _prepath /usr/X11R6/bin /usr/X11R6/sbin # OpenBSD _prepath /usr/local/bin /usr/local/sbin _prepath "$HOME/go/bin" # Go _prepath "$HOME/.local/bin" # My local stuff. if [[ -d "$HOME/.gem/ruby" ]]; then # Ruby for d in "$HOME/.gem/ruby/"*; do _postpath "$d/bin"; done fi unfunction _prepath unfunction _postpath
Easier alias
Adding a little _exist
helper is similarly useful for a portable zshrc:
_exists() { hash $1 2>/dev/null } _exists vim && export EDITOR=vim _exists less && export PAGER=less _exists bsdtar && alias tar='bsdtar' _exists htop && alias top='htop' if _exists vim; then alias vim="vim -p" alias vi="vim" fi unfunction _exists
Edit ag and grep results
“ag edit” and “grep edit” to quickly open stuff found with ag
or grep
in
Vim:
age() { vim +'/\v'"$1" +':silent tabdo :1 | normal! n' +':tabfirst' -p $(ag "$@" | cut -d: -f1 | sort -u) } grepe() { vim +'/\v'"$1" +':silent tabdo :1 | normal! n' +':tabfirst' -p $(grep "$@" | cut -d: -f1 | sort -u) } $ ag pattern [.. check if results look right ..] $ age pattern [open in Vim]
Caveat: the Vim regexp syntax isn’t quite the same as extended POSIX or PCRE, so the pattern doesn’t always work as expect in Vim. It works most of the time though.
Caveat 2: sometimes I use this to check if I have the right results:
$ ag pattern | less
And then I modify it age
while forgetting to remove the less
:
$ age pattern | less
Vim will not like this :sweat_smile: Not sure if we can write something to be a bit smarter about this. Ideally I would be smarter, but alas I am not.
Global aliases
You can define global aliases with alias -g
, which will work everywhere. I use
it to make piping stdout and stderr to less or Vim a bit easier:
alias -g VV=' |& vim -' alias -g LL=' |& less' $ ls LL $ go test -v VV
Playground environment
Set up a quick “tmp go” environment for testing; I mostly use Go these days, but this can be done for other languages as well:
tgo() { tmp="$(mktemp -p /tmp -d "tgo_$(date +%Y%m%d)_XXXXXXXX")" printf 'package main\n\nfunc main() {\n\n}\n' > "$tmp/main.go" printf 'package main\n\nfunc TestMain(t *testing.T) {\n\n}\n\n' > "$tmp/main_test.go" printf 'func BenchmarkMain(b *testing.B) {\n\tb.ReportAllocs()\n\tfor n := 0; n < b.N; n++ {\n\t}\n}\n' >> "$tmp/main_test.go" printf 'module %s\n' "$(basename "$tmp")" > "$tmp/go.mod" ( cd "$tmp" vim -p main.go main_test.go echo "$tmp" ) }
It will create a main.go
and main_test.go
in /tmp/
with some useful
boilerplate and a go.mod
so it’s recognized as a module (required to get gopls
etc. to work well) and opens the whole shebang in Vim.
This won’t be removed after Vim exits on purpose, so you won’t lose your prototype.
Run stored SQL queries
I have a bunch of scripts in ~/docs/sql/scripts
to get some stats and whatnot
from PostgreSQL. This adds a sql
command with tab-completion
to that directory and runs psql
with some useful flags:
sql() { cmd="psql -X -P linestyle=unicode -P null=NULL goatcounter" f="$HOME/docs/sql/scripts/$1" if [[ -f "$f" ]]; then eval "$cmd" < "$HOME/docs/sql/scripts/$1" | less -S else eval "$cmd" <<< "$1" | less -S fi } _sql() { _files -W ~/docs/sql/scripts } compdef _sql sql
If the file doesn’t exist then the query is just run:
$ sql ls-inactive.sql $ sql 'select * from sites'
less -S
prevents wrapping long lines, which I find more useful for tabular
output.
Shortcuts to edit commandline
Custom mappings to preform some common substitutions, use <C-r>
to prepend doas
to the commandline, or <C-r>
to replace the first word with rm
:
insert_doas() { zle beginning-of-line; zle -U "doas " } replace_rm() { zle beginning-of-line; zle delete-word; zle -U "rm " } zle -N insert-doas insert_doas zle -N replace-rm replace_rm bindkey '^s' insert-doas bindkey '^r' replace-rm
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Landing Page Optimization
Tim Ash / Wiley Publishing / 2008-1-29 / USD 29.99
在线阅读本书 How much money are you losing because of poor landing page design? In this comprehensive, step-by-step guide, you’ll learn all the skills necessary to dramatically improve your bottom li......一起来看看 《Landing Page Optimization》 这本书的介绍吧!
RGB CMYK 转换工具
RGB CMYK 互转工具
HSV CMYK 转换工具
HSV CMYK互换工具