summaryrefslogtreecommitdiff
path: root/fedora/.config/bash/keymaps.bash
blob: 15bd7e200c21dea58c36759cc9694226ac14478d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/bin/bash

# --------- Bash port (u-prefix removed) ---------
# Put this into ~/.bashrc and run: source ~/.bashrc

# enable vi editing mode
set -o vi

# Try to set beam cursor at prompt (best-effort)
PROMPT_COMMAND='echo -ne "\e[5 q"'

# ---------- helper: pre_cmd ----------
pre_cmd() {
  local last_cmd
  last_cmd=$(history 1 | sed 's/^[ ]*[0-9]*[ ]*//')

  if [[ $last_cmd != sudo* ]]; then
    READLINE_LINE="sudo $last_cmd"
    READLINE_POINT=${#READLINE_LINE}
  fi
}

# ---------- Clipboard detection ----------
_detect_clipboard_setup() {
  if command -v pbcopy >/dev/null 2>&1 && command -v pbpaste >/dev/null 2>&1; then
    clipcopy() { cat "${1:-/dev/stdin}" | pbcopy; }
    clippaste() { pbaste; }
    return 0
  fi
  if command -v wl-copy >/dev/null 2>&1 && command -v wl-paste >/dev/null 2>&1; then
    clipcopy() { cat "${1:-/dev/stdin}" | wl-copy; }
    clippaste() { wl-paste --no-newline; }
    return 0
  fi
  if command -v xclip >/dev/null 2>&1; then
    clipcopy() { cat "${1:-/dev/stdin}" | xclip -selection clipboard; }
    clippaste() { xclip -selection clipboard -out; }
    return 0
  fi
  if command -v xsel >/dev/null 2>&1; then
    clipcopy() { cat "${1:-/dev/stdin}" | xsel --clipboard --input; }
    clippaste() { xsel --clipboard --output; }
    return 0
  fi
  if command -v clip.exe >/dev/null 2>&1; then
    clipcopy() { cat "${1:-/dev/stdin}" | clip.exe; }
    clippaste() { powershell.exe -noprofile -command Get-Clipboard 2>/dev/null; }
    return 0
  fi
  if command -v tmux >/dev/null 2>&1 && [ -n "${TMUX:-}" ]; then
    clipcopy() { tmux load-buffer -; }
    clippaste() { tmux save-buffer -; }
    return 0
  fi
  return 1
}
_detect_clipboard_setup || true

paste_clipboard_to_readline() {
  if ! command -v clippaste >/dev/null 2>&1; then
    _detect_clipboard_setup || {
      printf 'No clipboard helper found\n' >&2
      return 1
    }
  fi
  local clip
  clip=$(clippaste 2>/dev/null) || return 1
  clip="${clip%$'\n'}"
  if [[ -z "${READLINE_LINE+set}" ]]; then
    printf '%s' "$clip"
    return
  fi
  local left=${READLINE_LINE:0:READLINE_POINT}
  local right=${READLINE_LINE:READLINE_POINT}
  READLINE_LINE="${left}${clip}${right}"
  READLINE_POINT=$((READLINE_POINT + ${#clip}))
}

copy_readline_to_clipboard() {
  if [[ -z "${READLINE_LINE+set}" ]]; then
    printf 'No current line to copy\n' >&2
    return 1
  fi
  if ! command -v clipcopy >/dev/null 2>&1; then
    _detect_clipboard_setup || {
      printf 'No clipboard helper found\n' >&2
      return 1
    }
  fi
  printf '%s' "${READLINE_LINE}" | clipcopy
}

# ---------- basic utilities ----------
clear_tree_2() {
  clear
  tree -L 2 2>/dev/null || true
}
clear_tree_3() {
  clear
  tree -L 3 2>/dev/null || true
}
insert_current_date() {
  local txt="$(date -I)"
  if [[ -z "${READLINE_LINE+set}" ]]; then
    printf '%s' "$txt"
    return
  fi
  local left=${READLINE_LINE:0:READLINE_POINT}
  local right=${READLINE_LINE:READLINE_POINT}
  READLINE_LINE="${left}${txt}${right}"
  READLINE_POINT=$((READLINE_POINT + ${#txt}))
}
insert_unix_timestamp() {
  local txt="$(date +%s)"
  if [[ -z "${READLINE_LINE+set}" ]]; then
    printf '%s' "$txt"
    return
  fi
  local left=${READLINE_LINE:0:READLINE_POINT}
  local right=${READLINE_LINE:READLINE_POINT}
  READLINE_LINE="${left}${txt}${right}"
  READLINE_POINT=$((READLINE_POINT + ${#txt}))
}
git_status_clear() {
  clear
  git status 2>/dev/null || git status --short 2>/dev/null || true
}
tmux_left_pane() {
  tmux select-pane -L 2>/dev/null || true
  tmux resize-pane -Z 2>/dev/null || true
}
vi_append_clip_selection() { paste_clipboard_to_readline; }
copybuffer() { copy_readline_to_clipboard; }
background_start() {
  local cmd="$1"
  shift || return 0
  for arg in "$@"; do "$cmd" "$arg" &>/dev/null & done
}

# ---------- pre_cmd widgets ----------
man_command_line() { pre_cmd "man"; }
sudo_command_line() { pre_cmd "sudo"; }

# ---------- wrappers (u-prefix REMOVED) ----------
bc() { command -v bc >/dev/null 2>&1 && /usr/bin/env bc "$@" || printf 'bc: not found\n' >&2; }
cdi() { command -v cdi >/dev/null 2>&1 && cdi "$@" || printf 'cdi: not found\n' >&2; }
lastfiles() { command -v lastfiles >/dev/null 2>&1 && lastfiles "$@" || printf 'lastfiles: not found\n' >&2; }
htop() { command -v htop >/dev/null 2>&1 && htop "$@" || printf 'htop: not found\n' >&2; }
sessionizer() { command -v sessionizer >/dev/null 2>&1 && sessionizer "$@" || printf 'sessionizer: not found\n' >&2; }
upd() { command -v upd >/dev/null 2>&1 && upd "$@" || printf 'upd: not found\n' >&2; }
cht() { command -v cht >/dev/null 2>&1 && cht "$@" || printf 'cht: not found\n' >&2; } # from '^ucht' -> 'cht'
ali() { command -v ali >/dev/null 2>&1 && ali "$@" || printf 'ali: not found\n' >&2; }
fD() { command -v fD >/dev/null 2>&1 && fD "$@" || printf 'fD: not found\n' >&2; }
lastfiles_l() { command -v lastfiles >/dev/null 2>&1 && lastfiles -l || printf 'lastfiles: not found\n' >&2; }

# ---------- Readline key bindings (bind -x) ----------
# basic movement
bind '"\C-a": beginning-of-line'
bind '"\C-e": end-of-line'

# function key bindings (map to bash functions above)
bind -x '"\C-x\C-e":clear_tree_2'
bind -x '"\C-x\C-w":clear_tree_3'
bind -x '"\C-x\C-s":git_status_clear'
bind -x '"\C-x\C-x\C-t":insert_current_date' # ^X^X^T (alternate: C-x C-t)
bind -x '"\C-x\C-t":insert_current_date'
bind -x '"\C-x\C-x\C-u":insert_unix_timestamp' # ^X^X^U
bind -x '"\C-x\C-u":insert_unix_timestamp'

# clipboard binds
bind -x '"\C-x\C-p":paste_clipboard_to_readline' # ^X^P
bind -x '"\C-x\C-y":copy_readline_to_clipboard'  # ^X^Y

# edit in editor
edit_command_line() {
  local tmp content
  tmp=$(mktemp /tmp/bash-edit.XXXXXX) || return
  printf '%s' "${READLINE_LINE:-}" >"$tmp"
  "${EDITOR:-vim}" "$tmp"
  content=$(<"$tmp")
  READLINE_LINE="$content"
  READLINE_POINT=${#content}
  rm -f "$tmp"
}
bind -x '"\C-x\C-v":edit_command_line' # ^X^V

# man & sudo insertion
bind -x '"\C-x\C-m":man_command_line' # ^X^M
stty -ixon 2>/dev/null || true
bind -x '"\C-s":sudo_command_line' # ^S  (stty -ixon to avoid flow control)

# tmux left pane (bind ESC + backslash)
#bind -x '\e\\':tmux_left_pane

# ---------- mappings of the original bindkey -s lines (u removed) ----------
bind -x '"\C-b":__bc' # will call function __bc below
__bc() { bc -lq "$@"; }

bind -x '"\C-d":cdi'
bind -x '"\C-f":fzffiles'
bind -x '"\C-n":lastfiles'
bind -x '"\C-o":tmo'
bind -x '"\C-p":fzfpass'
bind -x '"\C-q":htop'
bind -x '"\C-t":sessionizer'
bind -x '"\C-z":pd'
# ^_ (Ctrl-_) mapped to cht (from '^ucht' -> 'cht')
bind -x $'"\C-_":cht'

# ^X^... sequences (Ctrl-X then key)
bind -x '"\C-x\C-a":ali'
bind -x '"\C-x\C-d":fD'
bind -x '"\C-x\C-f":gitfiles'
bind -x '"\C-x\C-g":rgafiles'
bind -x '"\C-x\C-l":gloac'
bind -x '"\C-x\C-n":lastfiles_l'
bind -x '"\C-x\C-q":fpkill'
bind -x '"\C-x\C-_":fzffns' # ^X^_