How to use Vim as plain text editor

Vim is not only useful for code, but also as a general plain text editor. This is useful for writing README files, Markdown files or if you like publishing plain text file content.

In plain text files, a desirable property is to have lines wrapped at a certain width. The general wrap feature of Vim is meaningful only for code which has long lines. This option just wraps the long line visually to the screen width.

To use Vim as a plain text editor you want it to automatically breaks lines at a certain width as you type. You want it to do that only at word boundaries. The option that enables this mode is textwidth. For source code, this feature is disabled and is set to 0.

To enable Vim to set a maximum width for text that is inserted, set this option: :set textwidth=80

If you have existing text that was not set to this width or you have shortened some lines all of that can be fixed too: mark them visually and press gq.

A final feature needed from a plain text editor is to be able to center a line. This is useful for example for titles and section headings. To do that, mark that line visually and type :center. This only works if the textwidth option is set.

Tried with: Vim 8.0 and Ubuntu 18.04

How to use vimdiff

Vim can be used as a diff and merge tool. While it cannot match the beauty and power of GUI programs like Meld, it is immensely useful to diff and merge over a SSH session.

  • Vim can be invoked in diff mode either using vim -d or as vimdiff. Just pass in the names of the files to compare:
$ vimdiff 1.cpp 2.cpp
$ vim -d 1.cpp 2.cpp 3.cpp
  • Vim in diff mode displays each file in its own window side-by-side showing the diff sections in colors. You can switch between the windows using the normal Vim commands. (I use Ctrl-W-W.) And when you scroll down all the windows scroll down with you because they are locked to each other.

  • To switch to and fro between diffs use the ]-c and [-c commands.

  • To put the diff the cursor is resting on to the next window use the command dp

  • To pull the diff from the next window use the command do

Tried with: Vim 7.4 and Ubuntu 16.04

How to use clipboard for Vim in VSCode

Visual Studio Code has a decent Vim extension. I use it by default and sometimes wish I could yank to and paste from the system clipboard. Thankfully, this feature is available in the Vim extension, but turned off by default.

To turn on this feature, set the vim.useSystemClipboard setting to true. You may need to restart VSCode for it to take effect.

Note that you can yank text to the clipboard of your OS and also paste from it into VSCode using Vim.

Tried with: Vim extension for Visual Studio Code 0.11.5 and Visual Studio Code 1.22.2

Signify plugin for Vim

If you use version control systems like Git or Mercurial and you use Vim to edit your source files, then you might find the Signify plugin very useful. When you edit a version controlled file, Signify shows which lines are changed using signs (or markers) in the gutter on the left side.

  • Signify can be installed using your favorite plugin manager from:

  • By default, Signify works with a large number of version controls systems. If you only use one or a few of those, then you can speed up Signify a bit by informing it to only check those VCS. For example, I only use Git and Mercurial, so I add this line to my vimrc:

let g:signify_vcs_list = [ 'git', 'hg' ]
  • You can also override the default marker characters it uses for its signs. For example, to change its delete character:
let g:signify_sign_delete = "-"

Tried with: Vim 7.4 and Ubuntu 16.04

How to add NoScrollBar to LightLine

LightLine is a lightweight statusline plugin for Vim. By default, it shows the location of current line relative to the entire file as a percent at the left of the statusbar.

However, I like to the NoScrollBar plugin to get a general sense of the current line in the file. So, I replaced the percent display with the output of NoScrollBar using a tiny function.

I added these lines in Vim to achieve this:

" Replace percent component of Lightline statusline
let g:lightline = {
      \ 'component_function': {
      \   'percent': 'NoScrollbarForLightline'
      \ }
      \ }

" Instead of % show NoScrollbar horizontal scrollbar
function! NoScrollbarForLightline()
    return noscrollbar#statusline()

Tried with: Vim 7.4 and Ubuntu 16.04

How to show full file path in LightLine

LightLine is a lightweight statusline plugin for Vim. By default, it shows only the filename on the left side of the statusbar. I like to have the full file path displayed instead. It turns out that is easy to do.

The statusline has many components, and the component that shows the filename is called filename. We override this in LightLine with a tiny function that uses the expand function in Vim to get the full file path.

I added these lines in Vim to achieve this:

" Replace filename component of Lightline statusline
let g:lightline = {
      \ 'component_function': {
      \   'filename': 'FilenameForLightline'
      \ }
      \ }

" Show full path of filename
function! FilenameForLightline()
    return expand('%')

Tried with: Vim 7.4 and Ubuntu 16.04

Cursor and Del keys not working in Vim


After changing some configurations in a vimrc, I noticed that the keys that move the cursor in Insert mode of Vim did not work. Even the Del key did nothing but move the cursor.


After examining the configuration I had added to my vimrc, I found that this behavior was caused by setting set noesckeys. Turns out that this setting disables cursor keys in Insert mode. I did not really need that feature, so I took out that line. This leaves the default set esckeys setting which works as expected.

Tried with: Vim 7.4 and Ubuntu 16.04

AdvancedSorters plugin for Vim

Vim has an inbuilt :sort function that can be used to sort lines. However, I recently had to sort words in a line. I could have broken the line and put each word on one line, sort them and put the results back as a line. Thankfully, I found the AdvancedSorters plugin for Vim that does this job easily in one command.

Tried with: Vim 7.4 and Ubuntu 16.04

How to use Vim with Cscope

Cscope provides a rudimentary commandline interface for exploring your C or C++ codebase. But using it along with Vim provides a much richer interface.

  • Make sure you have installed Cscope and know how to use it. This is explained here.

  • To be able to use Cscope inside Vim, install the cscope_maps.vim plugin file. This old file can be found mirrored at this Github repository here.

  • Open Vim in the root of the code directory that has the generated Cscope cross-reference file cscope.out.

  • Open any source file and move cursor to any symbol and use the following commands: Ctrl-\ s to jump to any instance of the symbol, Ctrl-\ f to jump the file under the cursor, Ctrl-\ g to jump to the definition and Ctrl-\ c to jump to calls of the function under cursor.

  • Use the Vim command Ctrl-o to jump back to previous locations in files.

  • If you are using a cross-reference file with a filename that is not cscope.out or it is not in the current directory, then export that file path in the CSCOPE_DB environment variable. Vim will use that path to read the cross-reference file.

Tried with: Cscope 15.8b, Vim 7.4 and Ubuntu 16.04