1. Overview
The Vim (Vi IMproved) editor is a command-line text editor that is widely used for programming, scripting, and other text-related tasks. It’s an extension of the original Vi editor that addresses the limitations of Vi and provides a more feature-rich version.
In this tutorial, we’re going to learn how to configure Vim to remember the last cursor position when reopening files.
2. Problem Description
By default, the Vim editor starts the cursor at the first line of a file, requiring us to manually move the cursor to the position we want.
However, after making some edits, we’re always taken to the first line upon reopening the file. This can be frustrating, especially if the file content is quite lengthy or we’re working with a lot of files.
There are several ways to configure Vim to take us to the position of our last edit. Let’s go over them.
3. Configuring the ~/.vimrc File
The first method we’ll discuss is configuring the ~/.vimrc file. We use this file to store settings for the Vim text editor that are loaded when we open Vim.
We can always edit the existing file or simply create it if it doesn’t exist:
$ touch ~/.vimrc
This will create an empty ~/.vimrc file, which we can now edit:
" Enable line number
set number
" Enable syntax highlighting
syntax enable
" Remember cursor position
if has("autocmd")
au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | execute "normal! g`\"" | endif
endif
Let’s break down what this does:
- enable line numbers using set number
- enable syntax highlighting for different file types using syntax enable
- check if our current Vim configuration supports the autocmd feature using the if has(“autocmd”) statement
- if it does, define an autocommand, au, that is triggered after reading a BufReadPost event
- use a nested if block to check if the value stored in the last position register is greater than 1 and less than or equal to the last line number of the file
- if it meets this condition, execute a normal mode command, execute “normal! g`\””, which positions the cursor at the line and column it was on when we last closed the file
- close the if block with the endif statement
We save this in our ~/.vimrc file and restart all Vim sessions. We can now resume work from our last edit position.
4. Using a Plugin
We can also achieve the same result by installing a plugin for our Vim editor. We can do this manually by cloning the repository, or we can use a plugin manager like vim-plug.
4.1. Installing the Plugin Manually
We can install a plugin manually by cloning the required plugin into our system. Here, we want to use the vim-lastplace plugin, which will enable our Vim editor to remember our last edit position:
$ mkdir -p ~/.vim/pack/plugins/start
$ rm -rf ~/.vim/pack/plugins/start/vim-lastplace
$ git clone --depth=1 https://github.com/farmergreg/vim-lastplace.git ~/.vim/pack/plugins/start/vim-lastplace
This will clone and install the vim-lastplace plugin directly, with no additional tools. This method works for Vim 8 or newer versions.
4.2. Using vim-plug
We can also install the plugin by using the vim-plug plugin manager. This will require us to configure the ~/.vimrc file to download and install the plugin manager:
if empty(glob('~/.vim/autoload/plug.vim'))
silent !curl -fLo ~/.vim/autoload/plug.vim --create-dirs
\ https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
autocmd VimEnter * PlugInstall
endif
This code block runs only if we don’t have vim-plug on our system. After this is done, we can now add the configuration to install our required plugin:
call plug#begin('~/.vim/plugged')
Plug 'farmergreg/vim-lastplace'
call plug#end()
We save and exit the file. Finally, we open Vim and run the command that installs the vim-lastplace plugin:
:PlugInstall
After installation, we can now reopen files with the cursor at our last edit position. Using a plugin manager like this helps simplify installing, updating, and removing plugins.
5. Using Sessions
We can also manually create and load a session that saves and restores the editing state, including cursor positions.
To save a session:
:mksession Session.vim
To load and restore this session:
:source Session.vim
This can be useful in certain scenarios, such as when we need to restore more than just the cursor position.
6. Conclusion
In this article, we’ve looked at the various ways we can make Vim remember our last edit position when we reopen files. We saw that we could achieve this by configuring the ~/.vimrc file. We also explored the use of a plugin manager to install a plugin that can achieve the same purpose. Lastly, we considered manually creating and loading sessions, which can restore editing states, including cursor positions.