Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8113129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:43:45+00:00 2026-06-06T02:43:45+00:00

Is it possible to perform a replace-string operation inside of a rectangular region in

  • 0

Is it possible to perform a replace-string operation inside of a rectangular region in emacs? If so, how?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T02:43:47+00:00Added an answer on June 6, 2026 at 2:43 am

    If you enable CUA selection mode:

    M-x cua-selection-mode RET

    or permanently in your init file:

    (cua-selection-mode 1)
    

    You can then use its advanced rectangle editing facilities.

    (or if you’re a cua-mode user, then you don’t need to do that.)

    • C-RET to mark a corner.
    • Move point to opposite corner.
    • M-r to do a regexp replace within the marked rectangle.
    • C-RET to unmark/exit rectangle editing.

    For documentation, look for the “CUA rectangle support” heading in the commentary in M-x find-library RET cua-base RET

    If you don’t want to use the cua rectangle facilities for some reason (perhaps if you really need replace-string specifically), a custom function using apply-on-rectangle would be pretty simply to put together.

    Edit: Actually, slightly more complex than I had expected, but most of the code is the interactive spec and the support for the ‘delimited’ prefix argument behaviour (both based on replace-string).

    Edit 2: I decided this was worth doing in a more fully-featured manner:

    The following provides C-xrM-% and C-xrC-M-%, which (hopefully) act the way you would expect.

    (require 'rect)
    
    (defun my-search-replace-in-rectangle
      (start end search-pattern replacement search-function literal)
      "Replace all instances of SEARCH-PATTERN (as found by SEARCH-FUNCTION)
    with REPLACEMENT, in each line of the rectangle established by the START
    and END buffer positions.
    
    SEARCH-FUNCTION should take the same BOUND and NOERROR arguments as
    `search-forward' and `re-search-forward'.
    
    The LITERAL argument is passed to `replace-match' during replacement.
    
    If `case-replace' is nil, do not alter case of replacement text."
      (apply-on-rectangle
       (lambda (start-col end-col search-function search-pattern replacement)
         (move-to-column start-col)
         (let ((bound (min (+ (point) (- end-col start-col))
                           (line-end-position)))
               (fixedcase (not case-replace)))
           (while (funcall search-function search-pattern bound t)
             (replace-match replacement fixedcase literal))))
       start end search-function search-pattern replacement))
    
    (defun my-replace-regexp-rectangle-read-args (regexp-flag)
      "Interactively read arguments for `my-replace-regexp-rectangle'
    or `my-replace-string-rectangle' (depending upon REGEXP-FLAG)."
      (let ((args (query-replace-read-args
                   (concat "Replace"
                           (if current-prefix-arg " word" "")
                           (if regexp-flag " regexp" " string"))
                   regexp-flag)))
        (list (region-beginning) (region-end)
              (nth 0 args) (nth 1 args) (nth 2 args))))
    
    (defun my-replace-regexp-rectangle
      (start end regexp to-string &optional delimited)
      "Perform a regexp search and replace on each line of a rectangle
    established by START and END (interactively, the marked region),
    similar to `replace-regexp'.
    
    Optional arg DELIMITED (prefix arg if interactive), if non-nil, means
    replace only matches surrounded by word boundaries.
    
    If `case-replace' is nil, do not alter case of replacement text."
      (interactive (my-replace-regexp-rectangle-read-args t))
      (when delimited
        (setq regexp (concat "\\b" regexp "\\b")))
      (my-search-replace-in-rectangle
       start end regexp to-string 're-search-forward nil))
    
    (defun my-replace-string-rectangle
      (start end from-string to-string &optional delimited)
      "Perform a string search and replace on each line of a rectangle
    established by START and END (interactively, the marked region),
    similar to `replace-string'.
    
    Optional arg DELIMITED (prefix arg if interactive), if non-nil, means
    replace only matches surrounded by word boundaries.
    
    If `case-replace' is nil, do not alter case of replacement text."
      (interactive (my-replace-regexp-rectangle-read-args nil))
      (let ((search-function 'search-forward))
        (when delimited
          (setq search-function 're-search-forward
                from-string (concat "\\b" (regexp-quote from-string) "\\b")))
        (my-search-replace-in-rectangle
         start end from-string to-string search-function t)))
    
    (global-set-key (kbd "C-x r M-%") 'my-replace-string-rectangle)
    (global-set-key (kbd "C-x r C-M-%") 'my-replace-regexp-rectangle)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to perform a MySQL search and replace while honoring wildcards? eg:
Is it possible to perform a partial or patch commit in Team Foundation? I
Is it possible to perform query expansion with Lucene 3.6 using any ontology? I've
is it possible to perform a first commit on a new repository using a
Is it possible to perform a global reversed-find on NHibernate-managed objects? Specifically, I have
Is it possible to perform some custom processing when Windsor instantiates a type? Something
Is it possible to perform an action when a UITableViewCell is highlighted? For example
Is it possible to perform an active wifi scan on android without using the,
Is it possible to perform the memory management by yourself. e.g. We allocate a
is it possible to perform a dynamic role assignment based on a quiz result

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.