From Gray Tortoise, 2 Weeks ago, written in Plain Text.
  1. #!/bin/bash
  2. # Desc: Miscellaneous shenanigans and hacks to make vim comfier.
  3. # Usage: vim [-] [OPTS] [FILE]
  4. # Features: - Enable ^S and ^Q in vim by reassigning stty start and quit to '\0'
  5. #           - Create a tempfile to allow `:foo %:p:h` style commands
  6. #           - Dismiss "Press ENTER or type..." prompt on first open
  7.  
  8.  
  9. # Default tty chars are:
  10. # intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = M-^?;
  11. # eol2 = M-^?; swtch = M-^?; start = ^Q; stop = ^S; susp = ^Z;
  12. # rprnt = ^R; werase = ^W; lnext = ^V; flush = ^O;
  13.  
  14.  
  15. # Possible invocations. Correct value, behavior, and current state
  16. # format: var -> [test:action:status] where
  17. #   test=on/off, action=run/exit, status=pass/fail
  18. #            1:"vim"   2:"vim -"   3:"vim ./foo"   4:"foo|vim -"   5:"foo|vim"
  19. # stdin ->  [off:run:pass, !:exit:pass,  off:run:pass, on:run:pass, !:exit:pass]
  20. # nofile -> [on:run:pass,  on:exit:pass, off:run:pass  on:run:pass, !:exit:pass]
  21. # istty ->  [?:run:pass    !:exit:pass   ?:run:pass    on:run:pass  !:exit:pass]
  22.  
  23.  
  24. # /* FUNCDEF */ --------------------------------------------------------------
  25. msg(){ printf -- "%s\n" "$@"; }
  26. emsg(){ printf -- "%s\n" "$@" >&2; }
  27. bail(){ emsg "$@"; usage; exit 1; }
  28. usage(){ awk -F '# ' 'NR>2{print $2};$0==""{exit}' <"$0"; }
  29.  
  30.  
  31. # /* ENV */ ------------------------------------------------------------------
  32. declare -a argv
  33. declare SHITTYOPTS stdin nofile istty tempfile
  34.  
  35. stdin='false'; nofile='true'; istty='true'
  36. SHITTYOPTS="$(stty -g 2<&-)"
  37. trap 'rm "$tempfile" 2<&-; stty "$SHITTYOPTS" 2<&-' EXIT
  38.  
  39.  
  40. # /* INIT */ -----------------------------------------------------------------
  41. while (($#)); do
  42.   case "$1" in
  43.     --help) # Print usage and vim -h
  44.       vim -h; usage; exit 0
  45.       ;;
  46.     -g) # Use X11
  47.       declare istty='false'
  48.       argv+=("$1")
  49.       shift; continue
  50.       ;;
  51.     -) # Read from stdin
  52.       [[ -t 0 ]] && exit 1 || stdin='true'
  53.       shift; continue
  54.       ;;
  55.     *) # Accumulate other args. Test for input file
  56.       [[ -r "$1" ]] && nofile='false' # NOTE: Filetest must go here to prevent
  57.       argv+=("$1"); shift; continue   # inadvertant toggling of $nofile
  58.       ;;
  59.   esac
  60. done
  61.  
  62.  
  63. # /* MAIN */ -----------------------------------------------------------------
  64. if [[ "$nofile" == "true" ]]; then
  65.   tempfile="$(mktemp -u vim.tmp.XXXXXXXXXXXXXXXX)"
  66.   # Use tempfs if its mounted
  67.   awk '$2~/\/home\/mnrmnaugh\/dev\/mem/{exit 1}' </proc/mounts \
  68.     || tempfile="${HOME}/dev/mem/tmp/${tempfile}"
  69.   argv+=("--" "$tempfile")
  70.  
  71.   # Dump stdin to tempfile
  72.   if [[ "$stdin" == "true" ]]; then
  73.     { while read -r; do
  74.       printf -- "%s\n" "$REPLY" >>"$tempfile"
  75.     done < <(cat <&0); }
  76.     # Reset stdin
  77.     exec 0<&1
  78.   fi
  79. fi
  80.  
  81.  
  82. if [[ "$istty" == "true" ]]; then
  83.   stty start \000 2<&-
  84.   stty stop \000 2<&-
  85.   stty intr \000 2<&-
  86.   stty quit \000 2<&-
  87. fi
  88.  
  89.  
  90. # NOTE: Calling feedkeys() dismisses "Press ENTER or type..." prompt
  91. /usr/bin/vim --cmd 'call feedkeys("jk", "t")' --not-a-term "${argv[@]}"
  92. #/usr/bin/vim --cmd 'call feedkeys("jk", "t")' -c 'set balloonevalterm ballooneval balloondelay=50' -c 'au InsertEnter * call balloon_show("BALLOON!")' --not-a-term "${argv[@]}"
  93.  
  94.  
  95.  
  96. # Grrrrrr if only...
  97. #printf "$@" | \command $realvim < /dev/tty
  98. # AAAaaaahhh
  99. #\command /usr/bin/vim -c ':silent! read '"$@"' | filetype detect | file! '"$@"'' ${vargs} </dev/tty
  100.  
  101. #umktemp(){
  102. #  declare -a argv; declare TMPDIR template
  103. #  template="${1:-tmp.XXXXXXXXXXXXXXXX}"
  104. #
  105. #  while (($#)); do
  106. #    case "$1" in
  107. #      -t) [[  ! -z "$2" ]] && argv+=("$1" "$2"); shift 2; continue;;
  108. #      *)  [[ -d "$1" ]] && TMPDIR="$1"; shift; continue;;
  109. #    esac
  110. #  done
  111. #  [[ ! -d "$TMPDIR" ]] && printf -- "W!: unset variable \"\$TMPDIR\"" >&2
  112. #  [[ ! -z "${argv[@]}" ]] && set -- "${argv[$@]}" || set --
  113. #  \command mktemp "$@" "$template"
  114. #}
captcha