Vim

Just a collection of Vi/Vim stuff that I find most useful and often forget and then end up having to search for time and time again...

This looks like a cool way to learn VIM!

Deleting lines...

:g/regexp/d   -- delete lines matching regexp
:g/regexp/    -- just highlight but don't delete lines that would be delete
:g!/regexp/d  -- delete lines NOT matching regexp
:v/regexp/d  -- delete lines NOT matching regexp (same as above)

Remove Control Characters

See SO reference here.

Control characters - :%s/[[:cntrl:]]//g
Non-ASCII characters - :%s/[^[:print:]]//g
	

Editor Font

Type Face

To bring up a font selection dialog type the following (works on Windows and Linux but not all platforms may support it).

set guifont=*

To see what font you have set type the following.

set guifont?

To set the font using the command rather than a dialog box use the following (the font name depends on the GUI used). On windows you can use a '_' instead of a space, otherwise you must escape the space. To include commas in font name you must escape them.

set guifont=courier_new:h12

Colours

I find the default Vim colour for comments against a dark background. There are a couple of options.

Miscellaneous

Runtimepath on Windows

This is %userprofile%\vimfiles. Create the file vimrc to act as the equivalent of the Linux ~/.vimrc file. Under the windows run time path create the folder plugins (and bundle for pathogen).

Veritcal Line For Page Edge

If you want to make sure you're code is only 80, 100 or whatever characters in width and want a vertical guide line on that column use the following:

:set colorcolumn=80

The above puts a vertical coloured column at column 80. You can have multiple vertical lines using a comma seperated list.

To change the colour of the column use the following:

hi ColorColumn ctermbg=lightgrey guibg=lightgrey

Spell Checking

Wow, Vim supports spell checking. To enable spell checking just enter command mode and type the following:

:set spell spelllang=en_gb

Write As Admin (Even If Vim Not Opened Sudo)

:w !sudo tee %

Vim Plugins

Two very usefule links are:

Pathogen.Vim

Pathogen.vim, as the website says, "...makes it super easy to install plugins and runtime files in their own private directories...". To install just run the following (on your Linux box) and it will run straight out of the box.

mkdir -p ~/.vim/autoload ~/.vim/bundle && \
curl -LSso ~/.vim/autoload/pathogen.vim https://tpo.pe/pathogen.vim

Then edit your .vimrc file to contain the following

execute pathogen#infect()
syntax on
filetype plugin indent on

Now any plugins you install to ~/.vim/bundle will be automatically available to you. Yay!

On Windows just replace ~/.vim with %userprofile%\vimfiles. This is the Windows equivalent of the linux ~/.vim folder.

To check if Pathogen and any bundles have been loaded use :scriptnames and search for pathogen.vim.

Better Code Navigation

Thank you Munen Alain M. Lafon for the article VIM as Python IDE, which the following is taken from. Loved this Vim plugin... it seems to work with everything from C to Python to SQL.

Install Exuberant Ctags (the CTags which was distributed with my system was out of date so check this). I installed CTags 5.8 and it worked fine. To do this download the .tar.tgz file to a folder of your choice. Change directory to that folder and run the following (modifying CTags version if there is a mroe up to date one!).

tar xzvf ctags-5.8.tar.gz
cd ctags-5.8
./configure
make
sudo make install

Don't worry about the odd compilation warning. I had a couple. As long as the make install looks something like the following you'll be okay

enigma@enigma-VirtualBox:~/Downloads/ctags-5.8$ sudo make install
cp ctags /usr/local/bin/ctags  &&  chmod 755 /usr/local/bin/ctags
cp ./ctags.1 /usr/local/share/man/man1/ctags.1  &&  chmod 644 /usr/local/share/man/man1/ctags.1

Installing the navigation pugin Vim-TagList is straightforward. From the zip file, I downloaded tag_list46.zip just extract the contents into your ~/.vim directory so that taglist.vim goes into your ~/.vim/plugin directory and taglist.txt does into your ~/.vim/doc directory.

Now to get a code navigation pane on the LHS of your Vim window enter the vim command :TlistOpen and there you go! Proper IDE style code navigation pane...

Syntastic

Wow, get PyLint running on your Python code and display the results in your Vim editor... awesome!

Follow instructions at https://github.com/scrooloose/syntastic.

Make Linux Vim Key Shortcuts Behave Like Windows

I use Vim a lot on Windows and like the cut/copy/paste key shortcuts. Wanted to replicate on my Linux box. The following tip by Ludwig Weinzierl did the trick nicely...

Add the following lines to your _vimrc or .vimrc

source $VIMRUNTIME/mswin.vim
behave mswin

My .vimrc file

How I Like It

This is the contents of my .vimrc file. I often forget where it is on my Windows system so this wiki article very useful. In brief... you can easily edit .(g)vimrc files from within Vim using the following.

:e $MYVIMRC
:e $MYGVIMRC

There is the small caveat that the above only works if Vim has found your .vimrc file. If one doesn't exist create it in your home directory...

And this is my current file...

set hlsearch          -- Heighlight all search patterns matches
set incsearch         -- Move cursor to matched string, while typing
set autoindent        -- keep the indent of the previous line
set noexpandtab       -- tabs are never replaced by spaces
set tabstop=3         -- how many columns a tab counts for
set shiftwidth=3      -- columnsindented by the reindent operations
set list              -- turn on special character display
set listchars=tab:\|\ -- make only tabs replaced by a pipe and space
set colorcolumn=80              -- Highlight 80th column as guide
hi ColorColumn ctermbg=lightgrey guibg=lightgrey

execute pathogen#infect()
syntax on
filetype plugin indent on

source $VIMRUNTIME/mswin.vim
behave mswin

set number
set mouse=a
set spell spelllang=en_gb
set guifont=consolas:h12
colorscheme despacio

set statusline+=%#warningmsg#
set statusline+=%{SyntasticStatuslineFlag()}
set statusline+=%*

let g:syntastic_always_populate_loc_list = 1
let g:syntastic_auto_loc_list = 1
let g:syntastic_check_on_open = 1
let g:syntastic_check_on_wq = 0

Or, if you want to get fancy with the listchars settings...

set encoding=utf-8
set listchars=tab:».,trail:·

Debugging

Strangely I found when editing Yorick script files (a .i extension) the tab settings in my .vimrc file were being ignored (or superceeded). Thank you to this SO question for helping out!

To see a list of .vim files loaded use the command :scriptnames. On my system this fave the following output, which I've snipped to save space...

  1: /usr/share/vim/vimrc
  2: /usr/share/vim/vim73/debian.vim
<snip>
 11: ~/.vimrc
 12: /usr/share/vim/vim73/mswin.vim
<snip>
 23: /usr/share/vim/gvimrc
 24: /usr/share/vim/vim73/syntax/progress.vim
	

Doing a grep I then found in progress.vim, the line "/usr/share/vim/vim73/syntax/progress.vim:set expandtab". So that's the culprit. This could also have been achieved using the command :verbose set expandtab ?, where the question mark is very important! This will report where the value was last set, and indeed, I did see...

expandtab
        Last set from /usr/share/vim/vim73/syntax/progress.vim