Skip to content

Instantly share code, notes, and snippets.

@xuchunyang
Created December 28, 2014 08:07
Show Gist options
  • Save xuchunyang/1773c7366152965d03c0 to your computer and use it in GitHub Desktop.
Save xuchunyang/1773c7366152965d03c0 to your computer and use it in GitHub Desktop.
;; Narrowing -- use save-restriction
(defun my-what-line ()
"Print the current line number (in the buffer) of point."
(interactive)
(save-restriction
(widen)
(save-excursion
(beginning-of-line)
(message "Line %d"
(1+ (count-lines 1 (point)))))))
(defun replace-regexp-in-region (start end)
(interactive "*r")
(save-excursion
(save-restriction
(let ((regexp (read-string "Regexp: "))
(to-string (read-string "Replacement: ")))
(narrow-to-region start end)
(goto-char (point-min))
(while (re-search-forward regexp nil t)
(replace-match to-string nil nil))))))
(defun display-first-60-chars-of-current-buffer ()
(interactive)
(save-restriction
(widen)
(message "%s" (buffer-substring-no-properties 1 60))))
(display-first-60-chars-of-current-buffer)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment