Pasting text into emacs on Macintosh - emacs

I'm on a Macintosh and am using "terminal" for my shell. When I copy text from any window (via mouse drag then right mouse button menu -> copy) and then I paste the text (right mouse button -> paste) into a terminal with emacs running, it doesn't act as a paste. Instead, it is just like entering or typing in text. The problem occurs when the text is indented. Emacs does its auto-indentation on top of that so I get a cascading staircase-like look of text. I just want it to be a true "paste" so that whatever was copied shows up exactly as it was. Any ideas on how to change something to get this to work?

Try this:
(defun pt-pbpaste ()
"Paste data from pasteboard."
(interactive)
(shell-command-on-region
(point)
(if mark-active (mark) (point))
"pbpaste" nil t))
(defun pt-pbcopy ()
"Copy region to pasteboard."
(interactive)
(print (mark))
(when mark-active
(shell-command-on-region
(point) (mark) "pbcopy")
(kill-buffer "*Shell Command Output*")))
(global-set-key [?\C-x ?\C-y] 'pt-pbpaste)
(global-set-key [?\C-x ?\M-w] 'pt-pbcopy)
Use C-x C-y to paste and C-x M-w to copy.

For a quick and dirty solution which doesn't require configuring custom commands, you can run shell-command with a prefix argument to insert the results of calling pbpaste into the current buffer.
Thus:
C-u M-! pbpaste <RET>

Related

Rebuild cscope in emacs

I have cscope built-in in emacs.
When ever I change the code using emacs. The code change causes cscope to not behave the way I want it to.
eg.
Due to code change If I want to jump to the function definition. cscope does not take me to the definition of the func, instead it takes me to some other line.
Please tell if there is a way to rebuild cscope without closing the emacs window.
You will need https://github.com/dkogan/xcscope.el
and configuration :
(defun my-c-mode-common-hook ()
(require 'xcscope)
(cscope-setup)
(setq cscope-initial-directory "path to the cscope directory"))
(add-hook 'c-mode-common-hook 'my-c-mode-common-hook)
and then
C-c s L (or M-x cscope-create-list-of-files-to-index)
C-c s I (or M-x cscope-index-files) => build or rebuild
Hope this help
I use the following function to build/rebuild cscope database:
(require 'xcscope)
(cscope-setup)
(setq cscope-option-use-inverted-index t)
(defadvice cscope-bury-buffer (after cscope-bury-buffer activate)
"Kill the *cscope* window after hitting q or Q instead of leaving it open."
(delete-window))
(defun cscope-create-database (top-directory)
"Create cscope* files in one step containing, do this before using cscope:
1. C-c s L
2. C-c s I
3. C-c s a
"
(interactive "DCreate cscope* database files in directory: ")
(progn
(cscope-create-list-of-files-to-index top-directory)
(cscope-index-files top-directory)
(setq cscope-initial-directory top-directory)
(sit-for 2)
(delete-windows-on "*cscope-indexing-buffer*")
(kill-buffer "*cscope-indexing-buffer*")
))
(bind-keys*
("C-c s r" . cscope-create-database))

How to copy path of a file in emacs helm-mode

I'm using helm-mode in emacs to open files. However, when I try to copy the path of a file (say /home/user1/Documents/file1.txt) through mouse left-click and hold to paste it in terminal, I get a message saying
<down-mouse-1> is undefined
I guess helm does not support mouse operations as described here, in which case how can I copy path of a file from emacs (in helm-mode) to paste it in terminal
The answer given in this other thread may seem more straightforward.
In short: with the file selected in the minibuffer use C-u C-c C-k. This invokes helm-kill-selection-and-quit. The file's full path is copied to the kill ring and can be pasted in Emacs or elsewhere.
I guess you want to copy from Minibuffer to your system clipboard. Minibuffer keybinding isn't different from other buffers. If in other buffers you use M-w to copy the region, it should also work in Minibuffer. Note that if you niled x-select-enable-clipboard you need to enable it first. I have the following functions in my init.el
(defun copy-to-clipboard()
(interactive)
(setq x-select-enable-clipboard t)
(kill-ring-save (region-beginning) (region-end))
(setq x-select-enable-clipboard nil))
and
(defun paste-from-clipboard ()
(interactive)
(setq x-select-enable-clipboard t)
(yank)
(setq x-select-enable-clipboard nil))
Unfortunately you can't use your mouse to select the texts (ie. to make a region) in helm-mode; you need to set-mark-command (by default C-SPC or C-#) and move your point (ie. cursor). Or just hold the shift and move the point like most other text editors. There is also a mark-word command (by default M-#) that expands the region word by word.
I also recorded an asciinema (because they're fun 🙂) that you can watch it here

Open dired and select the file associated with the previous buffer?

Let's say I am editing blah.txt with Emacs and I decide to open dired to rename the file blah.txt. When I press C-x d RET (or C-x C-f RET), a dired buffer will show up to display the content of the directory containing blah.txt, but the cursor will not be on blah.txt. So I need to search my file first (C-s blah.txt) to place my cursor on it and then I can rename it (R).
How do I automate or remove the step C-s blah.txt?
dired-jump is exactly what you want.
(autoload 'dired-jump "dired-x" "Jump to dired corresponding current buffer.")
(autoload 'dired-jump-other-window "dired-x" "jump to dired in other window.")
Then call:
M-x dired-jump
or
M-x dired-jump-other-window
You want C-x C-j.
Sunrise Commander is a much improved dired. and it does what you need by default.
You can do something like that:
M-: (dired (buffer-name (current-buffer)))
Then the only file visible in dired will be your current file and cursor will be right on it.
This piece of advice will do what you want:
(defadvice dired (around dired-jump-to-buffer activate)
"When running dired, move cursor to the line for the buffer we came from"
(interactive (list nil nil)) ;; bogus values, will be overwritten below
(let ((coming-from (buffer-file-name)))
(ad-set-args 0 (dired-read-dir-and-switches ""))
ad-do-it
(when (and coming-from
(equal (file-truename default-directory) (file-truename (file-name-directory coming-from))))
(goto-char (point-min))
(search-forward (file-name-nondirectory coming-from) nil t))))
Note: Works for C-x d, but not the C-x C-f entry point to dired.
$ emacs --version
GNU Emacs 24.3.1
In .emacs:
(require 'dired-x)
Now C-x C-j should be bound to dired-jump.

How to open multiple terminals?

In Emacs, I often find myself in a situation where I need to jump back and forth between various source files to various terminals. However, I feel like I do not have a good way to do this efficiently and it's clumsy that you can only open one shell in Emacs (shell, eshell, or term).
Moreover, I need an efficient way of juggle between multiple terminals and source files.
How can I achieve this?
You can have as many terminals and shells open at once as you want. Just use M-x rename-buffer to change the name of an existing *term* or *shell* buffer, and the next time you do M-x term or M-x shell, a brand new buffer will be created. In the case of M-x shell, a prefix argument will cause you to be prompted for the name of the new shell buffer, as offby1 noted.
A few years ago I had a job where I had to regularly log in to various production servers named "host01.foo.com", "host02.foo.com", etc. I wrote a little function like this one to make it easier to manage them all:
(defun ssh-to-host (num)
(interactive "P")
(let* ((buffer-name (format "*host%02d*" num))
(buffer (get-buffer buffer-name)))
(if buffer
(switch-to-buffer buffer)
(term "/bin/bash")
(term-send-string
(get-buffer-process (rename-buffer buffer-name))
(format "ssh host%02d.foo.com\r" num)))))
Then I bound this command to (say) s-h (super H), enabling me to just type M-5 s-h. If I didn't already have a buffer named *host05*, it would start a new terminal emulator buffer, rename it to *host05*, and ssh me into host05.foo.com. If buffer *host05* already existed, it would simply switch me to it. Quite handy!
You can certainly have multiple interactive shells open. Try typing C-u M-x shell RET RET.
Try using MultiTerm to open multiple shells.
You can use Emacs Lisp Screen, which emulates GNU Screen and provides easy key bindings to jump to and between a number of different shells.
I use many methods for incorporating my terminal life into Emacs:
elscreen.el is a life saver, if you have a complicated window layout like gdb or have simply become overwhelmed with clutter you just open a new screen. In your case you could dedicate one screen to terminals.
multi-term.el makes managing terminals a bit easier.
shell-pop.el, a great tool for quick terminal access. shell-pop lets you assign a key to opening and closing a specific shell buffer window, if you've used drop-down terminals like tilda you know how incredibly handy this can be:
Here's and example of my shell-pop configuration, I use the key C-t to pop up an eshell:
(require 'shell-pop)
(shell-pop-set-internal-mode "eshell") ; Or "ansi-term" if you prefer
(shell-pop-set-window-height 60) ; Give shell buffer 60% of window
;; If you use "ansi-term" and want to use C-t
;; (defvar ansi-term-after-hook nil)
;; (add-hook 'ansi-term-after-hook
;; '(lambda ()
;; (define-key term-raw-map (kbd "C-t") 'shell-pop)))
;; (defadvice ansi-term (after ansi-term-after-advice (org))
;; (run-hooks 'ansi-term-after-hook))
;; (ad-activate 'ansi-term)
(global-set-key (kbd "C-t") 'shell-pop)
I usually do an an M-x server-start and then use emacsclient --no-wait to open files. I've aliased that to e with some embellishments so that it's a little more convenient.
I do all my work in a single terminal and just "throw" the files I want to edit into Emacs using e. Inside Emacs, I juggle around using iswitchb and it works just fine. YMMV.
I regularly used 10 or so shells in my old workplace. The secret is you have to rename additional shell buffers. I did this automatically though in my .emacs, creating and naming the shells logically (I had projnameRun and projnameBuild for every project). Worked really well together with anything, making it very easy to refind the right shell (you use the end of the project name combined with either r or b for run/build).
Instead of having several terminal windows in emacs, I spawn a different xterm whenever I need a new terminal. This of course is bearable because I use a very lightweight terminal emulator (urxvt) which starts in under 0.2s.
Then I use my window manager to switch between them and emacs frames. A configurable window manager will have plenty of options to tune to switch between windows (extremely) efficiently. Inside emacs, I use windmove and ido-mode, and have bound to C-tab a function that switches to the last buffer (because I use C-x b in that fashion a lot).
So um, not sure how useful it is to you since it's quite different from your use pattern, but this is what works for me.
I had exactly the same problem some years ago, and found nothing that satisfied me; so I wrote my own "toggle shell" function. It toggles between the current frame or window configuration and a system shell buffer. It can also put the shell into a dedicated frame, and inject a pushd to the current buffer directory.
This is an excerpt from my .emacs:
(defvar --toggle-shell-last-window-conf nil "The last window configuration.")
(defvar --toggle-shell-last-buf nil "The last buffer object in case there's no last window configuration.")
(defvar --toggle-shell-last-frame nil "The frame that was selected when opening a shell buffer.")
(defun --toggle-shell-have-conf ()
(window-configuration-p --toggle-shell-last-window-conf))
(defun --toggle-shell-store-last-conf ()
(setq --toggle-shell-last-buf (current-buffer)
--toggle-shell-last-frame (selected-frame)
--toggle-shell-last-window-conf (current-window-configuration)))
(defun --toggle-shell-restore-last-conf ()
(if (--toggle-shell-have-conf)
(progn (raise-frame --toggle-shell-last-frame)
(set-window-configuration --toggle-shell-last-window-conf))
(let ((bufnam (if (bufferp --toggle-shell-last-buf)
(buffer-name --toggle-shell-last-buf) --toggle-shell-last-buf)))
(if bufnam
(if (get-buffer bufnam) (switch-to-buffer bufnam t)
(message "%s: buffer not available" bufnam))))))
(defun --toggle-shell (&optional display inject-cd)
"Toggles between current buffers and a system shell buffer. With prefix-arg
close the shell.
When DISPLAY is 'vertical splits the shell as vertical window; when 'frame uses
a dedicated frame (default: single window). When INJECT-CD executes a `pushd'
to the working directory of the buffer from which you toggled the shell."
(interactive)
(let* ((shell-buf (get-buffer "*shell*"))
(shell-window ; non-nil when currently displayed
(if shell-buf (get-buffer-window shell-buf t)))
(shell-frame
(if shell-window (window-frame shell-window)))
(in-shell (eq (current-buffer) shell-buf))
(vertical (string= display 'vertical))
(popup-frame (or (string= display 'frame)
(and inject-cd (not (bufferp shell-buf)))
(and (framep shell-frame)
(not (eq shell-frame (selected-frame)))))))
;; With prefix-arg close shell, restore windows. Otherwise (no prefix-arg)
;; toggle shell window; restore windows when called twice in a row, or the
;; current buffer is the shell buffer (`in-shell').
(if current-prefix-arg
(if (bufferp shell-buf)
(progn (message "Exiting shell '%s'" (buffer-name shell-buf))
(kill-buffer shell-buf)
(if in-shell (--toggle-shell-restore-last-conf)))
(error "No shell buffer to kill."))
;; If already in shell-buffer toggle back to stored frame-configuration.
(if (and in-shell (not inject-cd))
(progn
(--toggle-shell-restore-last-conf)
;; Recurse to reopen the shell-buffer in a dedicated frame, or
;; close the dedicated frame and reopen the buffer in a window.
(if (and popup-frame (eq shell-frame (selected-frame)))
(--toggle-shell 'frame inject-cd)
(when (and popup-frame shell-frame)
(delete-frame shell-frame)
(--toggle-shell nil inject-cd))))
;; Not in shell buffer. Warp to it or create new one.
(unless in-shell
(--toggle-shell-store-last-conf))
(if popup-frame
(progn (switch-to-buffer-other-frame (or shell-buf "*shell*"))
(raise-frame
(or shell-frame (window-frame (get-buffer-window "*shell*" t)))))
(if (> (count-windows) 1)
(delete-other-windows)))
;; Finally `cd' into the working directory the current buffer.
(let ((new-shell (not (bufferp shell-buf)))
(new-dir ; `default-directory' of `--toggle-shell-last-buf'
(if --toggle-shell-last-buf
(buffer-local-value 'default-directory --toggle-shell-last-buf))))
;; Open shell, move point to end-of-buffer. The new shell-buffer's
;; `default-directory' will be that of the buffer the shell was
;; launched from.
(when vertical
(if (> (count-windows) 1)
(delete-other-windows))
(split-window-vertically) (other-window 1))
(funcall 'shell)
(when new-shell
(message "New shell %s (%s)" (buffer-name (current-buffer)) new-dir)
(if inject-cd (sit-for 2))) ; wait for prompt
(goto-char (point-max))
;; If on a command-prompt insert and launch a "cd" command (assume no
;; job is running).
(when (and inject-cd new-dir)
(save-excursion
(backward-line-nomark) (end-of-line)
(unless (setq inject-cd (re-search-forward comint-prompt-regexp (point-max) t))
(error "Cannot `pushd', shell is busy")))
(when (and inject-cd)
(let* ((cmd (format
"pushd '%s' %s" (comint-quote-filename new-dir)
(if (buffer-file-name --toggle-shell-last-buf)
(format "# '%s'" (file-name-directory (buffer-file-name --toggle-shell-last-buf)))
""))))
;; `shell-process-cd' set new `default-directory' and set
;; `shell-last-dir' to old. (If the pushd command is
;; successful, a dirs is performed as well; >nul discards this
;; output.)
(shell-process-cd new-dir)
(insert cmd)
(comint-send-input)
(message "%s: cd '%s'" (buffer-name --toggle-shell-last-buf) new-dir))
)
)
)
)
)
)
)
--toggle-shell is the function that does the trick. I bind it to F12:
;; F12 toggle between shell buffer and current window configuration
;; SHIFT-F12 like before, but let shell buffer appear in a dedicated frame
;; ALT-F12 inject a pushd to change to directory of current buffer
;; CTRL-F12 `shell-command'
(global-set-key [(f12)] '--toggle-shell)
(global-set-key [(shift f12)] '(lambda()(interactive)(--toggle-shell 'frame)))
(global-set-key [(meta f12)] '(lambda()(interactive)(--toggle-shell nil t)))
(global-set-key [(meta f10)] '(lambda()(interactive)(--toggle-shell nil t)))
(global-set-key [(control f12)] 'shell-command) ; alias M-!
This is a significant bunch of code to be posted here. But it shall work well.
Semi related - you can quickly run a shell command on selected file with
M+shift+!
It saves a lot of time for smaller commands chmod etc
And maybe my quick pop-up shell also might help you. A quick pop-up shell for emacs
Ecb + eshell will be what you want exactly!
I use vi, but hope this helps. I can open as many terminals as I want by (eg. in Ubuntu 16.04):
ctrl + alt + t
I usually open 2 terminals, and move (position) one terminal to the right by:
ctrl + super + right-arrow
and move the other terminal to the left by:
ctrl + super + left-arrow
so that I have a divided screen by 2 terminals.

How to copy text from Emacs to another application on Linux

When I cut (kill) text in Emacs 22.1.1 (in its own window on X, in KDE, on Kubuntu), I can't paste (yank) it in any other application.
Let's be careful with our definitions here
An Emacs copy is the command kill-ring-save (usually bound to M-w).
A system copy is what you typically get from pressing C-c (or choosing "Edit->Copy" in a application window).
An X copy is "physically" highlighting text with the mouse cursor.
An Emacs paste is the command yank (usually bound to C-y).
A system paste is what you typically get from pressing C-v (or choosing "Edit-Paste" in an application window).
An X paste is pressing the "center mouse button" (simulated by pressing the left and right mouse buttons together).
In my case (on GNOME):
Both Emacs and system copy usually work with X paste.
X copy usually works with Emacs paste.
To make system copy work with Emacs paste and Emacs copy work with system paste, you need to add (setq x-select-enable-clipboard t) to your .emacs. Or try
META-X set-variable RET x-select-enable-clipboard RET t
I think this is pretty standard modern Unix behavior.
It's also important to note (though you say you're using Emacs in a separate window) that when Emacs is running in a console, it is completely divorced from the system and X clipboards: cut and paste in that case is mediated by the terminal. For example, "Edit->Paste" in your terminal window should act exactly as if you typed the text from the clipboard into the Emacs buffer.
Insert the following into your .emacs file:
(setq x-select-enable-clipboard t)
The difficulty with copy and paste in Emacs is that you want it to work independently from the internal kill/yank, and you want it to work both in terminal and the gui. There are existing robust solutions for either terminal or gui, but not both. After installing xsel (e.g. sudo apt-get install xsel), here is what I do for copy and paste to combine them:
(defun copy-to-clipboard ()
(interactive)
(if (display-graphic-p)
(progn
(message "Yanked region to x-clipboard!")
(call-interactively 'clipboard-kill-ring-save)
)
(if (region-active-p)
(progn
(shell-command-on-region (region-beginning) (region-end) "xsel -i -b")
(message "Yanked region to clipboard!")
(deactivate-mark))
(message "No region active; can't yank to clipboard!")))
)
(defun paste-from-clipboard ()
(interactive)
(if (display-graphic-p)
(progn
(clipboard-yank)
(message "graphics active")
)
(insert (shell-command-to-string "xsel -o -b"))
)
)
(global-set-key [f8] 'copy-to-clipboard)
(global-set-key [f9] 'paste-from-clipboard)
I stick this in my .emacs:
(setq x-select-enable-clipboard t)
(setq interprogram-paste-function 'x-cut-buffer-or-selection-value)
I subsequently have basically no problems cutting and pasting back and forth from anything in Emacs to any other X11 or Gnome application.
Bonus: to get these things to happen in Emacs without having to reload your whole .emacs, do C-x C-e with the cursor just after the close paren of each of those expressions in the .emacs buffer.
Good luck!
I assume by emacs you are meaning Emacs under X (ie not inside a terminal window).
There are two ways:
(Applies to unix OS's only)
Highlight the desired text with your
mouse (this copies it to the X
clipboard) and then middle click to
paste.
Highlight the desired text and then "M-x clipboard-kill-ring-save"
(note you can bind this to an easier
key). Then just "Edit->Paste" in
your favorite app.
Clipboard operations available:
clipboard-kill-ring-save -- copy
selection from Emacs to clipboard
clipboard-kill-region -- cut
selection from Emacs to clipboard
clipboard-yank -- paste from
clipboard to Emacs
There is an EmacsWiki article that explains some issues with copy & pasting under X and how to configure it to work.
This works with M-w on Mac OSX. Just add to your .emacs file.
(defun copy-from-osx ()
(shell-command-to-string "pbpaste"))
(defun paste-to-osx (text &optional push)
(let ((process-connection-type nil))
(let ((proc (start-process "pbcopy" "*Messages*" "pbcopy")))
(process-send-string proc text)
(process-send-eof proc))))
(setq interprogram-cut-function 'paste-to-osx)
(setq interprogram-paste-function 'copy-from-osx)
Source https://gist.github.com/the-kenny/267162
I use the following, based on the other answers here, to make C-x C-w and C-x C-y be copy and paste on both Mac and Linux (if someone knows the version for Windows feel free to add it). Note that on Linux you will have to install xsel and xclip with your package manager.
;; Commands to interact with the clipboard
(defun osx-copy (beg end)
(interactive "r")
(call-process-region beg end "pbcopy"))
(defun osx-paste ()
(interactive)
(if (region-active-p) (delete-region (region-beginning) (region-end)) nil)
(call-process "pbpaste" nil t nil))
(defun linux-copy (beg end)
(interactive "r")
(call-process-region beg end "xclip" nil nil nil "-selection" "c"))
(defun linux-paste ()
(interactive)
(if (region-active-p) (delete-region (region-beginning) (region-end)) nil)
(call-process "xsel" nil t nil "-b"))
(cond
((string-equal system-type "darwin") ; Mac OS X
(define-key global-map (kbd "C-x C-w") 'osx-copy)
(define-key global-map (kbd "C-x C-y") 'osx-paste))
((string-equal system-type "gnu/linux") ; linux
(define-key global-map (kbd "C-x C-w") 'linux-copy)
(define-key global-map (kbd "C-x C-y") 'linux-paste)))
The code below, inspired by #RussellStewart's answer above, adds support for x-PRIMARY and x-SECONDARY, replaces region-active-p with use-region-p to cover the case of an empty region, does not return silently if xsel has not been installed (returns an error message), and includes a "cut" function (emacs C-y, windows C-x).
(defun my-copy-to-xclipboard(arg)
(interactive "P")
(cond
((not (use-region-p))
(message "Nothing to yank to X-clipboard"))
((and (not (display-graphic-p))
(/= 0 (shell-command-on-region
(region-beginning) (region-end) "xsel -i -b")))
(error "Is program `xsel' installed?"))
(t
(when (display-graphic-p)
(call-interactively 'clipboard-kill-ring-save))
(message "Yanked region to X-clipboard")
(when arg
(kill-region (region-beginning) (region-end)))
(deactivate-mark))))
(defun my-cut-to-xclipboard()
(interactive)
(my-copy-to-xclipboard t))
(defun my-paste-from-xclipboard()
"Uses shell command `xsel -o' to paste from x-clipboard. With
one prefix arg, pastes from X-PRIMARY, and with two prefix args,
pastes from X-SECONDARY."
(interactive)
(if (display-graphic-p)
(clipboard-yank)
(let*
((opt (prefix-numeric-value current-prefix-arg))
(opt (cond
((= 1 opt) "b")
((= 4 opt) "p")
((= 16 opt) "s"))))
(insert (shell-command-to-string (concat "xsel -o -" opt))))))
(global-set-key (kbd "C-c C-w") 'my-cut-to-xclipboard)
(global-set-key (kbd "C-c M-w") 'my-copy-to-xclipboard)
(global-set-key (kbd "C-c C-y") 'my-paste-from-xclipboard)
Hmm, what platform and what version of emacs are you using? With GNU Emacs 22.1.1 on Windows Vista, it works fine for me.
If, by any chance, you are doing this from windows to linux through a RealVNC viewer, make sure you are running "vncconfig -iconic" on the linux box first.....
I always use quick paste -- drag selection in emacs, hit the middle mouse button in target window.
(From the reference to kate, I take it you're on linux or similar and probably using emacs in X one way or another.)
You might want to specify what platform you are using. Is it on linux, unix, macosx, windows, ms-dos?
I believe that for windows it should work. For MacOSX it will get added to the x-windows clipboard, which isn't the same thing as the macosx clipboard. For Linux, it depends on your flavour of window manager, but I believe that x-windows handles it in a nice way on most of them.
So, please specify.
What I do is to use a good terminal tool (PuTTY on Windows, Konsole or Terminal on Linux) that has copy facilities built-in.
In PuTTY, you highlight the text you want with the mouse and then paste it elsewhere. Right-clicking in a PuTTY window pastes the contents of the Windows copy/paste buffer.
In Konsole or Terminal on Linux, you highlight what you want then press Shift+Ctrl+C for copy and Shift+Ctrl+V for paste.
In the win32 compile of emacs, yanking text does put it on the copy/paste buffer .. most of the time.
On Mac OS X, the Apple-key chortcuts work fine, because Terminal traps them.
There is no direct way of doing it on the commandline because the shell does not maintain a copy/paste buffer for each application. bash does maintain a copy/paste buffer for itself, and, by default, emacs ^k/^y shortcuts work.