Skip to content

Instantly share code, notes, and snippets.

View jamescherti's full-sized avatar

James Cherti jamescherti

View GitHub Profile
@jamescherti
jamescherti / add-hyphen-bash-imenu.el
Last active November 23, 2024 21:33
Emacs Bash scripts imenu: Support hyphens in function names
;;; add-hyphen-bash-imenu.el --- Bash scripts imenu: Support hyphens in function names -*- lexical-binding: t; -*-
;; Gits URL: https://gist.github.com/jamescherti/3dcfe5b67b69a331b09c3db66e82d5fc
;; License: MIT
;; Author: James Cherti
(with-eval-after-load "sh-script"
;; By default, imenu does not include functions with hyphens in their names by
;; default. While it is true that sh does not allow hyphens in function names,
;; Bash does permit them. The following code snippet adds a hyphen '-' to the imenu
;; Bash script regular expression.
@jamescherti
jamescherti / clone-indirect-buffer-current-window.el
Last active November 13, 2024 03:51
Emacs: Create an indirect buffer and switch to it
;;; clone-indirect-buffer-current-window.el --- Create an indirect buffer and switch to it.. -*- lexical-binding: t; -*-
;; Gits URL: https://gist.github.com/jamescherti/686d8cb3d636614cefe76c6fc7b76e55
;; License: MIT
;; Author: James Cherti
(defun my-clone-indirect-buffer-current-window (&optional newname norecord)
"Create an indirect buffer and switch to it.
Creates a new indirect buffer with the same content as the current buffer,
preserving point position, window start, and horizontal scroll position. The
original buffer remains unchanged.
@jamescherti
jamescherti / disable-truncation-arrow.el
Last active November 5, 2024 14:56
Emacs: Disable the truncation arrow.
;;; disable-truncation-arrow.el --- Disable the truncation arrow. -*- lexical-binding: t; -*-
;; Gits URL: https://gist.github.com/jamescherti/45c7f70d316c5d707910cbd1fdb676cb
;; License: MIT
;; Author: James Cherti
(defun my-disable-fringe-truncation-arrow ()
"Disable the truncation arrow."
(unless (boundp 'fringe-indicator-alist)
(error "The fringe-indicator-alist was not declared"))
(setq fringe-indicator-alist
@jamescherti
jamescherti / delete-before-cursor-except-asterisks.el
Last active October 25, 2024 00:17
Emacs Evil Org-mode: Delete everything before the cursor except the first '*' and space.
;;; delete-before-cursor-except-asterisks.el --- Emacs Evil Org-mode: Delete everything before the cursor except the first '*' and space. -*- lexical-binding: t; -*-
;; Gits URL: https://gist.github.com/jamescherti/15168bb060d79a5c57215e62dd9bfa55
;; License: MIT
;; Author: James Cherti
(with-eval-after-load "org"
(with-eval-after-load "evil"
(defun my-evil-delete-to-heading-star ()
"Delete everything before the cursor except the first '*' and space.
@jamescherti
jamescherti / save-marker-and-window.el
Created October 23, 2024 16:59
Emacs: Save point as a marker, window start/hscroll, restoring them after BODY.
;;; save-marker-and-window.el --- Emacs: Save point as a marker, window start/hscroll, restoring them after BODY. -*- lexical-binding: t; -*-
;; Gits URL: https://gist.github.com/jamescherti/8921b341b628599ca8c7ff7f173a435a
;; Usage: (my-save-marker-and-window (progn (your-code argument)))
;; License: MIT
;; Author: James Cherti
(defmacro my-save-marker-and-window (&rest body)
"Save point as a marker, window start/hscroll, restoring them after BODY."
`(let ((point-marker (copy-marker (point)))
(window-start-marker (copy-marker (window-start)))
@jamescherti
jamescherti / shell-command-first-line-to-string.el
Last active October 23, 2024 16:57
Emacs Lisp: Return the first line of output from executing COMMAND.
;;; shell-command-first-line-to-string.el --- Return the first line of output from executing COMMAND. -*- lexical-binding: t; -*-
;; Return the first line of output from executing COMMAND.
;; If the command fails or produces no output, return nil.
;;
;; This function is superior to `shell-command-to-string` because it allows
;; for the detection of command execution failures by examining the exit status.
;; While `shell-command-to-string` only returns the output as a string,
;; it does not provide information about whether the command succeeded or failed.
;; By using `call-process-shell-command`, this function can capture both
;; the output and the exit status, enabling better error handling
@jamescherti
jamescherti / describe-elisp-symbol-at-point.el
Last active September 17, 2024 12:32
;;; my-describe-symbol-at-point.el --- Describe variable or function at point -*- lexical-binding: t; -*-
;;; describe-symbol-at-point.el --- Describe variable or function at point -*- lexical-binding: t; -*-
;; Description: Clear the contents of the *Messages* buffer if it is the current buffer.
;; Gits URL: https://gist.github.com/jamescherti/abb288cb98f5f9f08431cc29225273fe
;; License: MIT
;; Author: James Cherti
(defun my-describe-elisp-symbol-at-point ()
"Describe the symbol at point as either a variable or a function.
This function determines whether the symbol at point is a variable or a
@jamescherti
jamescherti / clear-messages-buffer.el
Last active August 23, 2024 22:08
Emacs: Clear the contents of the *Messages* buffer if it is the current buffer
;; Description: Clear the contents of the *Messages* buffer if it is the current buffer.
;; Gits URL: https://gist.github.com/jamescherti/52e8fe8700ab394163bb99f2b8d456fd
;; License: MIT
;; Author: James Cherti
(defun my-clear-messages-buffer ()
"Clear the contents of the *Messages* buffer if it is the current buffer."
(when (string= (buffer-name) "*Messages*")
(let ((was-read-only buffer-read-only))
(when was-read-only
@jamescherti
jamescherti / comment-indent-newline.el
Last active August 18, 2024 16:50
Emacs: Enable multi-line commenting and indenting
;; Description: Enable multi-line commenting and indenting in Emacs
;; Gits URL: https://gist.github.com/jamescherti/b8987b3299c0b567f1fdfffc1878a686
;; License: MIT
;; Author: James Cherti
;; Enable multi-line commenting. This allows comments to span multiple lines,
;; which is useful for writing longer comments or docstrings.
(setq comment-multi-line t)
;; Bind the `RET` (Return) key to `comment-indent-new-line`.
@jamescherti
jamescherti / evil-comment-uncomment.el
Created August 13, 2024 13:09
Emacs Evil: Comment or uncomment line or visual selection
;; Description: Emacs Evil: Comment or uncomment line or visual selection
;; Gits URL: https://gist.github.com/jamescherti/2aa95dc674ba024114d25824ddef8d7b
;; License: MIT
(with-eval-after-load "evil"
(evil-define-operator my-evil-comment-or-uncomment (beg end)
"Toggle comment for the region between BEG and END."
(interactive "<r>")
(comment-or-uncomment-region beg end))
(evil-define-key 'normal 'global (kbd "gc") 'my-evil-comment-or-uncomment))