This content originally appeared on Level Up Coding - Medium and was authored by Grégoire Hornung
Or how I save hours every month by just tweaking a tiny bit my .vimrc
In just a few years, dbt became a major player in the data space. With the promise of bringing software engineering good practices to the sql world, dbt lowered the barrier to entry to data engineering and made it much easier to write sound data pipelines for anyone mastering sql.
While I am a proponent of the modern data stack, I am still pretty old school when it comes to working with the shell. I was therefore looking for a way to integrate dbt with vim, my favorite editor.
After analyzing my workflow, I realized that 90% of the dbt cli commands that I used boil down to three steps
- compile the current model to check that the jinja macros, references, etc.. are correctly interpreted
- run the model
- test the model
I added two simple functions to my .vimrc. It allows me to execute the steps above in my fav editor without having to leave the keyboard.
function! OpenDbtCompiled()
call ExecuteDbtCommand("compile")
let model_path = bufname()
let project_name = substitute(split(getcwd(), "/")[-1], "-", "_", "")
let compiled_model_path = "target/compiled/" . project_name . "/" . model_path
execute 'edit' compiled_model_path
endfunction
function! ExecuteDbtCommand(command)
:w
let buf = bufname()
let model = split(split(buf, "/")[-1], '\.')[0]
let dbt_command = "dbt " . a:command . " -s " . model
VimuxRunCommand(dbt_command)
endfunction
Put simply, the second function parses the current buffer name, builds the corresponding dbt command, and sends it to the terminal by leveraging a plugin called vimux.
With a couple of mappings, I can call the functions with just a few keystrokes.
nnoremap ,do :call OpenDbtCompile()<CR>
nnoremap ,dr :call ExecuteDbtCommand("run")<CR>
nnoremap ,dt :call ExecuteDbtCommand("test")<CR>
Without further ados, here is the demo:
By playing a bit more with the function arguments, you can define extra mappings to execute your commands in a prod environment for instance:
nnoremap ,drp :call ExecuteDbtCommand("run --target prod")<CR>
nnoremap ,dtp :call ExecuteDbtCommand("test --target prod")<CR>
And that’s it, folks!
You can find a working version of the .vimrc in this repo. If you are interested in further learning how to leverage vim for data science you can check out this or that post. Let me know in the comments if you find the approach useful. If I see some tractions, I will add extra features.
Vim + dbt = ❤️ was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Level Up Coding - Medium and was authored by Grégoire Hornung
Grégoire Hornung | Sciencx (2022-10-11T12:31:34+00:00) Vim + dbt = ❤️. Retrieved from https://www.scien.cx/2022/10/11/vim-dbt-%e2%9d%a4%ef%b8%8f/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.