I have been using emacs for years, but am by no means an expert. When I got my new verion of Ubuntu I got the latest version of emacs (24), but hated it, so dropped back to emacs23. It is like what I'm used to except that the find and replace function does not work as I want it to. I'm used to being able to be anywhere in a document, hit M-x %, then type my search and replace strings, hit enter, and replace throughout the rest of the document. It does not do that anymore. If I don't select a region, it will not even attempt to find any instances. If I have selected a region, and it is entirely visible in my window, it will do the search and replace. If I have highlighted a region that is larger than my window, it will only search and replace in the visible part of the region. This is maddening.

I think it has something to do with 'transient-mark-mode', which is on by default in emacs23 apparently, and people describe the behavior I'm seeing. But when I turn it off with M-x transient-mark-mode, or in my .emacs file, nothing changes. What am I doing wrong?


I can't reproduce your issue, but it might be that you have cua-mode enabled, and it prevents from turning off transient-mark-mode. Try to toggle M-x cua-mode until you'd get disabled, and after this toggle M-x transient-mark-mode until you get it disabled. Then check if this solves you issue.


As a side note, it sounds strange to me, that you like Emacs23 (since you were using it for years) and hate Emacs24. What went wrong when you tried Emacs24?


Here's query-replace-regexp-to-the-end-of-buffer-or-in-a-region defun which does what you want:

(defun query-replace-regexp-to-the-end-of-buffer-or-in-a-region (point)
  "If there's a region - query replaces regexp in region, 
   otherwise replaces from current point to the end of buffer."
  (interactive "d")
  (let (start end)
    (if (use-region-p)
        (progn (setq start (region-beginning)) ;; then
               (setq end (region-end)))
      (progn (setq start point) ;; else
             (setq end (point-max))))
    (set-mark start)
    (goto-char end)
    (apply #'query-replace-regexp 
           (let ((common (query-replace-read-args (concat "Query replace regexp") t))) 
             (list (nth 0 common) (nth 1 common) (nth 2 common) (if (and transient-mark-mode mark-active) (region-beginning)) (if (and transient-mark-mode mark-active) (region-end)))))))

Just bind it to some key, maybe even to M-%.

  • There's a lengthy comment thread on the cross-post at Emacs: emacs.stackexchange.com/questions/3965/… I can't make sense of it as a Vim guy, perhaps you could? – muru Dec 10 '14 at 11:18
  • Thanks for the help! As @muru has noted, I also asked this question over at the emacs board. I hadn't realized that M-M M-% was different than M-%, as in previous versions of emacs they did the same thing (i.e. replace to end of buffer), but for whatever reason they did different things this time around. But, once I knew that, I could just alter the binding to make it do what I wanted. – soulish Dec 12 '14 at 6:21
  • As for not liking emacs24, you should note that I am the type of person who hates new versions of everything. Unless I have a specific need for new features, I would much rather have everything stay the same forever, so I don't have to learn new commands. In my perfect world there would only be one car type available and it would be updated every 5 years. So don't take too much offense at me not liking new versions of emacs. – soulish Dec 12 '14 at 6:23
  • @soulish: ok, accepted. So does the defun I posted works for you? – Adobe Dec 12 '14 at 7:54
  • @Adobe, I hadn't tried it out yesterday, but yes it does do the trick. It replaces in a region if there is one, and in the rest of the buffer if there isn't a region. Thanks. – soulish Dec 12 '14 at 20:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.