Posts Tagged ‘vim’

controll xmms2 from vim

Saturday, April 25th, 2009

a really basic way to controll xmms2 from your vim session:

map <leader>xn <Esc>:!xmms2 next<CR><CR>
map <leader>xb <Esc>:!xmms2 previous<CR><CR>
map <leader>xP <Esc>:!xmms2 pause<CR><CR>
map <leader>xp <Esc>:!xmms2 play<CR><CR>
map <leader>xs <Esc>:!xmms2 stop<CR><CR>

now, type ,xn in vim, and xmms2 will start to play the next track from your playlist.

“tidify” a json in vim

Tuesday, February 17th, 2009

If you have to edit json files from vim, you may want to make them more readable, here is how you can do this:

start by installing the JSON::XS perl module from the CPAN

sudo cpan JSON::XS

then, edit your .vimrc and add the following

map <leader>jt  <Esc>:%!json_xs -f json -t json-pretty<CR>

now while editing a json file, you can hit ,jt (or whatever your is set to) and tidify a json.

vim and git

Friday, December 5th, 2008

idea from http://use.perl.org/~Ovid/journal/37966 (ovid is full of really good ideas for vim):

to get a quick git diff in my vim session, put this in your .vimrc


map ,gh   :call SourceDiff() " gh for git history

function! SourceDiff()
    let filename = bufname("%")
    let command = 'git log -5 --pretty=format:"%h - (%ar) %an - %s" "'.filename.'"'
    let result   = split( system(command), "\n" )

    if empty(result)
        echomsg("No past revisions for " . filename)
        return
    endif

    " get the list of files
    let revision = PickFromList('revision', result)

    if strlen(revision)
        let items = split(revision, " ")
         execute '!git diff ' . items[0] . ' -- "' . filename .'" | less'
    endif
endfunction

the output will look like

Choose a revision:
1: ea0bb4d - (3 days ago) franck cuny - fix new_freq
2: a896ac7 - (5 weeks ago) franck cuny - fix typo
3: c9bc5fd - (5 weeks ago) franck cuny - update test
4: e9de4be - (5 weeks ago) franck cuny - change the way we rewrite and check an existing url
5: 3df1fd6 - (7 weeks ago) franck cuny - put id category

You choose the revision you want to check the diff against, and you got a (colorless) diff in your vim buffer.

Git branch everywhere

Thursday, June 26th, 2008

The current trend is to have the name of the current git branch everywhere. Personnaly I display it in my vim’s status bar, and in my zsh prompt.

Here is my vimrc configuration for this (I’m not the author of this function, and can’t remember where I saw it first):

set statusline=%&lt;[%n]%m%r%h%w%{'['.(&amp;fenc!=''?&amp;fenc:&amp;enc).':'.&amp;ff}%{g:gitCurrentBranch}%{']'}%y\ %F%=%l,%c%V%8P
autocmd BufEnter * :call CurrentGitBranch()
 
let g:gitCurrentBranch = ''
function! CurrentGitBranch()
let cwd = getcwd()
cd %:p:h
let branch = matchlist(system('/usr/local/git/bin/git  branch -a --no-color'), '\v\* (\w*)\r?\n')
execute 'cd ' . cwd
if (len(branch))
let g:gitCurrentBranch = '][git:' . branch[1] . ''
else
let g:gitCurrentBranch = ''
endif
return g:gitCurrentBranch
endfunction
vim statusbar

vim statusbar

and my zshrc:

local git_b
git_b='$(get_git_prompt_info '%b')'
PROMPT="%(?..%U%?%u:) $git_b %40&lt;...&lt;%/%(#.%U&gt;%u.%B&gt;%b) "

with the following script S55_git

zsh git

zsh git

Ack

Tuesday, June 24th, 2008

“Ack is designed as a replacement for 99% of the uses of grep.”

Ack is a real nice tool for searching your source code. It’s faster than grep because he already knows what you want : searching sources files :)

By default it will not search in SCM files (.svn, .cvs, …), backups files (source.pl~, source.pl.bak, …). You can specify what kind of files you want (–perl –cc, …), make it match some regex with –match, …

And you can set some defaults configuration in a .ackrc file ! Mine looks like this:

--sort-files
--color
--context=1
--follow

Check also: vim with ack integration.

Oh, and it’s the only program with –thpppt option.