0

I'm trying to minimize using of mouse. Copying text in terminal is the mostly inconvenient activity for mouse avoiding. Now, tmux helping me to obtain this purpose. However, I regularly needs to copy text currently entered to bash prompt. Currently entered text means that I typed some symbols in prompt, but didn't press Enter. tmux way for this purpose needs for too much keyboard shortcuts.

I'd like some script, that copying current text from prompt to clipboard. I want to add this script to ~/.bashrc and assign keyboard shortcut to. How to write this script?

6
copy_buffer() {
  printf %s "$READLINE_LINE" | xclip -sel clipboard
}
bind -x '"\C-[c": "copy_buffer"'

Would store the current buffer to the clipboard selection upon pressing Alt-C.

Also note that bash (like all shells), has support for copy-paste inside the prompt. For instance Ctrl+U would kill the full buffer and paste (yank) it, leaving it in the yank buffer for you to yank it again later on would you want to.

See zsh or tcsh for more advanced features.

  • +1 especially for the hint for the bash internal shortcuts. – erik Feb 7 '14 at 15:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.