akermu / emacs-libvterm

Emacs libvterm integration
GNU General Public License v3.0
1.7k stars 136 forks source link

Copy/kill region in promt? #676

Open theottm opened 1 year ago

theottm commented 1 year ago

I am looking for M-w and C-w to work directly in the prompt (ouside of copy-mode), similar to how C-k can kill the line using the workaround here : https://github.com/akermu/emacs-libvterm/issues/304#issue-605130255

If I understand correctly, currently the mark is not set when doing C-SPC, but instead the command is directly send to vterm since C-SPC is bound to vterm--self-insert.

So one would need to add the mark somehow and then define something like vterm-send-M-w which would do something like the above mentioned issue.

e-hoarder commented 4 months ago

I haven't tested it at all but this could work for M-w


_kill_ring_save(){
    zle .copy-region-as-kill
    printf "%s" "${CUTBUFFER}" | osc-copy
    zle .neg-argument 1
    zle .set-mark-command
}
zle -N kill-ring-save _kill_ring_save
bindkey '^@' .set-mark-command
bindkey '\ew' kill-ring-save
e-hoarder commented 4 months ago

Use any clipboard manager like pbcopy or xclip instead of osc-copy in my example

e-hoarder commented 3 months ago
_kill_region_to_clip() {
  if ((REGION_ACTIVE));  then
    zle .copy-region-as-kill
    osc-copy "${CUTBUFFER}"
    zle .kill-region
  else
    zle .set-mark-command
    zle .backward-word
    zle .exchange-point-and-mark
    zle .copy-region-as-kill
    pbcopy "${CUTBUFFER}"
    zle .backward-kill-word
  fi
  zle .set-mark-command -n -1
}
zle -N kill-region-to-clip _kill_region_to_clip
bindkey '^@' .set-mark-command
bindkey '^w' kill-region-to-clip

You can use this for C-w, it kills the word backwards if there isn't a region but kills the region if there is. If you want the same behavior without killing you can leave out the kill-region and backward-kill-word and bind the result to '\ew'