Archive

Archive for the ‘vim’ Category

Vim tricks by Damian Conway

December 13, 2013 Leave a comment

At reddit I found an interesting talk given by Damian Conway. In this talk he shares some cool vim tricks.

The assets to the presentation are available here: Instantly_Better_Vim_2013.tar.gz

I found the following tips particularly interesting.

"====[ Make the 81st column stand out ]====================

"    " EITHER the entire 81st column, full-screen...
"    highlight ColorColumn ctermbg=magenta
"    set colorcolumn=81

    " OR ELSE just the 81st column of wide lines...
    highlight ColorColumn ctermbg=magenta
    call matchadd('ColorColumn', '\%81v', 100)

"=====[ Highlight matches when jumping to next ]=============

    " This rewires n and N to do the highlighing...
    nnoremap <silent> n   n:call HLNext(0.4)<cr>
    nnoremap <silent> N   N:call HLNext(0.4)<cr>

    " OR ELSE ring the match in red...
    function! HLNext (blinktime)
        highlight RedOnRed ctermfg=red ctermbg=red
        let [bufnum, lnum, col, off] = getpos('.')
        let matchlen = strlen(matchstr(strpart(getline('.'),col-1),@/))
        echo matchlen
        let ring_pat = (lnum > 1 ? '\%'.(lnum-1).'l\%>'.max([col-4,1]) .'v\%<'.(col+matchlen+3).'v.\|' : '')
                \ . '\%'.lnum.'l\%>'.max([col-4,1]) .'v\%<'.col.'v.'
                \ . '\|'
                \ . '\%'.lnum.'l\%>'.max([col+matchlen-1,1]) .'v\%<'.(col+matchlen+3).'v.'
                \ . '\|'
                \ . '\%'.(lnum+1).'l\%>'.max([col-4,1]) .'v\%<'.(col+matchlen+3).'v.'
        let ring = matchadd('RedOnRed', ring_pat, 101)
        redraw
        exec 'sleep ' . float2nr(a:blinktime * 1000) . 'm'
        call matchdelete(ring)
        redraw
    endfunction

"====[ Make tabs, trailing whitespace, and non-breaking spaces visible ]======

    exec "set listchars=tab:\uBB\uBB,trail:\uB7,nbsp:~"
    set list

"====[ dragvisuals ]======

    runtime plugin/dragvisuals.vim
    vmap  <expr>  h        DVB_Drag('left')
    vmap  <expr>  l        DVB_Drag('right')
    vmap  <expr>  j        DVB_Drag('down')
    vmap  <expr>  k        DVB_Drag('up')
    "vmap  <expr>  D        DVB_Duplicate()

    " Remove any introduced trailing whitespace after moving...
    let g:DVB_TrimWS = 1

The file dragvisuals.vim is in the archive linked above.

Categories: vim Tags:

vim color schemes with dark and light backgrounds

September 12, 2013 Leave a comment

Problem
I’m looking for a nice color scheme in vim for (1) dark background, and for (2) light background. Also, I want to be able to switch between them easily.

Solution
For dark background I prefer the elflord theme, which comes with the standard installation. It can be found here: /usr/share/vim/vim73/colors/elflord.vim.

For finding a nice and simple theme for light background I had to do some research. Finally I came across the trivial256 color scheme. However, I didn’t like that strings were colored red so I changed one line:

hi String       ctermfg=darkgreen

The modified version can be downloaded from here.

Installation
Create the directory .vim/colors in your HOME directory and copy trivial256.vim in there. I created two symlinks too called light.vim and dark.vim:

dark.vim -> /usr/share/vim/vim73/colors/elflord.vim
light.vim -> trivial256.vim

Specify in your ~/.vimrc which one should be the default:

colorscheme dark
"colorscheme light

In vim you can change the color scheme easily:

:colo light
:colo dark

You need to type just one of them. Notice that the extension “.vim” is omitted.

Screenshots
With dark background (click to enlarge):

dark

With light background (click to enlarge):

light

Links

Format text in vim in 5 seconds

April 18, 2012 Leave a comment

Problem
You use vim and you don’t want any lines to be longer than 80 characters. However, when you select a paragraph in the browser and you paste it in vim, the whole paragraph is inserted in one long line. Since you are just as lazy as me, you don’t want to break the line manually. What to do?

Solution
Select the line in visual mode (Shift + V). At the bottom you should see “-- VISUAL LINE --“. Press ! (now at the bottom you should see :'<,'>!), type fmt (thus you have :'<,'>!fmt at the bottom) and press Enter. Done.

What just happened?
We passed the selected text to the external Unix command fmt, which reformatted the text. Vim then inserted the output of fmt to the place of the selected text.

So, if you have a list of words, one word in a line, and you want to sort them, just pass them to the external command sort: select the lines visually, !sort and Enter.

[ reddit discussion ]

Categories: vim Tags: , , ,

Vim turned 20

November 10, 2011 Leave a comment

Vim turned 20 on November 2, 2011. Happy birthday! And thank you Bram for this wonderful text editor! :x

Categories: vim Tags:

Pretty print a JSON file

August 10, 2011 Leave a comment

This post is based on the following SO threads: one; two.

Problem

You have an unreadable JSON file from which you want to extract some data… How to prettify it, i.e. how to make it human readable?

Solution

There are web-based and command-line solutions. As an extra, we show you how to do it in Vim too.

Web-based prettifiers

Command-line beautifiers

Vim :)

This tip is based on this post: Editing json files in vim.

In my .vimrc file I had to add the following lines:

" pretty-print JSON files
autocmd BufRead,BufNewFile *.json set filetype=json
" json.vim is here: http://www.vim.org/scripts/script.php?script_id=1945
autocmd Syntax json sou ~/.vim/syntax/json.vim
" json_reformat is part of yajl: http://lloyd.github.com/yajl/
autocmd FileType json set equalprg=json_reformat

When opening a .json file, it will be colored using the json.vim syntax file. Selecting a text and pressing the “=” button will indent the marked text using json_reformat.

Firefox add-on

There are several JSON visualizer add-ons for Firefox, e.g. JSONView.

Add syntax highlighting to “less”

August 10, 2011 Leave a comment

This entry is based on this post: Redirecting wget to STDOUT – now with Syntax Highlighting.

Less is a program similar to more, but it allows backward movement in the file as well as forward movement. Example:

cat big_text_file.txt | less

If you have vim installed, it comes with a script called less.sh that acts as a replacement of less providing syntax highlighting. Here is how to use it:

$ locate less.sh
/usr/share/vim/vim73/macros/less.sh
$ sudo ln -s /usr/share/vim/vim73/macros/less.sh /usr/bin/vless
$ curl http://www.python.org | vless
Categories: bash, vim Tags: , ,

Learn Vim in 30 minutes

July 13, 2011 2 comments

Vim is one of the best text editors (if not THE best…). You can learn the basics in 30 minutes. All you have to do is launch the following command:

vimtutor

Then follow the instructions.

Vim has a learning curve, I admit, but it’s logical and not complicated. You don’t have to learn all its tricks at once. Learn the basics with vimtutor, read it over again if you need, then start using it. If you need some intermediate stuff that is not explained in the tutorial, look after it on the web.

Warning! Vim is highly addictive! Once you get used to it, you don’t want to use any other text editors. You’ve been warned! :)

Links

You can find my .vimrc settings here.

Categories: vim Tags:
Follow

Get every new post delivered to your Inbox.

Join 72 other followers