Emacs Tweaks

The last few months I really been heavy into software development and, honestly, that is my happy place. I started to get into the habit of writing down my nits or annoyances of my workflow or my Emacs setup to try to streamline things. When I get a chance, I go through the list and start figuring out how to tune things. The following is a list of what things I have figured out in the last few months. Most of them are small, but the small things add up.

dotenv creds

I'm on a project using AWS and my AWS creds refresh twice a day. Luckily we are using dotenv so I just need to paste the values into a .env file in my project. But AWS gives me export statements while dotenv just wants VAR=value . And I have to do this twice a day! So a little Emacs and Hyperbole to the rescue.

I worked with my favorite AI and we came up with the following ELisp:

(defun my-clean-env ()
  "Clean environment variables and format text below the button."
  (interactive)
  (let ((kill-content (current-kill 0)))  ; Get content first
    (save-excursion
      ;; Move to end of current line (after the button)
      (end-of-line)
      (insert "\n")
      (let ((start-point (point)))
        ;; Delete everything from start-point to end of buffer
        (delete-region start-point (point-max))
        ;; Insert and clean the new content
        (insert kill-content)
        (let ((end-point (point)))
          ;; Remove lines starting with AWS_
          (goto-char start-point)
          (while (re-search-forward "^AWS_.*\n" end-point t)
            (replace-match ""))
          ;; Replace all "export " instances
          (goto-char start-point)
          (while (search-forward "export " end-point t)
            (replace-match "")))))))

;; Create the Hyperbole button
(defact clean-env ()
  "Hyperbole action to clean environment variables."
  (my-clean-env))

Then make a Global Button to run the Lisp code when activated. I called this button clean-env. The bottom part of my .env file looks like:

## other stuff
# <(clean-env)>
AWS_TOKEN=<old>
AWS_SECRET=<old>
AWS_SESSION=<old>

When I need to refresh my creds:

  1. goto the AWS console, copy them into the system clipboard (which is a button on the AWS interface)
  2. Open the .env file in my project
  3. Go to the clean-env in the comment (which is a Hyperbole button) and hit M-enter.
  4. The old values are replaced by the new values (without the export charaters).
  5. Save the file

This seems simple but has saved me a lot of brain cycles.

Using eshell to run tests

I am on Windows for my current project and mostly because I use native Python in Msys2 Emacs, I can't run my Python test with project-run-test. This makes it hard to go exactly to the failed tests. But I remembered that I can use eshell, which I generally forget about. Then I can run the pytests within eshell and then use Hypberbole's action key to go to the failed test. Easy!

Navigating between variables, function call, etc.

This doesn't have anything to do with Hyperbole, funny enough. I stumbled onto someone talking about highlight-symbol, which does what it states -- will highlight all instances of a symbol that you are one. But it also has highlight-symbol-nav-mode, which let's you use M-p and M-n to goto the next instance of that symbol in a file. This is the kind of thing I've been looking for but didn't know how to describe it. Before used helm-occur for this type of navigation but that was very quickly replaced.

Editing the results of helm-occur

Speaking of helm-occur, I still use it and generally for tweaking a value or a pattern in my file. You can edit the search results directly, but I don't do it every day so I tend to forget the step. I finally wrote them down:

  1. Find candidates with helm-occur (it's C-c h o in my setup)
  2. Save the buffer with C-x C-s
  3. C-c C-p to change to wgrep mode
  4. Now it's a normal buffer! So I can make my changes
  5. C-x C-s to save the changes
  6. q to quit
  7. You may want to kill the *occur* buffer or Emacs will keep it around and ask you about it later

It's amazing what kind of little things can really add up to make you more productive.