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

Updated post here: https://codeyarns.github.io/tech/2018-08-31-how-to-use-vimdiff.html

How to use clipboard for Vim in VSCode

Updated post here: https://codeyarns.github.io/tech/2018-04-26-how-to-use-clipboard-for-vim-in-vscode.html

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: https://github.com/mhinz/vim-signify

  • 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()
endfunction

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('%')
endfunction

Tried with: Vim 7.4 and Ubuntu 16.04

Cursor and Del keys not working in Vim

Problem

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.

Solution

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

How to grep in Vim

Updated post here: https://codeyarns.github.io/tech/2017-09-14-how-to-grep-in-vim.html

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.

  • Install it from this URL: https://github.com/vim-scripts/AdvancedSorters

  • To sort the words in a line, I just visually block the line and press : (colon) and then type the command SortWORDs

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