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.
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.
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.
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.
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
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.
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:
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.
Vim can be started just for the purpose of executing one or more Vim commands. This can be done using the -c commandline option to Vim. When one or more of these is provided, Vim is started, then executes these commands first and then renders the edit window.
For example, to open Vim with its internal help documentation:
$ vim -c help
Note how the internal help command is :help and we can drop the colon when using the command option.
To open a file named foo in Vim using this command option:
$ vim -c "e foo"
To open two files named foo and bar in Vim using this command option:
$ vim -c "e foo" -c "e bar"
The above examples are convoluted, since files can be opened directly without using this command option.
Here is a problem that I could solve only using this command option: I update the Vim text spellfile manually and I want to compile the binary spellfile from the shell or from a script. Easy to achieve by calling the command to compile spellfile mkspell and then quit q:
$ vim -c "mkspell! ~/.vim/spell/en.utf-8.add" -c q
Vim can be used to perform a spellcheck of your document, as described here. Vim can maintain a spellfile for you where you can add your good words that are not in the dictionary used by Vim for spellcheck.
This spellfile by default is stored in the directory ~/.vim/spell and its filename is of the format lang.encoding.add. lang is what you set in the spelllang variable. encoding is what you set in the encoding variable. For example, on my system this file is named en.utf-8.add.
The spellfile described above is in plaintext format and has one word per line. You can back it up to version control, copy it to different computers and edit it yourself to add or remove words.
However, to actually use the spellfile for spellcheck, Vim needs to convert this to an optimal lookup data structure and store it as a binary file in the same directory with a .spl suffix. For example, on my system this is the file: ~/.vim/spell/en.utf-8.add.spl. The extremely confusing fact is that Vim refers to both types of these files as spellfile in its documentation!
Vim automatically builds the binary spellfile when you add a word to the dictionary. However, if you had copied the text spellfile from another computer, you may need to manually generate the binary spellfile once.
To make the .spl binary spellfile from the .add text spellfile, use the mkspell command:
Vim ships with the syntax highlighting rules for C and C++. This is a curse in disguise, because those rules are extremely old and they only highlight the keywords. This means that any C or C++ source file looks pretty much like plain text, no matter which color scheme you apply.
Vim Syntax Extra is a plugin that fixes this by adding more syntax rules for C and C++. There is a visible difference only in the syntax highlighting once this plugin is installed. Add its Github URL using any of your favorite Vim plugin manager and restart Vim to enjoy it 🙂
Being able to view relative line numbers in Vim makes it very easy to move around and operate in the editor. This is because a lot of Vim commands involve line counts that are relative to the current line.
If you are using Vrapper in Eclipse, you might be missing having relative line numbers. It is possible to get them by installing the Relative Number Ruler plugin.
If you do not see any plugins show up, uncheck the “Group items by category” option at the bottom of the dialog.
You should be able to choose the plugin and install it from there.
Right-click on the line number ruler, this is the editor column where the line numbers are displayed. From here, you can enable or disable both line numbers and relative line numbers.
If you would like to have the hybrid mode, where the current line shows the absolute line number and the rest show relative line numbers, that is supported too. Go to Window -> Preferences -> Relative Number Ruler and enable this feature.
Tried with: Relative Number Ruler 1.1, Eclipse 4.4.2 and Ubuntu 14.04