Storm's Free Software Collection
CUA-mode is a complete replacement for the pc-select, delete-selection,
and s-region packages, with one important improvement: it (almost)
transparently allows you to use the C-z, C-x, C-c, and C-v keys as
you are accustomed to on systems like Windows.
Release 2 adds integrated rectangle and register support,
as well as a global mark feature - all using C-x, C-c and C-v
rather than the normal plethora of obscure key sequences! Read on...
You can also use the CUA enhancements without rebinding the
C-z, C-x, C-c, and C-v keys if you activate it using (CUA-mode 'emacs).
Release 2.x of CUA-mode currently works with GNU Emacs 20.x and 21.1 only.
You can get the older release 1.7 for GNU emacs
here.
Xemacs is supported by
this older version (1.3).
Notice:
As of release 2.4, the file name for CUA-mode has been changed from
cua-mode.el to simply cua.el. If you are already using CUA-mode,
you may either continue using the file name cua-mode.el, or you
must change your .emacs file to specify (require 'cua).
Description
This is the CUA-mode package which provides a complete emulatation of
the standard CUA key bindings (Motif/Windows/Mac GUI) for selecting
and manipulating the region where S-<movement> is used to
highlight & extend the region.
This package allow the C-z, C-x, C-c, and C-v keys to be
bound appropriately according to the Motif/Windows GUI, i.e.
Emacs Key | CUA Key | Function |
C-_ | C-z | undo |
C-w | C-x | cut |
M-w | C-c | copy |
C-y | C-v | paste |
The tricky part is the handling of the C-x and C-c keys which
are normally used as prefix keys for most of emacs' built-in
commands. With CUA-mode they still do!!!
Only when the region is currently active (and highlighted since
transient-mark-mode is used), the C-x and C-c keys will work as CUA
keys, i.e. cut and copy, but when the region is not active, C-x and
C-c works as ordinary emacs prefix keys!
This probably sounds strange and difficult to get used to - but
based on my own experience and the feedback from many users of
CUA-mode, it actually works very well and users adapt to it
instantly - or at least very quickly. So give it a try!
... and in the few cases where you make a mistake and accidentally
delete the region - you just undo the mistake (with C-z).
If you really need to perform a command which starts with one of
the prefix keys even when the region is active, you have three options:
- Press the prefix key twice very quickly (within 0.2 seconds),
- press the prefix key and the following key within 0.2 seconds), or
- use the SHIFT key with the prefix key, i.e. C-X or C-C
In addition to using the shifted movement keys, you can also use
[C-space] to start the region and use unshifted movement keys to extend
it. To cancel the region, use [C-space] or [C-g].
Installation
Place the cua.el file somewhere on your load path, byte compile it,
and insert the following lines in your .emacs file:
(require 'cua)
(CUA-mode t)
If you don't want to modify the bindings of C-z C-x C-c C-v, but still
want the CUA enhanced functionality for the standard emacs bindings of
undo, kill, copy, and yank, use the following lines instead:
(require 'cua)
(CUA-mode 'emacs)
New features
With release 2 of the CUA-mode package, the CUA commands have been
extended to handle both rectangles and registers in a homogeneous
manner, as well as adding the concept of a "global mark" as
described below.
CUA rectangle support
Emacs' normal rectangle support is based on interpreting the region
between the mark and point as a "virtual rectangle", and using a
completely separate set of "rectangle commands" [C-x r ...] on the
region to copy, kill, fill a.s.o. the virtual rectangle.
CUA-mode's superior rectangle support is based on using a true visual
representation of the selected rectangle. To start a rectangle, use
[S-return] and extend it using the normal movement keys (up, down,
left, right, home, end, C-home, C-end). Once the rectangle has the
desired size, you can cut or copy it using C-x and C-c, and you can
subsequently insert it - as a rectangle - using C-v. So the only new
command you need to know to work with CUA-mode rectangles is S-return!
Furthermore, CUA-mode's rectangles are not limited to the actual
contents of the buffer, so if the cursor is currently at the end of a
short line, you can still extend the rectangle to include more columns
of longer lines in the same rectangle. Sounds strange? Try it!
You can enable padding for just this rectangle by pressing [M-p];
this works like entering 'picture mode' where the tabs and spaces
are automatically converted/inserted to make the rectangle truly
rectangular. Or you can do it for all rectangles by setting the
CUA-mode-auto-expand-rectangles variable to 'yes.
And there's more: If you want to extend or reduce the size of the
rectangle in one of the other corners of the rectangle, just use
[return] to move the cursor to the "next" corner. Or you can use
the [M-up], [M-down], [M-left], and [M-right] keys to move the
entire rectangle overlay (but not the contents) in the given
direction.
[S-return] cancels the rectangle
[C-space] activate region bounded by rectangle
If you type a normal (self-inserting) character when the rectangle is
active, the character is inserted on the "current side" of every line
of the rectangle. The "current side" is the side on which the cursor
is currently located. If the rectangle is only 1 column wide,
insertion will be performed to the left when the cursor is at the
bottom of the rectangle. So, for example, to comment out an entire
paragraph like this one, just place the cursor on the first character
of the first line, and enter the following:
[S-return] [down].... [;; ] [S-return]
CUA-mode's rectangle support also includes all the normal rectangle
functions with easy access:
[M-a] | aligns all words at the left edge of the rectangle |
[M-b] | fills the rectangle with blanks (tabs and spaces) |
[M-c] | closes the rectangle by removing all blanks at the left edge
of the rectangle |
[M-f] | fills the rectangle with a single character (prompt) |
[M-F] | performs text filling on the rectangle |
[M-i] | increases the first number found on each line of the rectangle
by the amount given by the numeric prefix argument (default 1)
It recognizes 0x... as hexadecimal numbers |
[M-k] | kills the rectangle as a normal multi-line text (for paste) |
[M-l] | downcases the rectangle |
[M-m] | kills the rectangle as a normal multi-line text (for paste) |
[M-n] | fills each line of the rectangle with increasing numbers using
a supplied format string (prompt) |
[M-o] | opens the rectangle by moving the highlighted text to the
right of the rectangle and filling the rectangle with blanks. |
[M-p] | toggles rectangle padding, i.e. insert tabs and spaces to
make rectangles truly rectangular |
[M-r] | replaces REGEXP (prompt) by STRING (prompt) in rectangle |
[M-R] | reverse the lines in the rectangle |
[M-s] | fills each line of the rectangle with the same STRING (prompt) |
[M-t] | performs text fill of the rectangle with TEXT (prompt) |
[M-u] | upcases the rectangle |
[M-|] | runs shell command on rectangle |
[M-'] | restricts rectangle to lines with CHAR (prompt) at left column |
[M-/] | restricts rectangle to lines matching REGEXP (prompt) |
[C-?] | Shows a brief list of the above commands. |
[M-C-up] [M-C-down] | scrolls the lines INSIDE the rectangle up
and down; lines scrolled outside the top or bottom of the rectangle
are lost, but can be recovered using [C-z]. |
CUA register support
Emacs' standard register support is also based on a separate set of
"register commands".
CUA-mode's register support is activated by providing a numeric
prefix argument to the C-x, C-c, and C-v commands. For example,
to copy the selected region to register 2, enter [M-2 C-c].
Or if you have activated the keypad prefix mode, enter [kp-2 C-c].
And CUA-mode will copy and paste normal region as well as rectangles
into the registers, i.e. you use exactly the same command for both.
In addition, the last highlighted text that is deleted (not copied),
e.g. by typing text over a highlighted region, is automatically saved
in register 0, so you can insert it using [kp-0 C-v].
CUA Global Mark
The final feature provided by CUA-mode is the "global mark", which
makes it very easy to copy bits and pieces from the same and other
files into the current text. To enable and cancel the global mark,
use [S-C-space]. The cursor will blink when the global mark
is active. The following commands behave differently when the global
mark is set:
<ch> | All characters (including returns) you type are inserted
at the global mark! |
[C-x] | If you cut a region or rectangle, it is automatically inserted
at the global mark, and the global mark is advanced. |
[C-c] | If you copy a region or rectangle, it is immediately inserted
at the global mark, and the global mark is advanced. |
[C-v] | Copies a single character to the global mark. |
[C-d] | Moves (i.e. deletes and inserts) a single character to the
global mark. |
[backspace] | deletes the character before the global mark |
[delete] | deltes the character after the global mark |
[S-C-space] | Jumps to and cancels the global mark. |
[C-u S-C-space] | Cancels the global mark (stays in current buffer). |
CUA mode indications
As mentioned above, the cursor will blink when the global mark is
active. In addition, you can choose to let CUA-mode use different
cursor colors to indicate overwrite mode and read-only buffers.
For example, the following setting will use a RED cursor in normal
(insertion) mode in read-write buffers, a YELLOW cursor in
overwrite mode in read-write buffers, and a GREEN cursor read-only
buffers:
(setq CUA-mode-normal-cursor-color "red")
(setq CUA-mode-overwrite-cursor-color "yellow")
(setq CUA-mode-read-only-cursor-color "green")
Additional information
When the region is highlighted, TAB and S-TAB will indent the entire
region by the normal tab-width (or the given prefix argument).
C-x C-x (exchange point and mark) no longer activates the mark (i.e.
highlights the region). I found that to be confusing since the
sequence C-x C-x (exchange once) followed by C-x C-x (change back)
would then cut the region! To activate the region in this way,
use C-u C-x C-x.
[delete] will delete (not copy) the highlighted region.
The highlighted region is automatically deleted if other text is
typed or inserted. The previously highlighted text is saved in
register 0 (if CUA-mode-delete-to-register-0 is non-nil).
You may choose to use M-r as a prefix for the register commands
instead of C-x r by setting CUA-mode-register-commands-prefix.
The original binding of M-r (move-to-window-line) is then moved
to M-r M-r.
Normally, when you paste a rectangle using C-v, each line of the
rectangle is inserted into the existing lines in the buffer.
However, if overwrite-mode is turned on, the lines of the rectangle
are inserted as a single block of lines at point.
CUA-mode works in a very homogeneous way via a versatile
pre-command-hook (like delsel.el).
It does not rebind any of the cursor movement or
scrolling keys; instead it puts different CUA properties on each of these
commands which the pre-command hook reacts to. This has the added bonus of C-h k
key returning the proper binding of key (rather than some
obscure function which messes with the mark, as some of the other
similar packages do).
It uses the transient-mark-mode to highlight the selected region.
The interpretation of C-x and C-c as either emacs
prefix keys or CUA cut/copy keys is handled via emacs'
key-translation-map feature.
Utilizing the numeric keypad
You can also bind the keys in the numeric keypad to work as numeric
prefix keys using the CUA-keypad-mode function as follows:
(CUA-keypad-mode 'prefix t)
This function provides a number of different standard bindings for
the keypad:
(CUA-keypad-mode mode &optional numlock decimal)
This sets the keypad bindings (in the function-key-map) according to MODE.
Mode | Binding |
'prefix | Command prefix argument, i.e. M-0 .. M-9 and M-- |
'cursor | Bind keypad keys to the cursor movement keys. |
'numeric | Plain numeric, i.e. 0 .. 9 and . (or DECIMAL arg) |
'none | Removes all bindings for keypad keys in function-key-map. |
If the optional second argument NUMLOCK is non-nil, the NumLock On bindings
are changed. Otherwise, the NumLock Off binding are changed.
If mode is 'numeric and the optional third argument DECIMAL is non-nil,
the decimal key on the keypad is mapped to DECIMAL instead of [.]."
Acknowledgements
CUA-mode includes code and ideas from several related packages:
pc-selection-mode by Michael Staats
s-region by Morten Welinder
delete-selection-mode by Matthieu Devin
The rectangle handling and display code borrows from the standard
GNU emacs rect.el package and the the rect-mark.el package by Rick
Sladkey .
Revision History
- Revision 2.10, 2002-01-08
Added CUA-mode-remap-cx-shift-only and
CUA-mode-highlight-shift-only customization options to make CUA-mode
less aggressive when the standard emacs bindings are used, i.e. when
the region is started with C-SPC. Specifically, if these options are
set, the special remapping of C-x and C-c and the use of
transient-mark-mode will only be enabled if the region is marked with
shifted movement keys.
- Revision 2.9, 2002-01-04
Rectangles can now be extended using any command which moves the point,
including clicking with mouse-1. If rectangle padding is active, mouse-1
will set extend the rectangle to the exact window position clicked on.
New CUA-cmd-do-rectangle-padding (H-P) to pad rectangle without turning
on padding. Also used to pad rectangle after point has moved is
padding is enabled.
Fixed: The rectangle corners are now updated after padding rectangle.
Fixed: yanking after a copy rectangle followed by a normal kill would
paste the rectangle rather than the killed text.
Fixed: yank-pop after yanking a rectangle didn't work correctly (it
could fail to remove the yanked rectangle, and it would then insert
the previous kill-ring element where the mark happened to be set. However,
this still only works if undo is enabled in the buffer.
Fixed: Bind \r rather than RET in keymaps.
Fixed: Bind \d like backspace in keymaps.
Added CUA-movement-key function for other packages to use.
Added support for begendol.el commands.
- Revision 2.8, 2001-12-09
The C-x 8 prefix (iso-translation) now works with CUA.
- Revision 2.7, 2001-10-31
Handle commands from foreign packages by looking for a
delete-selection property of commands without a CUA property.
- Revision 2.6, 2001-10-15
Handle kill-ring-save (M-w) as CUA-copy-region command.
Do not rebind shifted and unshifted movement commands to keys if
using emacs-bindings, i.e. (CUA-mode 'emacs).
(CUA-mode-bindings 'zxcv) binds C-z C-x C-c and C-v.
(CUA-mode-bindings 'shift) binds (un)shifted movement keys.
- Revision 2.5, 2001-06-27
Standard emacs binding for undo, kill, copy, and yank can now be
used when activated by (CUA-mode 'emacs) .
Alternatively, CUA-mode-emacs-bindings can be set prior to activating CUA-mode.
Fixed: [return] didn't work in mini-buffer when Global Mark is active.
- Revision 2.4, 2001-05-30
Changed package location to www.cua.dk
Changed file name from cua-mode.el to cua.el
- Revision 2.3, 2001-05-03
Modified standard CUA keybindings:
- [C-delete] now runs kill-word rather than kill-line.
- [C-backspace] now runs backward-kill-word.
- [delete] now runs delete-char.
- Revision 2.2, 2001-03-20
Various fixes to rectangle support.
- Revision 2.1, 2000-11-22
Major upgrade.
Added rectangle support, global marker, and registers.
|