1

I have a file with a .md extension, and I want to run a command to interpret/parse a file when markdown-mode is active with a C-c C-c key combination, for example. I want to run this shell command:

aglio -i foo.md -o foo.html

Wherefoo.md is the file name of the current buffer and foo.html is this name with replaced extension (to html). How can I do this in Emacs?

  • C-c C-c make different things into different major modes, in org-mode it have very much location-specific commands. But you could bind this on any key you want. But into markdown-mode the C-c C-c is the prefix key, so you'd better don't rebind it. – Konstantin Morenko Aug 30 '16 at 17:31
1

Running shell command could be done with (shell-command COMMAND). Then we need to make string for executing command. We'll concatenate it from sections with (concat ...). We have 5 sections of the command:

(concat "aglio -i "                                    ;; 1
        (buffer-file-name)                             ;; 2
        " -o "                                         ;; 3
        (file-name-sans-extension (buffer-file-name))  ;; 4
        ".html")                                       ;; 5

So, if the buffer file name is foo.md, then result of this script will be

aglio -i foo.md -o foo.html

We could make it function, like next:

(defun aglio-current-file ()
  "Execute aglio shell command on current file"
  (interactive)
  (shell-command
    (concat "aglio -i " (buffer-file-name)
     " -o " (file-name-sans-extension (buffer-file-name)) ".html")))

We could define key for markdown-mode-map with next code

(eval-after-load 'markdown-mode
  '(define-key markdown-mode-map (kbd "C-c C-c s")
    'aglio-current-file))

I use C-c C-c s instead of C-c C-c cause this version of the markdown package from melpa uses C-c C-c as prefix for buffer-wide commands.

Your Answer

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