Lc 命令 – 结合了 ls、cat 和 nano – 在没有 Home/End 键时很有用

1作者: codingblink5 个月前
这对于同时浏览目录和读取文件来说非常方便,尤其是在我的笔记本电脑上没有 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: No such file or directory" >&2 fi done } ```
查看原文
So this kinda works well for navigating directories and reading files at the same time, especially since my laptop doesn&#x27;t have home and end keys. Now I don&#x27;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 &#x27;-e&#x27;, then hit enter without having to use home&#x2F;end if you don&#x27;t have those keys Syntax: lc ~ : Displays all files in the home directory lc ~&#x2F;my-file.txt : Displays the content of my-file.txt lc ~&#x2F;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 &quot;$@&quot;; do case &quot;$arg&quot; in -e) edit=true ;; -<i>) opts+=(&quot;$arg&quot;) ;; </i>) paths+=(&quot;$arg&quot;) ;; esac done if [ ${#paths[@]} -eq 0 ]; then ls --color=auto &quot;${opts[@]}&quot; return fi for p in &quot;${paths[@]}&quot;; do if [ -d &quot;$p&quot; ]; then ls --color=auto &quot;${opts[@]}&quot; &quot;$p&quot; elif [ -f &quot;$p&quot; ]; then if $edit; then nano &quot;${opts[@]}&quot; &quot;$p&quot; else cat &quot;${opts[@]}&quot; &quot;$p&quot; fi else echo &quot;lc: $p: No such file or directory&quot; &gt;&amp;2 fi done }