mark.texi 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. @c This is part of the Emacs manual.
  2. @c Copyright (C) 1985-1987, 1993-1995, 1997, 2001-2017 Free Software
  3. @c Foundation, Inc.
  4. @c See file emacs.texi for copying conditions.
  5. @node Mark
  6. @chapter The Mark and the Region
  7. @cindex mark
  8. @cindex setting a mark
  9. @cindex region
  10. Many Emacs commands operate on an arbitrary contiguous part of the
  11. current buffer. To specify the text for such a command to operate on,
  12. you set @dfn{the mark} at one end of it, and move point to the other
  13. end. The text between point and the mark is called @dfn{the region}.
  14. The region always extends between point and the mark, no matter which
  15. one comes earlier in the text; each time you move point, the region
  16. changes.
  17. @cindex active region
  18. Setting the mark at a position in the text also @dfn{activates} it.
  19. When the mark is active, we say also that the region is active; Emacs
  20. indicates its extent by highlighting the text within it, using the
  21. @code{region} face (@pxref{Face Customization}).
  22. After certain non-motion commands, including any command that
  23. changes the text in the buffer, Emacs automatically @dfn{deactivates}
  24. the mark; this turns off the highlighting. You can also explicitly
  25. deactivate the mark at any time, by typing @kbd{C-g}
  26. (@pxref{Quitting}).
  27. The above default behavior is known as Transient Mark mode.
  28. Disabling Transient Mark mode switches Emacs to an alternative
  29. behavior, in which the region is usually not highlighted.
  30. @xref{Disabled Transient Mark}.
  31. @vindex highlight-nonselected-windows
  32. Setting the mark in one buffer has no effect on the marks in other
  33. buffers. When you return to a buffer with an active mark, the mark is
  34. at the same place as before. When multiple windows show the same
  35. buffer, they can have different values of point, and thus different
  36. regions, but they all share one common mark position. @xref{Windows}.
  37. Ordinarily, only the selected window highlights its region; however,
  38. if the variable @code{highlight-nonselected-windows} is
  39. non-@code{nil}, each window highlights its own region.
  40. There is another kind of region: the rectangular region.
  41. @xref{Rectangles}.
  42. @menu
  43. * Setting Mark:: Commands to set the mark.
  44. * Marking Objects:: Commands to put region around textual units.
  45. * Using Region:: Summary of ways to operate on contents of the region.
  46. * Mark Ring:: Previous mark positions saved so you can go back there.
  47. * Global Mark Ring:: Previous mark positions in various buffers.
  48. * Shift Selection:: Using shifted cursor motion keys.
  49. * Disabled Transient Mark:: Leaving regions unhighlighted by default.
  50. @end menu
  51. @node Setting Mark
  52. @section Setting the Mark
  53. Here are some commands for setting the mark:
  54. @table @kbd
  55. @item C-@key{SPC}
  56. Set the mark at point, and activate it (@code{set-mark-command}).
  57. @item C-@@
  58. The same.
  59. @item C-x C-x
  60. Set the mark at point, and activate it; then move point where the mark
  61. used to be (@code{exchange-point-and-mark}).
  62. @item Drag-mouse-1
  63. Set point and the mark around the text you drag across.
  64. @item mouse-3
  65. Set the mark at point, then move point to where you click
  66. (@code{mouse-save-then-kill}).
  67. @item @samp{Shifted cursor motion keys}
  68. Set the mark at point if the mark is inactive, then move point.
  69. @xref{Shift Selection}.
  70. @end table
  71. @kindex C-SPC
  72. @kindex C-@@
  73. @findex set-mark-command
  74. The most common way to set the mark is with @kbd{C-@key{SPC}}
  75. (@code{set-mark-command})@footnote{There is no @kbd{C-@key{SPC}}
  76. character in @acronym{ASCII}; usually, typing @kbd{C-@key{SPC}} on a
  77. text terminal gives the character @kbd{C-@@}. This key is also bound
  78. to @code{set-mark-command}, so unless you are unlucky enough to have
  79. a text terminal that behaves differently, you might as well think of
  80. @kbd{C-@@} as @kbd{C-@key{SPC}}.}. This sets the mark where point is,
  81. and activates it. You can then move point away, leaving the mark
  82. behind.
  83. For example, suppose you wish to convert part of the buffer to upper
  84. case. To accomplish this, go to one end of the desired text, type
  85. @kbd{C-@key{SPC}}, and move point until the desired portion of text is
  86. highlighted. Now type @kbd{C-x C-u} (@code{upcase-region}). This
  87. converts the text in the region to upper case, and then deactivates
  88. the mark.
  89. Whenever the mark is active, you can deactivate it by typing
  90. @kbd{C-g} (@pxref{Quitting}). Most commands that operate on the
  91. region also automatically deactivate the mark, like @kbd{C-x C-u} in
  92. the above example.
  93. Instead of setting the mark in order to operate on a region, you can
  94. also use it to remember a position in the buffer (by typing
  95. @kbd{C-@key{SPC} C-@key{SPC}}), and later jump back there (by typing
  96. @kbd{C-u C-@key{SPC}}). @xref{Mark Ring}, for details.
  97. @kindex C-x C-x
  98. @findex exchange-point-and-mark
  99. The command @kbd{C-x C-x} (@code{exchange-point-and-mark}) exchanges
  100. the positions of point and the mark. @kbd{C-x C-x} is useful when you
  101. are satisfied with the position of point but want to move the other
  102. end of the region (where the mark is). Using @kbd{C-x C-x} a second
  103. time, if necessary, puts the mark at the new position with point back
  104. at its original position. Normally, if the mark is inactive, this
  105. command first reactivates the mark wherever it was last set, to ensure
  106. that the region is left highlighted. However, if you call it with a
  107. prefix argument, it leaves the mark inactive and the region
  108. unhighlighted; you can use this to jump to the mark in a manner
  109. similar to @kbd{C-u C-@key{SPC}}.
  110. You can also set the mark with the mouse. If you press the left
  111. mouse button (@kbd{down-mouse-1}) and drag the mouse across a range of
  112. text, this sets the mark where you first pressed the mouse button and
  113. puts point where you release it. Alternatively, clicking the right
  114. mouse button (@kbd{mouse-3}) sets the mark at point and then moves
  115. point to where you clicked. @xref{Mouse Commands}, for a more
  116. detailed description of these mouse commands.
  117. @cindex shift-selection
  118. Finally, you can set the mark by holding down the shift key while
  119. typing certain cursor motion commands (such as @kbd{S-@key{RIGHT}},
  120. @kbd{S-C-f}, @kbd{S-C-n}, etc.). This is called @dfn{shift-selection}.
  121. It sets the mark at point before moving point, but only if there is no
  122. active mark set via shift-selection. The mark set by mouse commands
  123. and by shift-selection behaves slightly differently from the usual
  124. mark: any subsequent unshifted cursor motion command deactivates it
  125. automatically. For details, @xref{Shift Selection}.
  126. Many commands that insert text, such as @kbd{C-y} (@code{yank}), set
  127. the mark at the other end of the inserted text, without activating it.
  128. This lets you easily return to that position (@pxref{Mark Ring}). You
  129. can tell that a command does this when it shows @samp{Mark set} in the
  130. echo area.
  131. @cindex primary selection
  132. Under X, every time the active region changes, Emacs saves the text
  133. in the region to the @dfn{primary selection}. This lets you insert
  134. that text into other X applications with @kbd{mouse-2} clicks.
  135. @xref{Primary Selection}.
  136. @node Marking Objects
  137. @section Commands to Mark Textual Objects
  138. @cindex marking sections of text
  139. Here are commands for placing point and the mark around a textual
  140. object such as a word, list, paragraph or page:
  141. @table @kbd
  142. @item M-@@
  143. Set mark after end of next word (@code{mark-word}). This does not
  144. move point.
  145. @item C-M-@@
  146. Set mark after end of following balanced expression
  147. (@code{mark-sexp}). This does not move point.
  148. @item M-h
  149. Move point to the beginning of the current paragraph, and set mark at
  150. the end (@code{mark-paragraph}).
  151. @item C-M-h
  152. Move point to the beginning of the current defun, and set mark at the
  153. end (@code{mark-defun}).
  154. @item C-x C-p
  155. Move point to the beginning of the current page, and set mark at the
  156. end (@code{mark-page}).
  157. @item C-x h
  158. Move point to the beginning of the buffer, and set mark at the end
  159. (@code{mark-whole-buffer}).
  160. @end table
  161. @kindex M-@@
  162. @findex mark-word
  163. @kbd{M-@@} (@code{mark-word}) sets the mark at the end of the next
  164. word (@pxref{Words}, for information about words). Repeated
  165. invocations of this command extend the region by advancing the mark
  166. one word at a time. As an exception, if the mark is active and
  167. located before point, @kbd{M-@@} moves the mark backwards from its
  168. current position one word at a time.
  169. This command also accepts a numeric argument @var{n}, which tells it
  170. to advance the mark by @var{n} words. A negative argument moves the
  171. mark back by @var{n} words.
  172. @kindex C-M-@@
  173. @findex mark-sexp
  174. Similarly, @kbd{C-M-@@} (@code{mark-sexp}) puts the mark at the end
  175. of the next balanced expression (@pxref{Expressions}). Repeated
  176. invocations extend the region to subsequent expressions, while
  177. positive or negative numeric arguments move the mark forward or
  178. backward by the specified number of expressions.
  179. The other commands in the above list set both point and mark, so as
  180. to delimit an object in the buffer. @kbd{M-h} (@code{mark-paragraph})
  181. marks paragraphs (@pxref{Paragraphs}), @kbd{C-M-h} (@code{mark-defun})
  182. marks top-level definitions (@pxref{Moving by Defuns}), and @kbd{C-x
  183. C-p} (@code{mark-page}) marks pages (@pxref{Pages}). Repeated
  184. invocations again play the same role, extending the region to
  185. consecutive objects; similarly, numeric arguments specify how many
  186. objects to move the mark by.
  187. @kindex C-x h
  188. @findex mark-whole-buffer
  189. @cindex select all
  190. @kbd{C-x h} (@code{mark-whole-buffer}) sets up the entire buffer as
  191. the region, by putting point at the beginning and the mark at the end.
  192. @node Using Region
  193. @section Operating on the Region
  194. @cindex operations on a marked region
  195. Once you have a region, here are some of the ways you can operate on
  196. it:
  197. @itemize @bullet
  198. @item
  199. Kill it with @kbd{C-w} (@pxref{Killing}).
  200. @item
  201. Copy it to the kill ring with @kbd{M-w} (@pxref{Yanking}).
  202. @item
  203. Convert case with @kbd{C-x C-l} or @kbd{C-x C-u} (@pxref{Case}).
  204. @item
  205. Undo changes within it using @kbd{C-u C-/} (@pxref{Undo}).
  206. @item
  207. Replace text within it using @kbd{M-%} (@pxref{Query Replace}).
  208. @item
  209. Indent it with @kbd{C-x @key{TAB}} or @kbd{C-M-\} (@pxref{Indentation}).
  210. @item
  211. Fill it as text with @kbd{M-x fill-region} (@pxref{Filling}).
  212. @item
  213. Check the spelling of words within it with @kbd{M-$} (@pxref{Spelling}).
  214. @item
  215. Evaluate it as Lisp code with @kbd{M-x eval-region} (@pxref{Lisp Eval}).
  216. @item
  217. Save it in a register with @kbd{C-x r s} (@pxref{Registers}).
  218. @item
  219. Save it in a buffer or a file (@pxref{Accumulating Text}).
  220. @end itemize
  221. Some commands have a default behavior when the mark is inactive, but
  222. operate on the region if the mark is active. For example, @kbd{M-$}
  223. (@code{ispell-word}) normally checks the spelling of the word at
  224. point, but it checks the text in the region if the mark is active
  225. (@pxref{Spelling}). Normally, such commands use their default
  226. behavior if the region is empty (i.e., if mark and point are at the
  227. same position). If you want them to operate on the empty region,
  228. change the variable @code{use-empty-active-region} to @code{t}.
  229. @vindex delete-active-region
  230. As described in @ref{Erasing}, the @key{DEL}
  231. (@code{backward-delete-char}) and @key{delete}
  232. (@code{delete-forward-char}) commands also act this way. If the mark
  233. is active, they delete the text in the region. (As an exception, if
  234. you supply a numeric argument @var{n}, where @var{n} is not one, these
  235. commands delete @var{n} characters regardless of whether the mark is
  236. active). If you change the variable @code{delete-active-region} to
  237. @code{nil}, then these commands don't act differently when the mark is
  238. active. If you change the value to @code{kill}, these commands
  239. @dfn{kill} the region instead of deleting it (@pxref{Killing}).
  240. @vindex mark-even-if-inactive
  241. Other commands always operate on the region, and have no default
  242. behavior. Such commands usually have the word @code{region} in their
  243. names, like @kbd{C-w} (@code{kill-region}) and @code{C-x C-u}
  244. (@code{upcase-region}). If the mark is inactive, they operate on the
  245. @dfn{inactive region}---that is, on the text between point and the
  246. position at which the mark was last set (@pxref{Mark Ring}). To
  247. disable this behavior, change the variable
  248. @code{mark-even-if-inactive} to @code{nil}. Then these commands will
  249. instead signal an error if the mark is inactive.
  250. @cindex Delete Selection mode
  251. @cindex mode, Delete Selection
  252. @findex delete-selection-mode
  253. By default, text insertion occurs normally even if the mark is
  254. active---for example, typing @kbd{a} inserts the character @samp{a},
  255. then deactivates the mark. Delete Selection mode, a minor mode,
  256. modifies this behavior: if you enable that mode, then inserting text
  257. while the mark is active causes the text in the region to be deleted
  258. first. Also, commands that normally delete just one character, such
  259. as @kbd{C-d} or @kbd{@key{DEL}}, will delete the entire region
  260. instead. To toggle Delete Selection mode on or off, type @kbd{M-x
  261. delete-selection-mode}.
  262. @node Mark Ring
  263. @section The Mark Ring
  264. @cindex mark ring
  265. Each buffer remembers previous locations of the mark, in the
  266. @dfn{mark ring}. Commands that set the mark also push the old mark
  267. onto this ring. One of the uses of the mark ring is to remember spots
  268. that you may want to go back to.
  269. @table @kbd
  270. @item C-@key{SPC} C-@key{SPC}
  271. Set the mark, pushing it onto the mark ring, without activating it.
  272. @item C-u C-@key{SPC}
  273. Move point to where the mark was, and restore the mark from the ring
  274. of former marks.
  275. @end table
  276. @kindex C-SPC C-SPC
  277. The command @kbd{C-@key{SPC} C-@key{SPC}} is handy when you want to
  278. use the mark to remember a position to which you may wish to return.
  279. It pushes the current point onto the mark ring, without activating the
  280. mark (which would cause Emacs to highlight the region). This is
  281. actually two consecutive invocations of @kbd{C-@key{SPC}}
  282. (@code{set-mark-command}); the first @kbd{C-@key{SPC}} sets the mark,
  283. and the second @kbd{C-@key{SPC}} deactivates it. (When Transient Mark
  284. mode is off, @kbd{C-@key{SPC} C-@key{SPC}} instead activates Transient
  285. Mark mode temporarily; @pxref{Disabled Transient Mark}.)
  286. @kindex C-u C-SPC
  287. To return to a marked position, use @code{set-mark-command} with a
  288. prefix argument: @kbd{C-u C-@key{SPC}}. This moves point to where the
  289. mark was, and deactivates the mark if it was active. Each subsequent
  290. @kbd{C-u C-@key{SPC}} jumps to a prior position stored in the mark
  291. ring. The positions you move through in this way are not lost; they
  292. go to the end of the ring.
  293. @vindex set-mark-command-repeat-pop
  294. If you set @code{set-mark-command-repeat-pop} to non-@code{nil},
  295. then immediately after you type @kbd{C-u C-@key{SPC}}, you can type
  296. @kbd{C-@key{SPC}} instead of @kbd{C-u C-@key{SPC}} to cycle through
  297. the mark ring. By default, @code{set-mark-command-repeat-pop} is
  298. @code{nil}.
  299. Each buffer has its own mark ring. All editing commands use the
  300. current buffer's mark ring. In particular, @kbd{C-u C-@key{SPC}}
  301. always stays in the same buffer.
  302. @vindex mark-ring-max
  303. The variable @code{mark-ring-max} specifies the maximum number of
  304. entries to keep in the mark ring. This defaults to 16 entries. If
  305. that many entries exist and another one is pushed, the earliest one in
  306. the list is discarded. Repeating @kbd{C-u C-@key{SPC}} cycles through
  307. the positions currently in the ring.
  308. If you want to move back to the same place over and over, the mark
  309. ring may not be convenient enough. If so, you can record the position
  310. in a register for later retrieval (@pxref{Position Registers,, Saving
  311. Positions in Registers}).
  312. @node Global Mark Ring
  313. @section The Global Mark Ring
  314. @cindex global mark ring
  315. @vindex global-mark-ring-max
  316. In addition to the ordinary mark ring that belongs to each buffer,
  317. Emacs has a single @dfn{global mark ring}. Each time you set a mark,
  318. this is recorded in the global mark ring in addition to the current
  319. buffer's own mark ring, if you have switched buffers since the
  320. previous mark setting. Hence, the global mark ring records a sequence
  321. of buffers that you have been in, and, for each buffer, a place where
  322. you set the mark. The length of the global mark ring is controlled by
  323. @code{global-mark-ring-max}, and is 16 by default.
  324. @kindex C-x C-@key{SPC}
  325. @findex pop-global-mark
  326. The command @kbd{C-x C-@key{SPC}} (@code{pop-global-mark}) jumps to
  327. the buffer and position of the latest entry in the global ring. It also
  328. rotates the ring, so that successive uses of @kbd{C-x C-@key{SPC}} take
  329. you to earlier buffers and mark positions.
  330. @node Shift Selection
  331. @section Shift Selection
  332. @cindex shift-selection
  333. If you hold down the shift key while typing a cursor motion command,
  334. this sets the mark before moving point, so that the region extends
  335. from the original position of point to its new position. This feature
  336. is referred to as @dfn{shift-selection}. It is similar to the way
  337. text is selected in other editors.
  338. The mark set via shift-selection behaves a little differently from
  339. what we have described above. Firstly, in addition to the usual ways
  340. of deactivating the mark (such as changing the buffer text or typing
  341. @kbd{C-g}), the mark is deactivated by any @emph{unshifted} cursor
  342. motion command. Secondly, any subsequent @emph{shifted} cursor motion
  343. command avoids setting the mark anew. Therefore, a series of shifted
  344. cursor motion commands will continuously adjust the region.
  345. Shift-selection only works if the shifted cursor motion key is not
  346. already bound to a separate command (@pxref{Customization}). For
  347. example, if you bind @kbd{S-C-f} to another command, typing
  348. @kbd{S-C-f} runs that command instead of performing a shift-selected
  349. version of @kbd{C-f} (@code{forward-char}).
  350. A mark set via mouse commands behaves the same as a mark set via
  351. shift-selection (@pxref{Setting Mark}). For example, if you specify a
  352. region by dragging the mouse, you can continue to extend the region
  353. using shifted cursor motion commands. In either case, any unshifted
  354. cursor motion command deactivates the mark.
  355. To turn off shift-selection, set @code{shift-select-mode} to
  356. @code{nil}. Doing so does not disable setting the mark via mouse
  357. commands.
  358. @node Disabled Transient Mark
  359. @section Disabling Transient Mark Mode
  360. @cindex mode, Transient Mark
  361. @cindex Transient Mark mode
  362. @cindex highlighting region
  363. @cindex region highlighting
  364. @cindex Zmacs mode
  365. @findex transient-mark-mode
  366. The default behavior of the mark and region, in which setting the
  367. mark activates it and highlights the region, is called Transient Mark
  368. mode. This is a minor mode that is enabled by default. It can be
  369. toggled with @kbd{M-x transient-mark-mode}, or with the @samp{Active
  370. Region Highlighting} menu item in the @samp{Options} menu. Turning it
  371. off switches Emacs to an alternative mode of operation:
  372. @itemize @bullet
  373. @item
  374. Setting the mark, with commands like @kbd{C-@key{SPC}} or @kbd{C-x
  375. C-x}, does not highlight the region. Therefore, you can't tell by
  376. looking where the mark is located; you have to remember.
  377. The usual solution to this problem is to set the mark and then use it
  378. soon, before you forget where it is. You can also check where the
  379. mark is by using @kbd{C-x C-x}, which exchanges the positions of the
  380. point and the mark (@pxref{Setting Mark}).
  381. @item
  382. Some commands, which ordinarily act on the region when the mark is
  383. active, no longer do so. For example, normally @kbd{M-%}
  384. (@code{query-replace}) performs replacements within the region, if the
  385. mark is active. When Transient Mark mode is off, it always operates
  386. from point to the end of the buffer. Commands that act this way are
  387. identified in their own documentation.
  388. @end itemize
  389. While Transient Mark mode is off, you can activate it temporarily
  390. using @kbd{C-@key{SPC} C-@key{SPC}} or @kbd{C-u C-x C-x}.
  391. @table @kbd
  392. @item C-@key{SPC} C-@key{SPC}
  393. @kindex C-@key{SPC} C-@key{SPC}
  394. Set the mark at point (like plain @kbd{C-@key{SPC}}) and enable
  395. Transient Mark mode just once, until the mark is deactivated. (This
  396. is not really a separate command; you are using the @kbd{C-@key{SPC}}
  397. command twice.)
  398. @item C-u C-x C-x
  399. @kindex C-u C-x C-x
  400. Exchange point and mark, activate the mark and enable Transient Mark
  401. mode temporarily, until the mark is next deactivated. (This is the
  402. @kbd{C-x C-x} command, @code{exchange-point-and-mark}, with a prefix
  403. argument.)
  404. @end table
  405. These commands set or activate the mark, and enable Transient Mark
  406. mode only until the mark is deactivated. One reason you may want to
  407. use them is that some commands operate on the entire buffer instead of
  408. the region when Transient Mark mode is off. Enabling Transient Mark
  409. mode momentarily gives you a way to use these commands on the region.
  410. When you specify a region with the mouse (@pxref{Setting Mark}), or
  411. with shift-selection (@pxref{Shift Selection}), this likewise
  412. activates Transient Mark mode temporarily and highlights the region.