Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs specific region read only

Tags:

emacs

How does one make a specific region in a text file read only when using emacs. I know ctrl+X+Q to make the whole file a read only.

I am currently writing a code and I do not want to modify by accident the first 40 lines of my code while working on lines 41 and upwards.

like image 894
smilingbuddha Avatar asked Sep 14 '11 00:09

smilingbuddha


2 Answers

Use text properties:

(defun set-region-read-only (begin end)
  (interactive "r")
  (add-text-properties begin end '(read-only t)))

Relevant Docs:

Text-Properties

Changing Properties

Special Properties (like read-only)

like image 86
Michael Markert Avatar answered Oct 30 '22 23:10

Michael Markert


You can use narrow-to-region (C-x n n) to narrow the buffer just to the part you want to change. Then you won't see or be able to change the region you don't want to change.

like image 40
Ross Patterson Avatar answered Oct 30 '22 23:10

Ross Patterson