mark.texi 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. @node Mark, Mouse Selection, Help, Top
  2. @chapter Selecting Text
  3. @cindex mark
  4. @cindex region
  5. Many Emacs commands operate on an arbitrary contiguous
  6. part of the current buffer. You can select text in two ways:
  7. @itemize @bullet
  8. @item
  9. You use special keys to select text by defining a region between point
  10. and the mark.
  11. @item
  12. If you are running XEmacs under X, you can also select text
  13. with the mouse.
  14. @end itemize
  15. @section The Mark and the Region
  16. To specify the text for a command to operate on, set @dfn{the
  17. mark} at one end of it, and move point to the other end. The text
  18. between point and the mark is called @dfn{the region}. You can move
  19. point or the mark to adjust the boundaries of the region. It doesn't
  20. matter which one is set first chronologically, or which one comes
  21. earlier in the text.
  22. Once the mark has been set, it remains until it is set again at
  23. another place. The mark remains fixed with respect to the preceding
  24. character if text is inserted or deleted in a buffer. Each Emacs
  25. buffer has its own mark; when you return to a buffer that had been
  26. selected previously, it has the same mark it had before.
  27. Many commands that insert text, such as @kbd{C-y} (@code{yank}) and
  28. @kbd{M-x insert-buffer}, position the mark at one end of the inserted
  29. text---the opposite end from where point is positioned, so that the region
  30. contains the text just inserted.
  31. Aside from delimiting the region, the mark is useful for marking
  32. a spot that you may want to go back to. To make this feature more useful,
  33. Emacs remembers 16 previous locations of the mark in the @code{mark ring}.
  34. @menu
  35. * Setting Mark:: Commands to set the mark.
  36. * Using Region:: Summary of ways to operate on contents of the region.
  37. * Marking Objects:: Commands to put region around textual units.
  38. * Mark Ring:: Previous mark positions saved so you can go back there.
  39. @end menu
  40. @node Setting Mark, Using Region, Mark, Mark
  41. @subsection Setting the Mark
  42. Here are some commands for setting the mark:
  43. @c WideCommands
  44. @table @kbd
  45. @item C-@key{SPC}
  46. Set the mark where point is (@code{set-mark-command}).
  47. @item C-@@
  48. The same.
  49. @item C-x C-x
  50. Interchange mark and point (@code{exchange-point-and-mark}).
  51. @item C-<
  52. Pushes a mark at the beginning of the buffer.
  53. @item C->
  54. Pushes a mark at the end of the buffer.
  55. @end table
  56. For example, to convert part of the buffer to all
  57. upper-case, you can use the @kbd{C-x C-u} (@code{upcase-region})
  58. command, which operates on the text in the region. First go to the
  59. beginning of the text you want to capitalize and type @kbd{C-@key{SPC}} to
  60. put the mark there, then move to the end, and then type @kbd{C-x C-u} to
  61. capitalize the selected region. You can also set the mark at the end of the
  62. text, move to the beginning, and then type @kbd{C-x C-u}. Most commands
  63. that operate on the text in the region have the word @code{region} in
  64. their names.
  65. @kindex C-SPC
  66. @findex set-mark-command
  67. The most common way to set the mark is with the @kbd{C-@key{SPC}}
  68. command (@code{set-mark-command}). This command sets the mark where
  69. point is. You can then move point away, leaving the mark behind. It is
  70. actually incorrect to speak of the character @kbd{C-@key{SPC}}; there is
  71. no such character. When you type @key{SPC} while holding down
  72. @key{CTRL}, you get the character @kbd{C-@@} on most terminals. This
  73. character is actually bound to @code{set-mark-command}. But unless you are
  74. unlucky enough to have a terminal where typing @kbd{C-@key{SPC}} does
  75. not produce @kbd{C-@@}, you should think of this character as
  76. @kbd{C-@key{SPC}}.
  77. @kindex C-x C-x
  78. @findex exchange-point-and-mark
  79. Since terminals have only one cursor, Emacs cannot show you where the
  80. mark is located. Most people use the mark soon after they set it, before
  81. they forget where it is. But you can see where the mark is with the
  82. command @kbd{C-x C-x} (@code{exchange-point-and-mark}) which puts the
  83. mark where point was and point where the mark was. The extent of the
  84. region is unchanged, but the cursor and point are now at the previous
  85. location of the mark.
  86. @kindex C-<
  87. @kindex C->
  88. @findex mark-beginning-of-buffer
  89. @findex mark-end-of-buffer
  90. Another way to set the mark is to push the mark to the beginning of a
  91. buffer while leaving point at its original location. If you supply an
  92. argument to @kbd{C-<} (@code{mark-beginning-of-buffer}), the mark is pushed
  93. @var{n}/10 of the way from the true beginning of the buffer. You can
  94. also set the mark at the end of a buffer with @kbd{C->}
  95. (@code{mark-end-of-buffer}). It pushes the mark to the end of the buffer,
  96. leaving point alone. Supplying an argument to the command pushes the mark
  97. @var{n}/10 of the way from the true end of the buffer.
  98. If you are using XEmacs under the X window system, you can set
  99. the variable @code{zmacs-regions} to @code{t}. This makes the current
  100. region (defined by point and mark) highlight and makes it available as
  101. the X clipboard selection, which means you can use the menu bar items on
  102. it. @xref{Active Regions}, for more information.
  103. @kbd{C-x C-x} is also useful when you are satisfied with the location of
  104. point but want to move the mark; do @kbd{C-x C-x} to put point there and
  105. then you can move it. A second use of @kbd{C-x C-x}, if necessary, puts
  106. the mark at the new location with point back at its original location.
  107. @node Using Region, Marking Objects, Setting Mark, Mark
  108. @subsection Operating on the Region
  109. Once you have created an active region, you can do many things to
  110. the text in it:
  111. @itemize @bullet
  112. @item
  113. Kill it with @kbd{C-w} (@pxref{Killing}).
  114. @item
  115. Save it in a register with @kbd{C-x r s} (@pxref{Registers}).
  116. @item
  117. Save it in a buffer or a file (@pxref{Accumulating Text}).
  118. @item
  119. Convert case with @kbd{C-x C-l} or @kbd{C-x C-u} @*(@pxref{Case}).
  120. @item
  121. Evaluate it as Lisp code with @kbd{M-x eval-region} (@pxref{Lisp Eval}).
  122. @item
  123. Fill it as text with @kbd{M-q} (@pxref{Filling}).
  124. @item
  125. Print hardcopy with @kbd{M-x print-region} (@pxref{Hardcopy}).
  126. @item
  127. Indent it with @kbd{C-x @key{TAB}} or @kbd{C-M-\} (@pxref{Indentation}).
  128. @end itemize
  129. @node Marking Objects, Mark Ring, Using Region, Mark
  130. @subsection Commands to Mark Textual Objects
  131. There are commands for placing point and the mark around a textual
  132. object such as a word, list, paragraph or page.
  133. @table @kbd
  134. @item M-@@
  135. Set mark after end of next word (@code{mark-word}). This command and
  136. the following one do not move point.
  137. @item C-M-@@
  138. Set mark after end of next Lisp expression (@code{mark-sexp}).
  139. @item M-h
  140. Put region around current paragraph (@code{mark-paragraph}).
  141. @item C-M-h
  142. Put region around current Lisp defun (@code{mark-defun}).
  143. @item C-x h
  144. Put region around entire buffer (@code{mark-whole-buffer}).
  145. @item C-x C-p
  146. Put region around current page (@code{mark-page}).
  147. @end table
  148. @kindex M-@@
  149. @kindex C-M-@@
  150. @findex mark-word
  151. @findex mark-sexp
  152. @kbd{M-@@} (@code{mark-word}) puts the mark at the end of the next word,
  153. while @kbd{C-M-@@} (@code{mark-sexp}) puts it at the end of the next Lisp
  154. expression. These characters sometimes save you some typing.
  155. @kindex M-h
  156. @kindex C-M-h
  157. @kindex C-x C-p
  158. @kindex C-x h
  159. @findex mark-paragraph
  160. @findex mark-defun
  161. @findex mark-page
  162. @findex mark-whole-buffer
  163. A number of commands are available that set both point and mark and
  164. thus delimit an object in the buffer. @kbd{M-h} (@code{mark-paragraph})
  165. moves point to the beginning of the paragraph that surrounds or follows
  166. point, and puts the mark at the end of that paragraph
  167. (@pxref{Paragraphs}). You can then indent, case-convert, or kill the
  168. whole paragraph. In the same fashion, @kbd{C-M-h} (@code{mark-defun})
  169. puts point before and the mark after the current or following defun
  170. (@pxref{Defuns}). @kbd{C-x C-p} (@code{mark-page}) puts point before
  171. the current page (or the next or previous, depending on the argument),
  172. and mark at the end (@pxref{Pages}). The mark goes after the
  173. terminating page delimiter (to include it), while point goes after the
  174. preceding page delimiter (to exclude it). Finally, @kbd{C-x h}
  175. (@code{mark-whole-buffer}) sets up the entire buffer as the region by
  176. putting point at the beginning and the mark at the end.
  177. @node Mark Ring,, Marking Objects, Mark
  178. @subsection The Mark Ring
  179. @kindex C-u C-SPC
  180. @cindex mark ring
  181. @kindex C-u C-@@
  182. Aside from delimiting the region, the mark is also useful for marking
  183. a spot that you may want to go back to. To make this feature more
  184. useful, Emacs remembers 16 previous locations of the mark in the
  185. @dfn{mark ring}. Most commands that set the mark push the old mark onto
  186. this ring. To return to a marked location, use @kbd{C-u C-@key{SPC}}
  187. (or @kbd{C-u C-@@}); this is the command @code{set-mark-command} given a
  188. numeric argument. The command moves point to where the mark was, and
  189. restores the mark from the ring of former marks. Repeated use of this
  190. command moves point to all the old marks on the ring, one by one.
  191. The marks you have seen go to the end of the ring, so no marks are lost.
  192. Each buffer has its own mark ring. All editing commands use the current
  193. buffer's mark ring. In particular, @kbd{C-u C-@key{SPC}} always stays in
  194. the same buffer.
  195. Many commands that can move long distances, such as @kbd{M-<}
  196. (@code{beginning-of-buffer}), start by setting the mark and saving the
  197. old mark on the mark ring. This makes it easier for you to move back
  198. later. Searches set the mark, unless they do not actually move point.
  199. When a command sets the mark, @samp{Mark Set} is printed in the
  200. echo area.
  201. @vindex mark-ring-max
  202. The variable @code{mark-ring-max} is the maximum number of entries to
  203. keep in the mark ring. If that many entries exist and another entry is
  204. added, the last entry in the list is discarded. Repeating @kbd{C-u
  205. C-@key{SPC}} circulates through the entries that are currently in the
  206. ring.
  207. @vindex mark-ring
  208. The variable @code{mark-ring} holds the mark ring itself, as a list of
  209. marker objects in the order most recent first. This variable is local
  210. in every buffer.