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).