歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Linux中文環境快速進入$HOME目錄“桌面”的辦法

Linux中文環境快速進入$HOME目錄“桌面”的辦法

日期:2017/2/28 16:10:06   编辑:Linux教程

安裝Linux的時候選擇了中文版,桌面的目錄就在 $HOME目錄下的"桌面"目錄;

在gnome terminal要進這個目錄,從RedHat、FC,到RHEL,Ubuntu,沒少煩過我:每次敲cd命令後還要把輸入法切換到中文,再輸入" 桌面";

後來找到一下辦法:在.bashrc中export d=~/桌面/,要進“桌面"目錄也就輸入: "cd $d"

這時,問題又來了:

要把桌面的某個文件移到別的地方,敲mv $d/之後按TAB,bash竟然幫我補全成: "mv \$d/"....無語....

  1. www.linuxidc.com@linuxidc:~$ complete |grep mv
  2. complete -o default -F _longopt mv
  3. www.linuxidc.com@linuxidc:~$
剛開始以為是,default行為或_longopt函數搞的鬼,跟蹤了一下發現_longopt調用了_filedir()函數,_filedir()處理完了還沒有把"$"號變成“\$",結論:bash後面又處理了一下

解決的辦法:在_filedir()函數中把 "$d"替換掉,因此在_filedir()函數最後增加代碼:

  1. local cnt=0
  2. while [ $cnt -lt ${#COMPREPLY[@]} ];
  3. do
  4. local v=${COMPREPLY[$cnt]}
  5. if [[ ${v:0:1} == "{1}quot; ]]; then
  6. COMPREPLY[$cnt]=$(eval echo "$v")
  7. fi
  8. ((cnt++))
  9. done
最後_filedir()函數變成:
  1. _filedir()
  2. {
  3. local i IFS=$\'\n\' xspec
  4. _tilde "$cur" || return 0
  5. local -a toks
  6. local quoted tmp
  7. _quote_readline_by_ref "$cur" quoted
  8. toks=( ${toks[@]-} $(
  9. compgen -d -- "$quoted" | {
  10. while read -r tmp; do
  11. # TODO: I have removed a "[ -n $tmp ] &&" before 'printf ..',
  12. # and everything works again. If this bug suddenly
  13. # appears again (i.e. "cd /b<TAB>" becomes "cd /"),
  14. # remember to check for other similar conditionals (here
  15. # and _filedir_xspec()). --David
  16. printf '%s\n' $tmp
  17. done
  18. }
  19. ))
  20. if [[ "$1" != -d ]]; then
  21. # Munge xspec to contain uppercase version too
  22. [[ ${BASH_VERSINFO[0]} -ge 4 ]] && \
  23. xspec=${1:+"!*.@($1|${1^^})"} || \
  24. xspec=${1:+"!*.@($1|$(printf %s $1 | tr '[:lower:]' '[:upper:]'))"}
  25. toks=( ${toks[@]-} $( compgen -f -X "$xspec" -- $quoted) )
  26. fi
  27. [ ${#toks[@]} -ne 0 ] && _compopt_o_filenames
  28. # If the filter failed to produce anything, try w/o it (LP: #533985)
  29. if [[ -n "$1" ]] && [[ "$1" != -d ]] && [[ ${#toks[@]} -lt 1 ]] ; then
  30. toks=( ${toks[@]-} $( compgen -f -X -- $quoted) )
  31. fi
  32. COMPREPLY=( "${COMPREPLY[@]}" "${toks[@]}" )
  33. local cnt=0
  34. while [ $cnt -lt ${#COMPREPLY[@]} ];
  35. do
  36. local v=${COMPREPLY[$cnt]}
  37. if [[ ${v:0:1} == "{1}quot; ]]; then
  38. COMPREPLY[$cnt]=$(eval echo "$v")
  39. fi
  40. ((cnt++))
  41. done
  42. }
Copyright © Linux教程網 All Rights Reserved