Using VimDiff for file comparison and merge

How to use:

1. From terminal –

$vimdiff file1 file2

2. From Vim –
Open vim and use the following commands if you already have windows with other buffers open in the current tab.

:tabe file1
:vert diffsplit file2


:e file1
:vert diffsplit file2

– If you have two or three windows open in your current tab and they display the buffers that you want to diff, you might want to turn on diff mode for each window:

:windo diffthis

or, synonym:

windo set diff

– vimdiff will correctly highlight differences inside a line (whole line pink, differing characters red).

– If you load up two files in splits (:vs or :sp), you can do :diffthis on each window and achieve a diff of files that were already loaded in buffers

– turning off:
:diffoff can be used to turn off the diff mode.
issuing :diffoff! which will turn off diff mode for all windows in the current tab page

Keyboard Shortcuts:
do – Get changes from other window into the current window.
dp – Put the changes from current window into the other window.
]c – Jump to the next change.
[c – Jump to the previous change.
Ctrl W + Ctrl W – Switch to the other split window.

Tips on file comparison:

– Sometimes the difference can be a special character – :set list in Vim will show whitespace. End of lines show as ‘$’ and carriage returns usually show as ‘^M’.

– Counting number of characters in a file from bash –

wc -c filename

Useful resources:
1. vimdiff – the cool way to diff for vim users
2. Vim documentation: diff
3. Diffuse -graphical tool for merging and comparing text files


Vim tip: How to fix Python exception IndentationError

The problem is usually with mixup in tabs and spaces –

Solution 1

1. Apply following command in Vim to highlight tabs, spaces and other whitespace differently.

:set listchars=tab:>-,trail:-,eol:$ list

2. Apply the following to set correct width

:set shiftwidth=4 tabstop=4 expandtab 

3. running


to fix the problem,Vim automatically fixes all indentation space and tab mix ups.

Solution 2

1. set ‘list’, so that you can see the whitespace and change.

Have the following mapping in .vimrc for this:

nnoremap    <F2> :<C-U>setlocal lcs=tab:>-,trail:-,eol:$ list! list? <CR>

2. Ensure ‘expandtab’ is reset, check using following command –

:verbose set ts? et? 

3. To expand all leading spaces (wider than ‘tabstop’), use retab.

retab takes a range, so specify % to mean “the whole file”.

:set tabstop=4      " To match the python file
:set noexpandtab    " Use tabs, not spaces
:%retab!            " Retabulate the whole file

Turn Vim into a powerful Python IDE: Essential Vim plugins for Python programmers

Essential plugins:

1. Pathogen: A must have if you want to keep your vim plugins in a clean state. Makes it super easy to install plugins and runtime files in their own private directories.

2. Powerline: Powerline is a statusline plugin for vim, and provides statuslines and prompts for several other applications, including zsh, bash, tmux, IPython, Awesome and Qtile.

3. jedi-vim: A python autocompletion library for VIM.

(You’ll need Vim python support enabled for this plugin to work. You can check if your Vim supports Python using this (you should see a line with +python in it as a result):

vim --version | grep +python

if not found, you need to install Vim, the following command will remove vim-tiny, and install Vim 7.3 with +python and –with-features=huge:

sudo apt-get remove vim-tiny
sudo apt-get install vim


4. NERDTree – NerdTree is a filebrowser that pops up in a split when you need it and features a tree like file browse.

5. NERDCommenter: Very useful tool for multiline commenting/uncommenting, just like IDLE.

For more information like this check the following :

1. Vim as a Python IDE: Talks about some of the plugins here and others.

2. Martin Brochhaus’s excellent talk on converting Vim into a Python IDE

You can donwload the talk slides and other stuff from his github page. You can also check his excellent .vimrc file here (which I use.)

How to install Vim 7.4 with Perl, Python and Ruby support on Ubuntu 12.04

1. Remove preexisting vim installations:

sudo apt-get remove vim-tiny vim vim-nox

2. Make sure you have mercurial install, otherwise install it:

sudo apt-get install mercurial

3. Download Vim 7.4 from repo:

hg clone vim74

4. change directory to vim74, and install, for example I have used installed with following options enabled:

./configure --enable-perlinterp --enable-pythoninterp --enable-rubyinterp --enable-cscope  --with-features=huge --prefix=/usr
make install

Vim: How to start using it

The following quote from Why, oh WHY, do those #?@! nutheads use vi? basically sums up the “philosophy” of Vim use:

The first time you stumble into vi or vim, you are shocked and disgusted that you have to use ‘i’ to start typing text. Can’t remember which of hjkl moves in which direction. And you need to press ‘a’ if you want to insert after the last character of the line. Since you’re already probably used to other editors, and if the arrow keys work in insert mode (they do if the system is configured properly, which is over 90% of the cases), you will stay in insert mode and not go back to normal mode except if absolutely necessary. You’ll probably spend 20 minutes with it, and never go back. And also complaining: “How on earth am I going to remember whether I’m in insert or normal mode?”

Turns out, this is just a completely wrong way to use vi or vim. Using vi/vim properly, you don’t use it modally. You are always in normal mode, and only enter insert mode for short bursts of typing text, after which you press to go to normal mode. Thus, the remembering-the-mode problem just doesn’t exist: you don’t answer the phone in insert mode to get back to vi and not remember where you were. If you are typing text and the phone rings, you exit insert mode and then answer the phone. Or you press when you come back. But you never think about insert mode as a mode where you stay.

The best way to learn Vim IMHO is to learn incrementaly. Here’s a great article on that – Everyone Who Tried to Convince Me to use Vim was Wrong.

Tips to getting started with Vim :

1. Create a list of substitue Vim commands/keyboard shortcuts for the commands/keyboard shortcuts that you use for your current editor of choice.

There are some great cheatsheets and reference tables available online to help you out with creating your list.

Vim Commands Cheat
Essential Vim
Vim Quick Reference Card

2. Practice with vimtutor

3. Make mode change easier

Another important tips is to use another shortcut for Esc key. I use Ctrl-C. Here’s a link for other shortcuts and tricks to use alter methods.

4. Install essential plugins: These very useful plugins will make you usage experience a whole lot easier if you intend to code in Vim.

5. Watch Derek Wyatt’s Vim screencasts: Derek Wyatt has some excellent Vim tutorial videos starting from novice to expert level which will make you a Vim expert in no time.

Some useful talks and tutorials:

1. Vim Introduction and Tutorial : Nice tutorial for beginners with visual guides.

2. Ben Orenstein – Write code faster: expert-level vim (Railsberry 2012)

you can download the lecture materials from his github page. and his .vimrc file is here.

3. Vim Cookbook by Steve Oualline

4. archive : Great resource for digging deep into vim. Advanced stuff.