I have this function:
;; needs work in case of spaces in filenames (cl-defun filename-from-buffer (&key get-ext get-abs) "Returns the absolute name, with or without extension, of the file that is open in the current buffer." (let ((filename (buffer-name)) (absname (buffer-file-name))) (cond ((and get-ext get-abs) absname ) (get-abs (file-name-sans-extension absname )) (get-ext filename) (t (file-name-sans-extension filename)))))
You can test it with something like this,
(global-set-key (kbd "C-l t") (lambda () (interactive) (insert (filename-from-buffer :get-ext t :get-abs t)) ;; /Nuskha/With Space/file.ext (insert "\n") (insert (filename-from-buffer)) ;; file (insert "\n") (insert (filename-from-buffer :get-ext t)) ;; file.ext (insert "\n") (insert (filename-from-buffer :get-abs t)))) ;; /Nuskha/With Space/file
It works fine, but when there are spaces in the name, as shown in the comments above, the names are not such that you could take them verbatim and use with the
$ cd /Nuskha/With Space/file bash: cd: too many arguments
I want a function that returns something like this
/Nuskha/With\ Space/file. How could this be done?