Skip to content

Instantly share code, notes, and snippets.

@ClashTheBunny
Last active September 27, 2019 22:04
Show Gist options
  • Save ClashTheBunny/cd79a700f75e43e95157f51691959cf3 to your computer and use it in GitHub Desktop.
Save ClashTheBunny/cd79a700f75e43e95157f51691959cf3 to your computer and use it in GitHub Desktop.
Edit YAML in VIM by converting to EDN
augroup YAML
au!
au BufReadPost *.yaml %!yaml2edn
au BufReadPost *.yaml set ft=clojure
au BufWritePre *.yaml %!edn2yaml
au BufWritePost *.yaml %!yaml2edn
au BufWritePost *.yaml set nomod
augroup END

So you want to edit yaml in a structured way?

  • First, install lumo and js-yaml globally and set your NODE_PATH:

    yarn global add lumo-cljs js-yaml
    export NODE_PATH=~/.config/yarn/global/node_modules
    
  • Put yaml2edn and edn2yaml in your path.

    chmod a+x edn2yaml yaml2edn
    sudo cp yaml2edn edn2yaml /usr/local/bin/
    
  • Append .vimrc_local to your .vimrc_local if you have one, or copy it to your homedir:

    [[ -f ~/.vimrc_local ]] && cat .vimrc_local >> ~/.vimrc_local || cp .vimrc_local ~/
    

vim bla.yaml and experience editing data in a structural way. No t-square required.

#!/usr/bin/env lumo
(ns edn2yaml.core
(:require
[cljs.pprint :refer [pprint]]
[cljs.reader :refer [read-string]]))
(def yaml (js/require "js-yaml"))
(js/require "process")
(.setEncoding js/process.stdin "utf8")
(.on js/process.stdin "data"
(fn [data]
(println (.safeDump yaml (clj->js (read-string data))))))
MIT License
Copyright (c) 2019 Randall Mason
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
#!/usr/bin/env lumo
(ns yaml2edn.core
(:require
[cljs.pprint :refer [pprint]]))
(def yaml (js/require "js-yaml"))
(js/require "process")
(.setEncoding js/process.stdin "utf8")
(.on js/process.stdin "data"
(fn [data]
(pprint (js->clj (.safeLoad yaml data)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment