Search:

Snippets

Eshell Switch

  • Elisp
(defun cyn/eshell-switch ()
  "Hotswap between the current and eshell/project eshell buffers.

If you are in a project directory, and *not* in `eshell-mode',
it will run `project-eshell'.

If you are *not* in a project directory, and *not* in `eshell-mode',
it will run `eshell'.

If you are in in `eshell-mode',
it will run `previous-buffer'."
  (interactive)
  (if (eq major-mode 'eshell-mode)
      (previous-buffer)
    (if (project-current)
        (project-eshell)
      (eshell))))

HTML Boilerplate

  • HTML
<!DOCTYPE html>
<html lang="en"> <!-- Replace "en" with the document language, if applicable -->
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="style.css"> <!-- Change href/remove entirely if applicable -->
        <title>Page Title</title> <!-- Replace "Page Title" with the title of your page, if applicable -->
    </head>
    <body>
        <!-- Body content goes here -->
    </body>
</html>

Insert Org Mode Code Block

  • Elisp
(defun cyn/org-insert-code-block ()
  "Insert a code block at point."
  (interactive)
  (let ((code-block-language (read-string "Enter code block language: ")))
    (insert (concat "#+begin_src " code-block-language))
    (newline)
    (insert "#+end_src")
    (forward-line -1)
    (end-of-line)
    (call-interactively 'org-return)))

Strum Example

  • Rust

An example of using the strum crate to format file extensions into their associated language names.

#[derive(Debug, strum_macros::Display, EnumString, EnumMessage)]
#[strum(serialize_all = "snake_case")]
enum Languages {
    #[strum(message = "Markdown")]
    Md,
    #[strum(message = "Org Mode")]
    Org,
    #[strum(message = "Rust")]
    Rs,
    #[strum(message = "Lua")]
    Lua,
    #[strum(message = "Haxe")]
    Hx,
    #[strum(message = "GD Script")]
    Gd,
}

fn print_language(path: &PathBuf) { if path.exists() && let Some(extension_os_str) = path.extension() && let Some(extension_str) = extension_os_str.to_str() && let Ok(lang) = Languages::from(extension_str) && let Some(lang_message) = lang.get_message() { println!("The language is: {lang_message}"); } }

Zig Stdout Print

  • Zig

This shows how to print using stdout in Zig- you can print in a simpler way via the std.debug.print() function, however that prints to stderr- this snippet is for printing to stdout.

var buffer: [1024]u8 = undefined; // Create buffer that stdout will use
var writer = std.fs.File.stdout().writer(&buffer); // Create a writer and assign it to the buffer
const stdout = &writer.interface(); // Assign the writer interface as a variable

// "Print" the string "Hello from from Zig!" to the writer // Note that this will not output it to the terminal on it's own try stdout.print("Hello from Zig!", .{}); try stdout.flush(); // Flush (output) the writer content to the terminal