Lc命令 - 结合了ls、cat和nano - 在没有Home/End键时非常有用。
这个功能非常适合同时浏览目录和读取文件,特别是因为我的笔记本没有 Home 和 End 键。现在我不需要回到命令的开头,将 `ls` 改为 `cat` 或 `nano`。我还添加了 `-e` 选项,这样你可以通过按上箭头键,输入 `-e`,然后按回车来编辑文件,而不需要使用 Home/End 键(如果你没有这些键的话)。
语法:
- `lc ~`:显示主目录中的所有文件
- `lc ~/my-file.txt`:显示 `my-file.txt` 的内容
- `lc ~/my-file.txt -e`:使用 nano 打开 `my-file.txt` 进行编辑(你也可以替换为 vim 或其他编辑器)
使用方法:将以下内容添加到你的 `.bashrc` 文件中并刷新它:
```bash
# lc 命令
lc() {
edit=false
opts=()
paths=()
for arg in "$@"; do
case "$arg" in
-e)
edit=true
;;
-*)
opts+=("$arg")
;;
*)
paths+=("$arg")
;;
esac
done
if [ ${#paths[@]} -eq 0 ]; then
ls --color=auto "${opts[@]}"
return
fi
for p in "${paths[@]}"; do
if [ -d "$p" ]; then
ls --color=auto "${opts[@]}" "$p"
elif [ -f "$p" ]; then
if $edit; then
nano "${opts[@]}" "$p"
else
cat "${opts[@]}" "$p"
fi
else
echo "lc: $p: 没有这样的文件或目录" >&2
fi
done
}
```
查看原文
So this kinda works well for navigating directories and reading files at the same time, especially since my laptop doesn't have home and end keys. Now I don't have to go back to the start of my command and change ls to cat or nano. I also added the -e option so you can edit the file by pressing (up arrow), type '-e', then hit enter without having to use home/end if you don't have those keys
Syntax:
lc ~ : Displays all files in the home directory
lc ~/my-file.txt : Displays the content of my-file.txt
lc ~/my-file.txt -e : opens my-file.txt for editing using nano (you can replace with vim or anything else too)<p>To use, add this to your .bashrc file and refresh it
# lc command
lc() {
edit=false
opts=()
paths=()
for arg in "$@"; do
case "$arg" in
-e)
edit=true
;;
-<i>)
opts+=("$arg")
;;
</i>)
paths+=("$arg")
;;
esac
done
if [ ${#paths[@]} -eq 0 ]; then
ls --color=auto "${opts[@]}"
return
fi
for p in "${paths[@]}"; do
if [ -d "$p" ]; then
ls --color=auto "${opts[@]}" "$p"
elif [ -f "$p" ]; then
if $edit; then
nano "${opts[@]}" "$p"
else
cat "${opts[@]}" "$p"
fi
else
echo "lc: $p: No such file or directory" >&2
fi
done
}