Almost all applications have the ability to display Most Recently Used (MRU) documents.
Vim editor by default does not have this feature. But using MRU.vim plugin, you can use the most recently used (opened and edited) files inside Vim editor as explained below.
This article is part of the on-going Vi / Vim Tips and Tricks series.
This article explains how to install the MRU.vim plugin in 2 easy steps. This also explains about 7 powerful feature of the Most Recently Used Plugin. This will definitely change the way how you use Vim editor.
2 Steps to Install MRU.vim
Step 1: Download the latest version of the plugin from vim.org
Download the plugin from vim.org
$ cd /usr/src $ wget -O mru.vim http://www.vim.org/scripts/download_script.php?src_id=9274
Step 2: Install the MRU vim plugin
$ mkdir ~/.vim # if the directory does not exist already $ cd ~/.vim/plugin/ $ cp /usr/src/mru.vim .
7 Powerful Features of MRU.vim
After installing this plugin, whenever you open a file for reading or writing it will be added to the MRU list.
1. View the Most Recently Used Files in Vi / Vim Editor
:MRU Note: :MRU (upper-case) works. :mru (lower-case) doesn't work.
:MRU will show “most recently used” files ordered by the recent usage from top to bottom as shown below. To open a file press ENTER while the cursor in that file name.
Note: Immediately after installing the MRU plugin, you may not see any files in the MRU list. Use Vim editor to open few files after installing the plugin, for the MRU list to grow.
2. How to Increase MRU List Count? (Increase the Number of Files Stored)
By default MRU list stores only the last 10 used files. Add the following line to your ~/.vimrc to increase this limit as shown below.
$ cat ~/.vimrc let MRU_Max_Entries = 20
3. How to Search Files Inside the MRU List
When you increased the number of files, it may be difficult to scroll through the MRU list to pick the file you are looking for. Instead, you can use the normal Vim commands to search inside the MRU list. So you can give (slash)/ and give a pattern to search.
In the following example, the MRU list was very long. I did a “/yum”, which moved the cursor automatically to /etc/yum.conf MRU entry. Pressing enter, will open this file.
You can also use the MRU search functionality as explained below.
- Enter the partial file name and press TAB which will fill the file name, which you can open by pressing enter,
- Enter one or two characters of the file name or the path name, and press tabs to rotate around the file names.
4. Opening Files from MRU List in a New Tab
Pressing enter on an MRU entry will open the file in the same window by default. It will open in new window if the file opened in the current window is not yet saved.
- To open the file from the MRU entry in a new Tab , press ‘t’ when the cursor is on a file name in MRU window.
- :tabn will take you to the next tab
- :tabp will take you to the previous tab
5. Update the MRU Window
To update the current MRU window, press ‘u’. When ever opening the MRU window it will open the newer content always.
6. Excluding Certain Files Using MRU_Exclude_Files
To exclude certain files from being shown in the MRU, you can store the regex in the MRU_Exclude_Files variable as:
$ cat ~/.vimrc let MRU_Exclude_Files = '^/tmp/.*\|^/var/tmp/.*' " For Unix
The above will exclude the /tmp and /var/tmp from being stored in MRU.
7. Including Certain Files Using MRU_Include_Files
To include only certain kind of files in MRU, you can store the regex in the MRU_Include_Files variable as shown below. This will include only the files which are in /home/ramesh to be stored in MRU.
$ cat ~/.vimrc let MRU_Include_Files = '^/home/ramesh/.*'
Get the Linux Sysadmin Course Now!