Archive
Format text in vim in 5 seconds
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.
Vim turned 20
Vim turned 20 on November 2, 2011. Happy birthday! And thank you Bram for this wonderful text editor! :x
Pretty print a JSON file
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
- http://chris.photobooks.com/json/default.htm (it can show you the path of a tag too)
- http://pretty-print.org/
- http://jsonviewer.stack.hu/
- http://www.shell-tools.net/index.php?op=json_format
- http://jsonlint.com/
- http://jsonformatter.curiousconcept.com/ (formatter and validator)
Command-line beautifiers
curl -s http://www.reddit.com/r/nsfw/.json | python -mjson.toolsudo apt-get install edit-json; prettify_json myfile.json
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”
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
Learn Vim in 30 minutes
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.
Best Vim Tips
Check out the page http://vim.wikia.com/wiki/Best_Vim_Tips for a nice collection of vim tips. The tips are categorized. Printer friendly version is here.
(I’ve read about it on Bernhard Brunner’s blog.)
How good are you in Vim?
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
- VimCasts
(Thanks Fred for the link).