Advertisement
Guest User

fm - a very simple file manager

a guest
Feb 7th, 2011
669
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.53 KB | None | 0 0
  1. # fm v1.9.1 by Kiah Morante
  2. # A very simple file manager.
  3. # Depends on pycp/pymv, http://github.com/yannicklm/pycp and feh
  4. # 'source' this file in a BASH shell
  5.  
  6. showHidden=0 # Hidden files not shown
  7. showDetails=0 # ls is replaced with ls -lh if showDetails is 1
  8.  
  9. shopt -s autocd # cd to a dir just by typing its name
  10. PROMPT_COMMAND='[[ ${__new_wd:=$PWD} != $PWD ]] && list; __new_wd=$PWD' # ls after cding
  11.  
  12. # Shortcuts
  13. source ~/.config/fm/shortcuts # Call all custom shortcuts
  14.  
  15. alias ..='cd ..'
  16. alias ...='cd ../..'
  17. alias ....='cd ../../..'
  18. alias h='cd ~'
  19. alias n='cd "$n"'
  20.  
  21. # Keybindings
  22.  
  23. bind '"\C-l":"list\C-m"'
  24. bind '"\C-h":"hide\C-m"'
  25. bind '"\C-o":"details\C-m"'
  26. bind '"\C-f":"makedir\C-m"'
  27. bind '"\C-n":"n\C-m"'
  28. bind '"\C-y":"cpwd\C-m"'
  29. bind '"\C-p":"cd "$OLDPWD"\C-m"' # Hint: You could also type '~-'
  30.  
  31. # FM prompt appearance
  32.  
  33. if [[ $(whoami) == 'root' ]]; then
  34.     # So that the user knows if they have root privileges:
  35.     PS1="\[\e[0;32\]mf\[m\e[m\] \[\e[0;31m\]root\[\e[m\] \[\e[0;34m\]\w \[\e[m\]\[\e[0;31m\]> \[\e[m\]"
  36. else
  37.     PS1="\[\e[0;32\]mf\[m\e[m\] \[\e[0;34m\]\w \[\e[m\]\[\e[0;31m\]> \[\e[m\]"
  38. fi
  39.  
  40. # Functions
  41.  
  42. # Usage
  43. fmhelp () {
  44.     echo "hide - toggle hidden (hidden by default)
  45. ls - lists contents of dir(s) passed in args.
  46. lsd - list directories
  47. cd - changed to directory \$1
  48. cp \$@ \$2 - copies file from \$1 to \$2
  49. mv \$@ \$2 - moves file from \$1 to \$2
  50. rm \$@ - deletes \$@
  51. sc \$1 \$2 - make a shortcut called \$1 pointing to \$2. If no \$2 is passed, it is evaluated as \$PWD
  52. cpwd - copy current working directory
  53. .., ..., .... - cd .. etc.
  54. o \$1 - opens \$1 with xdg-open
  55. hm - how many files are in the current directory
  56. details - show file details (ls -lh)
  57. fmhelp - this help menu
  58. n - Intelligent guess of the next dir you wish to cd to. Last $1 in open, list, or makedir; last argument in copy or move; pwd before a cd
  59. ~- - BASH shortcut for \$OLDPWD
  60.  
  61. img - feh frontend with the following usage:
  62.      img -t \$2 - views the dirs/images specified in \$2..\$n as clickable thumbnails
  63.      img -s \$2 \$3 - views the images specified in \$3..\$n as a slideshow with a slide change speed of \$2 seconds
  64.      img \$@ - views the dirs/images specified
  65.  
  66. Shortkeys:
  67.  
  68. Ctrl-f - mkdir
  69. Ctrl-h - hide
  70. Ctrl-l - ls
  71. Ctrl-n - cd \$n
  72. Ctrl-o - details
  73. Ctrl-p - cd \$OLDPWD
  74. Ctrl-y - cpwd
  75. Ctrl-u - clear line # urxvt default"
  76. }
  77.  
  78. # Toggle display hidden files
  79. # If $showHidden is 1, hidden files are shown
  80. hide () {
  81.     showHidden=$(( 1 - $showHidden ))
  82.     list
  83. }
  84.  
  85. # Toggle display file details
  86. # If $showDetails is 1, file details are shown
  87. details () {
  88.     showDetails=$(( 1 - $showDetails ))
  89.     list
  90. }
  91.  
  92.  
  93. # ls
  94. listToggle () {
  95.     if [[ $showHidden == 1 && $showDetails == 1 ]]; then
  96.         ls -C --color -A -lh "$dir"
  97.     elif [[ $showHidden == 1 && $showDetails == 0 ]]; then
  98.         ls -C --color -A "$dir"
  99.     elif [[ $showHidden == 0 && $showDetails == 1 ]]; then
  100.         ls -C --color -lh "$dir"
  101.     else
  102.         ls -C --color "$dir"
  103.     fi
  104. }
  105.  
  106. list () {
  107.     clear # Unclutter the screen
  108.    
  109.     # List pwd if no $1
  110.     if [[ $@ == "" ]]; then
  111.         set '.'
  112.     fi
  113.  
  114.     # List multiple folders:
  115.     for dir in "$@"
  116.     do
  117.         listToggle
  118.     done
  119.  
  120.     n="$1" # See 'n' in fmhelp
  121. }
  122.  
  123. # use feh to view thumbnails/images/slideshow
  124.  
  125. img () {
  126.  
  127.     case "$1" in
  128.        -t)       nohup feh --thumbnails "${@:2}" --thumb-height 120 --thumb-width 120 -S filename -d --cache-thumbnails -B black > /dev/null 2>&1 & ;;
  129.        -s)       nohup feh "${@:3}" -S filename -d -B black --slideshow-delay "$2" > /dev/null 2>&1 & ;;
  130.        *)        nohup feh "$@" -S filename -d -B black > /dev/null 2>&1 & ;;
  131.     esac
  132.  
  133.     list
  134. }
  135.  
  136. # cp
  137. copy () {
  138.     if [[ $showHidden == 1 ]]; then
  139.         pycp --interactive --all "$@"
  140.     else
  141.         pycp --interactive "$@"
  142.     fi
  143.  
  144.     list
  145.     n="${@:(-1)}" # n is the last argument (where stuff is moved to)
  146. }
  147.  
  148. # mv
  149. move () {
  150.     if [[ $showHidden == 1 ]]; then
  151.         pymv --interactive --all "$@"
  152.     else
  153.         pymv --interactive "$@"
  154.     fi
  155.  
  156.     list
  157.     n="${@:(-1)}"
  158. }
  159.  
  160. makedir () {
  161.      if [[ $1 == "" ]]; then
  162.          read -e n
  163.          set "$n"
  164.      fi
  165.  
  166.      if mkdir -- "$1"; then
  167.          list # Update pwd to show new dir(s) that have been made.
  168.          n="$1"
  169.      fi
  170. }
  171.  
  172. # rm
  173. remove () {
  174.      rm -rfI "$@"
  175.      list
  176. }
  177.  
  178. # open files
  179. o () {
  180.     # To use xdg-open
  181.     #nohup xdg-open "$1" > /dev/null 2>&1 &
  182.  
  183.     if [ -f "$1" ] ; then
  184.        case "$1" in
  185.        *.tar.bz2)   tar xjf "$1"    ;;
  186.        *.tar.gz)    tar xzf "$1"    ;;
  187.        *.bz2)       bunzip2 "$1"    ;;
  188.        *.rar)       rar x "$1"      ;;
  189.        *.gz)        gunzip "$1"     ;;
  190.        *.tar)       tar xf "$1"     ;;
  191.        *.tbz2)      tar xjf "$1"    ;;
  192.        *.tgz)       tar xzf "$1"    ;;
  193.        *.zip)       unzip "$1"      ;;
  194.        *.Z)         uncompress "$1" ;;
  195.        *.7z)        7z x "$1"       ;;
  196.        *.pdf)       nohup zathura "$1" > /dev/null 2>&1 &     ;;
  197.        *.html)      nohup luakit "$1" > /dev/null 2>&1 &      ;;
  198.        *.blend)     nohup blender "$1" > /dev/null 2>&1 &     ;;
  199.        *.avi)       nohup mplayer "$1"  ;;
  200.        *.wmv)       nohup mplayer "$1"  ;;
  201.        *.rmvb)      nohup mplayer "$1"  ;;
  202.        *.mp3)       nohup urxvtc -si -sw -sh 30 -e mplayer "$1" > /dev/null 2>&1 &     ;;
  203.        *.flv)       nohup mplayer "$1"  ;;
  204.        *.mp4)       nohup mplayer "$1"  ;;
  205.        *.ogg)       nohup urxvt -si -sw -sh 30 -e mplayer "$1" > /dev/null 2>&1 &      ;;
  206.        *.wav)       nohup audacity "$1" > /dev/null 2>&1 &    ;;
  207.        *.jpg)       img "$1"        ;;
  208.        *.jpeg)      img "$1"        ;;
  209.        *.JPG)       img "$1"        ;;
  210.        *.png)       img "$1"        ;;
  211.        *.gif)       nohup gpicview "$1" > /dev/null 2>&1 &    ;;
  212.        *)           nohup urxvt -si -sw -sh 30 -e vim "$1" > /dev/null 2>&1 &          ;;
  213.        esac
  214.     else
  215.         echo "'$1' is not a valid file"
  216.     fi
  217.  
  218.     n="$1"
  219. }
  220.  
  221. # Add shortcuts
  222.  
  223. makeShortcut () {
  224.     if [[ $2 == "" ]]; then
  225.         set $1 .
  226.     fi
  227.  
  228.     echo ""$1"=\""$2"\"
  229.    alias "$1"='cd \""$2"\"'
  230.         " >> ~/.config/fm/shortcuts
  231.  
  232.     source ~/.config/fm/shortcuts
  233. }
  234.  
  235. # Copy pwd to clipboard
  236.  
  237. cpwd () {
  238.     echo \"$(pwd)\" | xclip
  239. }
  240.  
  241. # List directories
  242.  
  243. lsd () {
  244.     ls -F "$@" | grep \/$
  245. }
  246.  
  247. # Command aliases
  248.  
  249. alias mv="move"
  250. alias sc="makeShortcut"
  251. alias cp="copy"
  252. alias ls="list"
  253. alias rm="remove"
  254. alias mkdir="makedir"
  255. alias hm="ls -l . | egrep -c '^-'"
  256.  
  257. list # ls when fm starts
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement