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
dragvisuals.vim is in the archive linked above.
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.
For dark background I prefer the elflord theme, which comes with the standard installation. It can be found here:
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.
Create the directory
.vim/colors in your HOME directory and copy
trivial256.vim in there. I created two symlinks too called
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.
With dark background (click to enlarge):
With light background (click to enlarge):
- Vim Color Scheme Test (choose a category from “Browse By File Type” to see ‘em all)
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?
Select the line in visual mode (
Shift + V). At the bottom you should see “
-- VISUAL LINE --“. Press
! (now at the bottom you should see
fmt (thus you have
:'<,'>!fmt at the bottom) and press
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,
Vim turned 20 on November 2, 2011. Happy birthday! And thank you Bram for this wonderful text editor!
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?
There are web-based and command-line solutions. As an extra, we show you how to do it in Vim too.
- http://chris.photobooks.com/json/default.htm (it can show you the path of a tag too)
- http://jsonformatter.curiousconcept.com/ (formatter and validator)
curl -s http://www.reddit.com/r/nsfw/.json | python -mjson.tool
sudo apt-get install edit-json; prettify_json myfile.json
This tip is based on this post: Editing json files in vim.
.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
There are several JSON visualizer add-ons for Firefox, e.g. JSONView.
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
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:
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! :)
You can find my
.vimrc settings here.
(I’ve read about it on Bernhard Brunner’s blog.)
If you think you are a Vim power-user and there is nothing new to learn, check out VimGolf. On this site you will find various challenges, and by submitting your own solution, you can earn scores. The goal is to improve your knowledge about Vim. If you are really good, you may get on the leaderboard.
I looked at some solutions and they look like a Perl nightmare :) Even if you don’t want to dive in, you can have a look at the “Resources for learning Vim“. I copy them here too:
- VIM From Novice To Professional By: Derek Wyatt
- Efficient Editing With vim
- Vim Tutorial @ linuxconfig.org
- Vim Tutorial Videos @ derekwyatt.org
(Thanks Fred for the link).