Wednesday, 28 November 2012

More VIM amazingness

So I probably missed this in the few tutorials I looked at on vim.

But... oh... my... god...

Passing vim contents to the shell (ie bash)

You can pass your current file to a tool (like bash, or mysql) like this:

w | ! . %
w | ! mysql %

Or you can do this to just pipe in the "lines 7 to 9" or the current line to bash:

7,9:w ! bash
.w ! bash

Or pipe in selected text to mysql:

'<,'>:w ! mysql


Paraphrasing those clever folk at stackoverflow you can capture and run macro:
Perform desired editing interactively with

    vim -w log.vim file1.txt

and then repeat it on other files:
    for f in file*.txt; do vim -s log.vim $f; done 
or alternatively:
    for f in file*.txt; do vim -c '1,55d|25,35s/^/\/\/ /|w! '"${f}_new"'|q!' $f 

this is also very cool:

These things in your .vimrc are gold:

 syntax on

inoremap <expr> <C-Space> pumvisible() \|\| &omnifunc == '' ?
\ "\<lt>C-n>" :
\ "\<lt>C-x>\<lt>C-o><c-r>=pumvisible() ?" .
\ "\"\\<lt>c-n>\\<lt>c-p>\\<lt>c-n>\" :" .
\ "\" \\<lt>bs>\\<lt>C-n>\"\<CR>"
imap <C-@> <C-Space>
autocmd FileType php set omnifunc=phpcomplete#CompletePHP

Clearing the highlighting from your last search can be useful:
:let @/ = ""

Opening and finding files

Obviously :Ex, :Tex: and :Vex are good, and to those you can look for patterns in the:

   */filepat   filenames in current directory
   **/filepat  filenames in current directory or below
   *//pattern  vimgrep in current directory
   **//pattern vimgrep in the current directory or below

No comments:

Post a Comment