Vim Tips

This post evolves each time I discover an exciting feature of Vim.

Pasting The Output of a Shell Command

Recently, I wondered if it was possible to display in a buffer a specific revision of a file taken from the Git repository of my project without leaving Vim. In my case, the file was removed in the latest branches on the project and I had to backport some logic that was in the original file.

I ended up on the read command and more precisely to the following form which is documented at :h read!:

:[range]r[ead] [++opt] !{cmd}

So if I want to fill an empty buffer with the file of a specified commit/branch:

:r !git show old_branch:src/build.sh

Insert The Current Date/Time

We can use the Vim internal strftime() function with the put command (see :h strftime() and :h put). Be careful, your system needs to have this function available.

So to insert the current date in the default format:

:pu=strftime('%c')

Update Plugin Help Tags

Sometimes it can occur that some plugin's help is not available. If the plugins you use are installed via the Vim 8's plugins manager, you can simply use the following command to regenerate all the help tags of all the installed plugins

:helptags ALL

More info there: :h helptags

Clearing the Search Pattern

When search highlighting is enabled, you might want to clear the highlighted patterns. One solution could be to disable the highlighting with the command :noh. This can be cumbersome because you will need to enable it again on your next search.

A better way is probably to clear the last search pattern register as shown below:

:let @/=""

More info there: :h noh and :h registers

My Vim configure options

My options for the vim build I use everyday (here for MacOS, to be adapted for other environments):

% ./configure --prefix="$HOME/.local" \
              --enable-multibyte \
              --with-tlib=ncurses \
              --with-compiledby=stac47 \
              --enable-cscope \
              --enable-terminal \
              --disable-perlinterp \
              --disable-rubyinterp \
              --disable-python3interp \
              --disable-gui \
              --without-x \
              --disable-luainterp \
              --disable-gtktest \
              --disable-netbeans \
              --enable-fail-if-missing

Opening Files From A Generated Files List

Sometime, we may want to start vim and open the set of files you were working on, let's say a subset of the files in the previous git commit.

That's sound easy, but there's a tiny trick to be aware of.

If you run the following command:

% git diff --name-only HEAD~1 | grep '.rb$' | xargs vim --

You will see the following warning (and maybe, it will break your terminal display and you will have to use reset or stty sane to fix it).

Vim: Warning: Input is not from a terminal

The reason is that vim expects to be started from a terminal: here vim is a child process created by xargs. But there is an option with the xargs command as described in the manpage:

-o Reopen stdin as /dev/tty in the child process before executing the command. This is useful if you want xargs to run an interactive application.

So the right command is:

% git diff --name-only HEAD~1 | grep '.rb$' | xargs -o vim --

Emacs 29.4 (Org mode 9.6.15)

Validate