So the problem is pretty straight forward I'm just trying to get delete trailing spaces from all files in a projectile project.

Here's what I've tried so far:

(defun projectile-delete-trailing-white-spaces-from-project-files ()
  "Deletes trailing spaces from all projectile project files."
  (let* ((list (projectile-current-project-files))
         (buffer (car list)))
    (while buffer
      (message buffer)
        (when (file-exists-p buffer)
          (set-buffer (find-file-noselect buffer))
          (delete-trailing-whitespace) ;; <-- This isn't working?
          (save-buffer))               ;; <-- This isn't working?
        (setq list (cdr list))
        (setq buffer (car list))

2 Answers 2


Adjusting Jack's answer slightly:

(defun projectile-whitespace-cleanup-project-files ()
  "Run `whitespace-cleanup' on all project files."
  (dolist (file (projectile-current-project-files))
    (let ((path (concat (projectile-project-root) file)
          (buffer (find-file-noselect path)))
      (when buffer
        (with-current-buffer buffer
  • @ksrb I did not want to simply reject your proposed edit, but there was no option to add a comment. :( Anyway, I don't even have a projectile-switch-to-project command, so I wonder if you need to upgrade the package, maybe there's a bug that's causing the behavior you described. I don't understand why this code would save files in a different directory; as far as I know, find-file-noselect should open the file, and save-buffer should save the buffer to the file it's backed by. Either way, I think using cd is probably not a good idea; better to work with the file's full path.
    – user3871
    Oct 15, 2016 at 13:02
  • It was saving in the wrong directory for me when I opened a file in a subdirectory using <kbd>C-p p</kbd>.
    – ksrb
    Oct 20, 2016 at 20:57
  • Hmm no way to edit comments -_-. Oh and code(projectile-switch-to-project)code was a typo on my part I meant code(projectile-switch-project)`.
    – ksrb
    Oct 20, 2016 at 21:04
  • @ksrb Okay, I tracked down the problem to projectile-current-project-files returning filenames relative to the current directory. I edited this answer to add the project root directory, which fixes the problem. Also it kills buffers now, so it doesn't leave open ones behind.
    – user3871
    Oct 21, 2016 at 1:27

This works for me:

(defun projectile-delete-trailing-white-spaces-from-project-files ()
  "Deletes trailing spaces from all projectile project files."
  (let ((project-files (projectile-current-project-files)))
    (dolist (pr project-files)
      (when (file-exists-p pr)
        (message "clearing trailing whitespace in %s" pr)
          (insert-file-contents pr)
          (delete-trailing-whitespace (point-min) (point-max))
          (write-file pr))))))

Although not certain it's the best way to do it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.