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


How to grep in Vim

When you are editing files in Vim, you might feel like finding out which other files have a certain text. Vim has such grep features built into it to support this usage. There are many popular Vim plugins that offer similar features, but you might want to first examine what Vim grep offers. In my opinion, most users should find the grep features built inside Vim to be quite adequate for their use.

Right off the bat, you need to know that there are two types of grep that are available in Vim: internal grep and external grep.

And you should remember that all grep operations operate from the present working directory (PWD) of Vim. You can check what is the PWD using the command :pwd. So, it is to your advantage to invoke Vim from the base directory of a code base.

Internal grep

This is the grep feature that is implemented inside Vim. Not surprisingly, this internal grep is far easier to use compared to Unix grep. However, it is also noticeably slower compared to Unix grep, for obvious reasons. However, the ease of use of internal grep is so high that I tend to prefer it over external grep.

The command to invoke internal grep is :vimgrep. But, you can use the short form :vim. Yeah this is the one command that won the Vim lottery to have the same name as the editor! 😄

This command in its most common form takes two arguments: the pattern to search for and which files to search for.

  • To search for Xanadu in all the files in the PWD: :vim Xanadu * This shows all the search results in a Quickfix window at the bottom and Vim jumps to the first match right away. You can navigate through this window and press Enter on search results to jump to them.

  • To recurse the directories or files in Vim, you have this strange syntax called star-star. For example, to search recursively in all files and directories in PWD: :vim Xanadu **

  • Once you are comfortable with the star-star syntax, you will find it very convenient to use: :vim Xanadu ../include/**/*.h

  • One trick I like to use a lot is to search for the current word under the cursor in the files. I first press * so that the word is selected. Then I use this empty pattern to search for it: :vim // *.cpp

External grep

External grep is nothing but Vim invoking the commandline grep in a shell child process. This is super dumb: Vim just invokes whatever you pass it verbatim and grabs the grep output, parses it and fills it in the Quickfix window.

  • To search in all files in the PWD: :grep Xanadu *
  • To search recursively in all files and directory under the PWD: grep -R Xanadu * Note how the recursive search is far easier to invoke in the internal grep.

Quickfix window trick to browse code

No matter whether you use internal or external grep, its results are filled in the Quickfix window. As you do new searches, new Quickfix windows are created and the latest one is displayed at the bottom. You can move through this list of Quickfix windows using the :colder and :cnewer commands.

With these grep features and Quickfix window history Vim can be used as an rudimentary IDE to examine a code base: use the grep commands to search for text (variables, methods or classes) and jump to them. And you can move back and forth in this investigation using the Quickfix window commands described above.

Tried with: Vim 7.4 and Kubuntu 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

VisIncr plugin for Vim


One of the most powerful aspects of using Vim is that you never have to do any drudge work. All boring repeatable tasks can be executed with a meaningful command. A recent problem I ran into was to create variable names with a large number of incrementally increasing indices. I found an awesome plugin by Dr. Chip called VisIncr that automates this very useful operation.

  • Install: Use your favorite Vim plugin installation method to install this plugin from this URL:

  • Usage: Let us assume I want to generate variables ranging from x_3 to x_16. I first type x_3 and repeat it 13 times one on each line. Mark the column containing 3 using visual block (Ctrl+V). Then press I and press Enter. Boom! See the output in above image. The command I is easy to remember cause the operation is i-ncrement.

  • You can refer to the documentation for the commands to increment with right justification (default is left), by a different increment or want to increment octal number, Roman numerals or dates. Pretty much any scenario that appears in code is covered by the plugin.

Tried with: Vim 7.4 and Ubuntu 16.04

Sensible plugin for Vim

Sensible is a super simple plugin for Vim by Tim Pope. It is nothing but a set of sensible settings for your vimrc. It is a great way to start off on a new system where you do not have access to your fully featured vimrc. I only wish he explained some of the choices he made for the options in this file.

If I have a local account on the remote or new computer, I find it far easier to just replicate my vimrc, and other dot files, using GNU Stow.

How to autocomplete path in Vim insert mode

Vim path autocomplete in insert mode
Vim path autocomplete in insert mode

