If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!
It has been some time since I’ve done a Vim Tip of the Week, but I came across something today that I thought I would share. This tip will allow you to use Vim, with all its syntax highlighting glory, as a pager (similar to less or more).
If you use cat or less or more regularly to quickly view files, but you’d like to keep the same syntax highlighting that you’d get in Vim you can use an included config that makes Vim act as a pager. Setup the following within your .bashrc file, or wherever you keep your shell aliases:
alias vless='vim -u /usr/share/vim/vim71/macros/less.vim'
You’ll then need to re-read that file, which can be done using:
. .bashrc
At this point you can use vless to view a file, which will use the beloved syntax highlighting. Normal pager shortcuts should work. q to quit, / to search, pg-up, pg-dn, etc.
I recently posted about the power of Vim and we also learned about some of Vim’s Sweet Moves. Today I wanted to talk about a few more advanced actions within Vim that can make your life easier. What are they? Find and Replace.
Something that we’ve all had to do at one time or another is use find and replace within a document. This is really easy within Vim.
To change all instances of one word to another within one line use:
:s/word/replacement/g
To change all instances of one word to another within the entire file use:
:%s/word/replacement/g
To ask for confirmation before replacing each instance of a word use:
:%s/word/replacement/gc
I use these options regularly, especially when I’m upgrading between releases (or steps to upgrading). It’s simple to find & replace all instances of dapper within your /etc/apt/sources.list to edgy using one simple command within Vim. Once those lines are updated you’re ready to upgrade to the next release–it’s as simple as that!
If you want to find out more about Vim (as if this wasn’t enough!) you can use the command:
:help
while within Vim and it’ll give you some more tips and options. You can also run the command:
vimtutor
to walk you through doing all of these suggestions in actual practice. It’s a really easy to follow, very informative tutor.
Also, for some more advanced uses of Vim including using it as a file explorer, using tabs and quite a bit more check out my buddy Aaron’s post Use Vim!