Open text files in vim CLI on Ubuntu

vim_compThis article is for vim users who want to use vim to edit plaintext files directly from the file manager. Instead of using gvim, the idea is to open the text file in vim in the terminal. The file will be opened in cmdline mode of vim. The following steps explain how to do this on Ubuntu.

  • Create a new file /usr/share/applications/vim.desktop and add the following contents in it:
    [Desktop Entry]
    Keywords=editor
    Name=Vim
    Comment=Vim text editor
    Exec=vi %f
    Icon=vim
    Terminal=true
    Type=Application
    MimeType=text/plain
    Categories=GTK;Utility;TextEditor;
  • Next, open the file ~/.local/share/applications/mimeapps.list and search for the line that starts with text/plain=. Change it to
    text/plain=vim.desktop

Enable spellcheck and language in vim

vim_compMy favourite lightweight editor on Linux is Leafpad. Though I am more comfortable with vim, I use Leafpad for drafting articles like this one. An option Leafpad lacks is spell checking and I was looking for another alternative to do that. However, most of them were bulky or written in languages like Python making them damn slow.

Finally I ended up enabling spell check by default in vim. To do that, I added the following in ~/.vimrc:

set spell

However, I prefer to use UK English spellings. The system default is US English. To use the UK option I ad to add the following:

setlocal spell spelllang=en_gb

Note that this also enables spellcheck so you do not need to add the earlier switch. To urn it off, type in the following while in command mode of vim:

:set nospell

To check only comments and not actual code, add the following line:

syntax enable

The syntax description files already know what to check and what not.

To add a word to the dictionary, place the cursor on the word and press <zg> in command-mode.

Whitespace removal on save

To remove all trailing whitespace characters from a file when you save it, add the following auto-command to ~/.vimrc:

autocmd BufWritePre * :%s/\s\+$//e

To specify a single file type:

autocmd BufWritePre *.c :%s/\s\+$//e

To specify certain file types:

autocmd FileType c,cpp,java,php autocmd BufWritePre <buffer> :%s/\s\+$//e

vim column selection

vim_compThere are many editors which support column mode selection. It comes handy in many situations e.g. when you want to remove the preceding line numbers before each line in a code snippet. To do this in vim:

  1. Open the file
  2. Press <v> to enter visual mode
  3. Press <Ctrl-v> to enter the block selection mode
  4. Use the arrow keys to select all the text you want
  5. Press <Del> to remove the selection, <y> to yank (copy), <c> to cut

To insert spaces or shift a block towards right by spaces:

  1. Press <Ctrl-v> to enter the block selection mode
  2. Select the first column of each line
  3. Press <Shift-i> for block insertion
  4. Insert space(s)
  5. <Esc> to normal mode

Neovim: fork to simplify and extend vim

vim_compvim is 20+ years in age and there’s some fresh development around it – vim is being refactored. Today it is awfully difficult for a single maintainer to keep up with the huge codebase as well as the growing plugin ecosystem. In my experience I have seen the same vim plugin work perfectly on one distro and behave differently on another. To bring some order into the chaos vim has been forked recently to refactor the code on a large scale (read aggressively) with the following goals in mind:

  • Simplify maintenance to improve the speed that bug fixes and features get merged.
  • Split the work between multiple developers.
  • Enable the implementation of new/modern user interfaces without any modifications to the core source.
  • Improve the extensibility power with a new plugin architecture based on coprocesses. Plugins will be written in any programming language without any explicit support from the editor.

The initiative has been named “vim’s rebirth for the 21st century” and the authors have started a fundraiser to keep the project fueled. In my opinion it is about time vim undergoes a modernization. From a recent check-in today it seems that the new vim executable is going to be named nvim.

Installation

To install on Ubuntu, run:

$ sudo add-apt-repository ppa:neovim-ppa/unstable
$ sudo apt-get update
$ sudo apt-get install neovim

Webpage: neovim

Customize vimdiff color scheme

vim_compvimdiff’s default color scheme is not everyone’s cup of tea. However, it is such a powerful and useful tool that I end up using it frequently. Here’s the snippet from my .vimrc file to customize the vimdiff default color scheme. You can play around with the colors to find a color scheme soothing to your eyes.

highlight DiffAdd cterm=none ctermfg=fg ctermbg=Blue gui=none guifg=fg guibg=Blue
highlight DiffDelete cterm=none ctermfg=fg ctermbg=Blue gui=none guifg=fg guibg=Blue
highlight DiffChange cterm=none ctermfg=fg ctermbg=Blue gui=none guifg=fg guibg=Blue
highlight DiffText cterm=none ctermfg=bg ctermbg=White gui=none guifg=bg guibg=White

Sometimes, this script may fail as in some terminals like a putty connection, vim fails to identify the bg and fg colors. For that, explicitly set the color scheme before the above snippet:

highlight Normal term=none cterm=none ctermfg=White ctermbg=Black gui=none guifg=White guibg=Black

VIM Adventures: learn vim while playing

vim_compFor guys who work on Linux but find vim to be too intimidating, there’s a better way to learn vim – through a game which has many levels; thanks to VIM Adventures!

The amazing thing about vim is that you keep learning more and more fascinating features of it. Even after using vim for many years, yesterday I learnt from another vim enthusiast that there is a shortcut to jump to just the upper or lower curly braces from any point in the code.

  • [[ moves the cursor to the starting curly bracket { of the function starting above current position.
  • ]] moves the cursor to the starting curly bracket of the function starting below current position.
  • [{ moves the cursor to the start of the current scope [starting curly bracket {].
  • ]} moves the cursor to the end of the current scope [closing curly bracket }].
  • To copy a whole function press [[ (to jump to the starting braces) followed by ya{. Remember that if your function signature starts in the line above curly braces, you need to copy that separately.

Webpage: VIM Adventures

vim copy paste registers

vim_compThere’s an excellent way of storing yanked buffers in vim using numbered registers and named registers giving you a total of 36 buffers to bookmark your copied information and remember them as long as you don’t use the same register once again. This extends the regular yy/cc/dd/p/P… combinations that we use to yank/paste the default register.

numbered registers: "0 to "9 (total 10)
named registers: "a to "z OR "A to "Z (total 26)

Usage (applies to all the shortcuts yy, cc, dd, p, P):

  1. to copy 10 lines from the current line into the named register a, press "a10yy in vim command mode. Same for numbered registers, just replace a with the number of choice, for example "710yy.
  2. to paste the buffered line(s), paste "ap or "7P etc. again in the command mode.

Wait! There are some special registers too:

"" : the last defined register
": : the last entered command
"% : name of the file currently opened
"* : copy to system clipboard (X)
"+ : similar to "*

To see all the currently set registers, use :reg. To know more about registers type in

:help registers