0

Trying to trim off some of the path in the filename in mode-line. I now get /share/Projects/<project_name>/... trying to trim off the prefix /share/Projects

In my .emacs file:

(setq-default mode-line-format `(
 ,(propertize " Mode: " 'face 'bold)
 ,(propertize "%m " 'face '(:foreground "#fff"))
 ,(propertize " File: " 'face 'bold)
 ,(propertize (s-replace "/share/Projects" "" "%f") 'face '(:underline "red" :foreground "#fff" :height 1))
 ))

Not sure why it's not working, it still prints the full path, if I manually set the "%f" to "/share/Projects/<project_name>" it trims the string down as expected.

  • What is s-replace? What is f (where is it defined)? Have you tried just setting variable mode-line-buffer-identification? – Drew Nov 7 '18 at 18:44
  • Here is a link to s-replace docs link. f is the "buffer-file-name" link. The mode-line-buffer-id (%b), if thats what you meant, only shows the buffer name, not the filename/filepath. – ladrua Nov 7 '18 at 19:01
  • Have you tried replace-regexp-in-string instead of s-replace? – nega Nov 7 '18 at 19:44
  • Yes, same result, I'm starting to think maybe this aproach wont work. I guess maybe the actual variables are not populated at that point? – ladrua Nov 7 '18 at 19:59
  • %f is the file name, not f. – Drew Nov 7 '18 at 20:56
0

Replacing "/share/Projects" with "" in the string "%f" (a string which clearly does not contain "/share/Projects" at all) will result in the exact same unaltered string "%f" -- which will subsequently be processed as a mode line construct.

You will surely see this if you look at the value of mode-line-format after setting it.

You can try this construct instead:

(setq-default mode-line-format `(
  ...
  (:propertize (:eval (s-replace "/share/Projects" "" buffer-file-name))
               face (:underline "red" :foreground "#fff" :height 1))
  ...))
  • Thank you for your suggestion, however your example sets all model-lines to the .emacs file's buffer-file-name, so they now all say ~/.emacs – ladrua Nov 7 '18 at 22:01
  • Also worth noteing that (s-replace "/share/Projects" "" f), so using f instead of "%f", also prints the full path. – ladrua Nov 7 '18 at 22:03
  • @ladrua Don't unquote any part of the code I suggested. Your original code used ,(propertize ...) -- don't do that here! – phils Nov 7 '18 at 22:49
  • You are absolutely right, works like a charm! One thing, I had to change 'face to just face for the styling to work. Thank you very much! – ladrua Nov 8 '18 at 4:05
  • Oops, very true; a bad copy/paste on my part, sorry. Glad you got it working. The list value of the face property shouldn't be quoted either, but it looks like that still works regardless. I've updated the answer accordingly. – phils Nov 11 '18 at 0:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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