It is useful to be able to autocomplete a path to a directory or filename while typing code or script files. This helps in ensuring that there are no mistakes and the path is correct. Thankfully, Vim has an autocompletion feature for typing path in insert mode! It calls this feature as file name completion.

  • Ctrl-x Ctrl-f: This is the shortcut to autocomplete path in insert mode. Start typing a path (absolute or relative) and use this keyboard shortcut. You will see a dropdown menu filled with the next entries from the filesystem that can be used to fill the path.

  • Ctrl-n or Ctrl-p: Shortcuts to move up and down the entries in the autocomplete dropdown menu. If you move off the top or bottom of the dropdown menu, you get the empty entry.

  • Absolute path: Any path that you begin typing with a slash is autocompleted as an absolute path.

  • Relative path: This is any path that does not begin with a slash. They are autocompleted too. Note that they will use the directory that Vim was launched from (PWD) as the root.

  • Hidden files or directories: I found that this was autocompleted only if I typed the first period and then used Ctrl-x Ctrl-f.

Tried with: Vim 7.4 and Ubuntu 14.04

Visual Studio Code extensions that I use

  • CPP Tools: The official extension for working with C++ code. Automatically indexes all code in the currently open directory, offers auto-completion and syntax highlighting.

  • Python by Don Jayamanne: There are many Python extensions, but this seems to be the most popular one. Syntax highlighting, indexing and code completion.

  • Vim: There are many Vim extensions, but this seems to be the most popular one. It has entire universes to traverse before it can be as good as Vrapper, the Vim extension for Eclipse. This VSCode extension offers very basic navigation and editing commands.

  • Git Blame: This extension does one little thing that I need everyday to work with code from other people: know who modified a line of code. This extension shows that for the current line in the status bar.

  • Matlab: I need to regularly browse through some MATLAB files. This extension offers syntax highlighting of Matlab files.

Tried with: Visual Studio Code 1.4 and Ubuntu 16.04

How to use xclip

xclip is a very useful tool to copy and paste text content between the shell and GUI (X11) programs. You can pipe text to xclip and that text will be copied to the X clipboard. You can invoke xclip to output and it will spit out the content in the X clipboard. Once you learn it, you will wonder how you ever worked without it at the shell, inside Vim and across SSH sessions.

  • Installing it is easy:
$ sudo apt install xclip
  • To copy output from shell to X11 clipboard:
$ pwd | xclip
  • To paste from X11 clipboard to shell:
$ xclip -o | ls
  • Note that xclip works with the X clipboard, not the clipboard maintained by your window manager. Yes, you have two clipboards in Linux! So, if this copy-paste is not working for you, then the selection copied to your X clipboard might not be appearing in your window manager clipboard. Typically, the window manager clipboard is configured by default for all of this to work transparently. If it is not working, then you can configure the window manager clipboard to get all the selections from X clipboard.

  • To copy from Vim to clipboard: Visually highlight the text or lines you want to copy and type :w !xclip. The :w command is used to write the entire contents of the buffer or the visually highlighted text to a specified output file. Instead of a file, we write that text to xclip here, so it will be copied to the clipboard.

  • To copy from clipboard to Vim: Just do the reverse of the above operation. Type this command :r !xclip -o. The :r command in Vim is used to read text into the current buffer. So, this command takes the output of xclip and inserts it into the currently buffer.

  • Copy or paste in SSH session: The above commands works without any change between your local computer and the remote computer you have SSHed into! Yes, works like magic all thanks to the X11 protocol! Note that the remote computer should have xclip installed and you should have created the SSH session with X11 forwarding.

Tried with: XClip 0.12 and Ubuntu 14.04

How to wrap text for printing in Vim


Vim can be used to print a text file using your printer. The font and font size used for printing can be configured in Vim. Depending on the font and font size used you may find that the width of the paper is not enough to accommodate the width of the text. Parts of the lines longer than the paper width are simply not printed!


Vim can be requested to wrap the text using :set wrap command. However, this is only for display and the text width used for wrapping is the width of the terminal or window that Vim is using. This has no effect on printing.

What we need to do is to set a text width and reformat the text by enforcing this text width. Note that you will need to check how many columns of text width will fit into the printed page for your font, font size and paper size.

  • First, we set the text width. Let me assume that 80 columns works for my setup:
:set textwidth=80
  • Next, we ask Vim to reformat the entire text file by enforcing this text width:

The breakdown of this command is: gg moves cursor to first line of file. gq is the command to reformat all text from current position to the position specified after this. G is the command to move to last line of file. When applied after gq, this reformats the entire file from first line to last line, enforcing the text width we specified earlier.

The formatted file can now be printed. After printing, you can undo this reformatting if it is not needed.

For more details about these commands:

:help textwidth
:help gq
:help wrap

Tried with: Vim 7.4 and Ubuntu 14.04