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.useSytemClipboard 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

Advertisements

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

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

VisIncr

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: https://github.com/vim-scripts/VisIncr

  • 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.