Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Execute query under cursor #33

lackac opened this issue Sep 9, 2018 · 3 comments

Execute query under cursor #33

lackac opened this issue Sep 9, 2018 · 3 comments


Copy link

lackac commented Sep 9, 2018

I was making a lot of use of vim-dadbod in recent weeks. One thing that I was missing was the ability to run the query under the cursor. I ended up creating a query text object to help me do just that. It is implemented like this:

" sql query text object
vnoremap aq <esc>:call search(";", "cWz")<cr>:call search(";\\<bar>\\%^", "bsWz")<cr>:call search("\\v\\c^(select<bar>with<bar>insert<bar>update<bar>delete<bar>create)\>", "Wz")<cr>vg`'
omap aq :normal vaq<cr>

It searches backward for the last ; character or the beginning of the file and then from there forward for the next SQL keyword (I'm sure the list there might be refined, this worked for me though). It switches to visual mode and searches for the next ; character.

Then I mapped <C-Q> to execute the query using b:db as the database url:

" setup b:db and run queries with it
nmap <expr> <c-q> exists("b:db") ? 'vaq:DB b:db<cr>' : ':DB b:db = g:db_'
vmap <expr> <c-q> exists("b:db") ? ':DB b:db<cr>' : '<esc>:DB b:db = g:db_'

The conditional expression checks first whether b:db is set. It starts a command line expression to set it based on a g:db_* variable if it's not. We could throw an error instead here.

Would it make sense to add this to dadbod?

Copy link

tpope commented Sep 10, 2018

Dadbod has preliminary map support, although it's not enabled by default. Try this:

nmap <expr> <C-Q> db#op_exec()
xmap <expr> <C-Q> db#op_exec()

Now you can use <C-Q>aq in normal mode or <C-Q> in visual mode to do what you intend. It's superior to mapping to :DB directly because it uses the exact selection rather than just line numbers.

I haven't mapped it by default because I'm not sure what a good map would be. <C-Q> is okay but for conflicting with terminal flow control. I kind of like Q, but lots of people inexplicably map that to gq. I might have to settle for making it an option (gross).

A few additional notes:

  • I don't want to take responsibility for something like aq because it's really hard to generalize to all use cases, and there's no reason it can't live in a separate plugin.
  • I really, really hate the "prompt me the first time" stuff. It's the reason I couldn't make peace with dbext. I provided several methods of defaulting and a map is definitely not the place to contradict that.

Copy link

lackac commented Sep 10, 2018

All of that makes sense. I'm also starting to get annoyed by the prompt.

I'm closing this now as it looks like there's no action to take on it here.

@lackac lackac closed this as completed Sep 10, 2018
Copy link

oliverralbertini commented Sep 3, 2021

I was trying to map <c-c><c-c> since I've gotten used to using slime recently, but couldn't get it to work.

I ended up using

autocmd FileType sql xmap <expr> <C-M> db#op_exec()

By making each query it's own paragraph, running queries is vip<enter>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
None yet

No branches or pull requests

3 participants