Lc command – combines ls, cat, and nano – useful when you don't have home/end

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)
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
          ;;
        -*)
          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
  }

3 points | by codingblink 1 hour ago

1 comments

  • codingblink 1 hour ago
    ok why did it not format my code? :/