8

Given 2 absolute numbers (compatible with region-beginning, region-end).

How can these numbers be used to set the selection?


Edit: If you use evil mode you may want to check this question, at the time of asking I wasn't aware this required a different method in some cases.

ideasman42
  • 8,786
  • 1
  • 32
  • 114

1 Answers1

9

The region is defined as the span of text between point and mark, therefore it's sufficient to adjust both to the numbers in question. The following uses point-min and point-max for that purpose:

(set-mark (point-min))
(goto-char (point-max))

However you mention "set the selection" afterwards, so perhaps you don't only want to change the region (as there is always a region), but activate it as well:

(activate-mark)
wasamasa
  • 22,178
  • 1
  • 66
  • 99
  • 1
    Maybe worth mentioning that (setq deactivate-mark nil) may need to be set in some cases (as you found when looking into why setting selection sometimes fails). – ideasman42 Aug 15 '17 at 02:03
  • Not really as activating the mark works, it's just that it's deactivated right afterwards. This is therefore material for a new question of the "Why is mark deactivated after a <...> command?" kind. – wasamasa Aug 15 '17 at 05:18