From eBower Wiki
Jump to: navigation, search

There is really only one editor that you'll ever need, and EMACS ain't it! If you're here, I'm assuming you're a vim user. I'm not here to give you a tutorial on vim or to explain how to do common stuff, there are plenty of places that do that already. I'm here to document what I've done to customize vim for my own use.

Fun and Games

A true vim user isn't all about work, we're also about fun. Like vim Adventures. Or Word War vi. Or even just being able to laugh at ourselves every so often.

Neat Tricks

Some things you can do with vim out of the box. And some of them aren't even really about vim but more about using an editor in places you may not have thought possible.

In-Place Privilege Escalation

Let's say you're editing a file and you didn't notice that you don't have the privileges to save it. Option one is to save it to a temp file, change the ownership and permissions, then move it over. Option two is:

:w !sudo tee %

Syncing plugins and .vimrc

I use SpiderOak as a cloud storage provider (you should try them out, they don't own the decryption keys to your data so they can't accidentally drop your box of data to the public). On each of my PCs I have a ~/cloud directory that I sync between the two. Instead of having a local config I've simply ran ln -s ~/cloud/vimrc ~/.vimrc && ln -s ~/cloud/vim ~/.vim. Now whenever I make edits to my vim config files it automatically syncs to my other systems.

Using fc

First, edit your ~/.bashrc and add the line FCEDIT=vim someplace near the top. Now close the terminal and open it up again. If you every have a huge command you need to type in and you need to tweak it just a little bit you could hit the up arrow and move your cursor over to the right spot(s) to edit things, or you can type in fc. This command will not appear in your history list, but will load up the last command you typed into the editor specified (do you really need an option?) so you can modify it as you see fit. Do a standard :wq to run the edited command.

For more advanced usage, you can always check out the man page. If you're lazy, fc -l will essentially show the same output as history and fc [history_number] will edit a specific command.

.vimrc

The ~/.vimrc file contains your personalized configuration for vim. The rc extension stands for (arguably) runtime configuration and it's read by vim on startup.

Formatting

One useful feature of .vimrc is to define formatting options. I won't go over all of the options, but only the correct ones as defined by the ones I use!

set expandtab
set shiftwidth=2
set tabstop=2

The expandtab variable will convert messy tabs into an appropriate number of spaces. The shiftwidth helps tell vim how many columns to indent while the tabstop tells it how to interpret evil tab characters. I like my tabs short so I set it to 2.

PrettyXML

If you deal with XML documents, you probably have xmllint from the libxml2-utils package installed and you can use this to reformat your XML right in vim. The documentation for that is here, and you can simply copy the text into your .vimrc file.

It assumes your entire document is XML and can be executed by running :PrettyXML in normal mode.

Note that this will not validate the XML but simply reformat it. To validate it, I prefer to have a full output of xmllint but my XSD is in a common shared location and annoying to type in. By adding this to my .vimrc file I can quickly validate the most common (and complex) XML documents I deal with:

command! XMLValidateMySchema execute '!xmllint --schema /very/long/path/to/xml/schema/my_schema.xsd % > /dev/null'

Note that I pump it to /dev/null because I couldn't care less about the formatted output, I just care about STDERR. Now I can simply type :XMLValidateMySchema to quickly validate the XML file. Obviously, the contents above are obfuscated since it contains proprietary information but you should get the general idea.

More useful vim-based XML tricks can be found here.

vim Plugins

Plugins typically get installed in the ~/.vim/plugin directory and often refer to the ~/.vim/bin directory.

PrettySQL

The PrettySQL plugin will reformat selected SQL into something a bit, well, prettier. It uses the package python-sqlparse so make sure that's installed (manual installation can be done here). Installation involves copying the files from github to ~/.vim/plugin and ~/.vim/bin as appropriate. Running the plugin means using v or V to select the SQL statement (no pun intended) and then using <Leader>fs - this is usually \fs for the uninitiated.

indentLine

If you get lost in a maze of nested codeblocks as easily as I do, you may want to look into indentLine. Just download the latest version and extract it into your ~/.vim directory - it will place it into ~/.vim/after/plugin. You can check out the documentation for some customization variables you can add to your ~/.vimrc file. I like using an atypical character for this, so I have the following:

" Set variables for indentLine.vim
let g:indentLine_char = '┆'

The big downside of indentLine is that the character shows up when you select the text and copy/paste it.

vim-indent-guides

Enter vim-indent-guides. Download this into someplace using the following:

git clone git://github.com/nathanaelkane/vim-indent-guides.git
cp -r vim-indent-guides/* ~/.vim/

Now you can edit your ~/.vimrc to add lines like these to edit the color scheme:

" set colorscheme information
colorscheme default
set t_Co=256
 
" vim indent guides
let g:indent_guides_auto_colors = 0
autocmd VimEnter,Colorscheme * :hi IndentGuidesOdd  ctermbg=234
autocmd VimEnter,Colorscheme * :hi IndentGuidesEven ctermbg=235

You can see a chart of the cterm256 colors here.

The problem is that you need to run \ig to use it. If you want it to run by default you can add the following line to the .vimrc file:

autocmd VimEnter,Colorscheme * :IndentGuidesEnable

If you get an error like:

E411: highlight group not found: Normal

Just add the following line to your .vimrc:

colorscheme default