Posts mit dem Label vim werden angezeigt. Alle Posts anzeigen
Posts mit dem Label vim werden angezeigt. Alle Posts anzeigen

Sonntag, 12. April 2015

My vim/gvim setup for a node.js project

In my .vimrc, there is a function ProjectLoad() which will look for and read a session file in the current working dir, and then look for/source a  local .vimrc.

In the .vimrc for my current node.js project, there is a function NodeTest() which will run my project's test suite and paste the results into a new buffer.

Here are the relevant function definitions:

~/.vimrc:

function! ProjectLoad()
    if filereadable(".vimsession")
        so .vimsession
    endif
    if filereadable(".vimrc")
        so .vimrc
    endif
endfunction
function! ProjectSave()
    mks! .vimsession
endfunction

command! ProjectLoad call ProjectLoad()
command! ProjectSave call ProjectSave()

/path/to/project/.vimrc:

function! NodeIntoFile()
    redir! > out.log
    set nomore
    !node js/test/runTests.js 2>&1
    redir END
endfunction

function! NodeIntoBuffer()
    new
    r !node js/test/runTests.js
endfunction 
command! NodeTest call NodeIntoBuffer()


I wrote NodeIntoFile() while I was trying to auto-run the tests upon :w (i.e., FileWritePost), but that doesn't work out on Windows because it will always pop up a cmd window which stinks. If you know a way around that, please let me know!

Otherwise, maybe you can use those functions in your own setup.

Mittwoch, 28. Januar 2015

Using VIM as a Frontend for MySQL

Being deeply dissatisfied with MySQL Workbench, which tends to go nonresponsive on me at least once per day, I started looking around for other solutions. Since I'm a VIM aficionado, this occurred to me as one of the places to look. Of course, I got lucky.

Here's the solution (It's a slightly modified version of the code found here: http://superuser.com/questions/266934/how-to-pass-vim-buffer-contents-through-shell-command-and-capture-the-output-to):

function! FilterToNewWindow(script)
    let TempFile = tempname()
    let SaveModified = &modified
    exe 'w ' . TempFile
    let &modified = SaveModified
    exe 'split ' . TempFile
    exe '.! ' . a:script
    exe 'setlocal buftype=nofile'
    exe 'setlocal bufhidden=hide'
    exe 'setlocal noswapfile'
    exe 'setlocal noet'
    exe 'setlocal ts=25'
    exe 'setlocal number'
endfunction
command! MF call FilterToNewWindow('mysql --defaults-file=/etc/mysql/debian.cnf -t <DATABASENAME>')

Paste the above code into your .vimrc, open a new vim, and...:

  • Enter a SQL command into current buffer
  • Do :MF at the line you wish to use in mysql
  • A new buffer will open up with the results of your query. (Actually, of course, it's the other way around, but who cares...)
  • This buffer will be a scratch buffer, so you don't get an ugly error message when doing :q
You should use the mysql defaults file, otherwise your mysql credentials will end up in (I guess) your bash history. The -t argument will make mysql export its results in the nice table format used in the mysql command line client. setlocal number, noet and ts (tabsize) are not necessary, but I want them in my scratch buffer anyway.

Of course, you can use FilterToNewWindow() with all kinds of external programs. I'm sure I will put it to good use in the future.