Adventure through my vimrc (nvimrc)
In this post I’m going to take you in an adventure in my previously vimrc
and currently init.vim which I collected from using vim then neovim along the last few years. I will explain the feature or the function, then include the configurations or code that should be inserted in the vimrc
(init.vim
) file to activate that feature.
This might not always be up to date, for I keep changing these configurations when I feel like it.
Contents:
- General Configurations (neovim)
- General Configurations (vim)
- Plugins Manager
- Mappings
- Status Line
- Useful Functions
General Configurations (neovim)
The very first section is for the general settings which I think that they should be set up in vim by default, as some of them already are default in neovim, which include:
- Use incremental search: this feature enables vim from highlighting the search pattern while it is being typed.
- Use fast terminal connection, this feature improves smoothness of redrawing when there are multiple windows.
- When a file has been detected to have been changed outside of vim and it has not been changed inside of vim, automatically read it again.
- Activate the wild menu for command line completion using the
TAB
key.
- Highlight all matches for the search pattern.
- Vim uses history to remember command line commands that a user enters in either on of these command-lines:
:
commands- search strings
- expressions
- input lines, typed for the
input()
function. - debug mode commands I like to make vim remember a lot (1000) of my command history.
- Make vim behave in a more useful way (this literally exactly what is written in vim docs). It is optional to include this in one’s vimrc, for when a vimrc or gvimrc file is found while Vim is starting up, this option is enabled.
- Make backspace erase character as other editors do.
- When a tab character is inserted using pressing the
key, backspace is used to delete what was inserted, it depends on other configurations parameters `shiftwidth`. `tabstop` and `softtabstop`.
- When starting a new line, use the same indentation from previous line. This is a very useful feature when one is coding, especially in an indent sensitive language such as python or yaml.
- Live substitution (neovim only), when replacing a pattern.
General Configurations (vim)
Another set of general configurations which are not included in neovim by default. I still find them very useful.
- I don’t like to use the mouse while I’m editing, still I like to enable it.
- Show (partial) command in the last line of the screen. The docs explain this very well so I copied that from there:
In Visual mode the size of the selected area is shown:
- When selecting characters within a line, the number of characters. If the number of bytes is different it is also displayed: “2-6” means two characters and six bytes.
- When selecting more than one line, the number of lines.
- When selecting a block, the size in screen characters: {lines}x{columns}.
- Show the matching of a bracket when inserted, this looks like as if the cursor moved to the matching one for a small period of time to highlight it.
matchtime
is to control that time period.
- Use case insensitive when searching for a match.
- If the search pattern contains upper case characters, override the
ignorecase
option and use case sensitive search.
- Automatically save before executing commands like
:next
and:make
. I do use make a lot, so this is very efficient.
- Hide buffers when they are abandoned, if they do not show in other windows of course. This is useful when I deal with many buffers at a time, and I don’t have to worry about saving before switching.
- I find modeline very useful, when I get other’s code, or when I want to give others my code, I set specific settings inside the modeline to be set automatically for any other user who usees vim, the most important ones are
tabstop
andexpandtab
.
- I like to have a minimal number of screen lines to keep above and below the cursor.
- Show the mode that I am using as a message on the last line, weather it is
insert
,visual
orreplace
mode.
- Wrap text and show long lines on multiple lines without inserting line breaks.
- Set the title of the window to the value of ‘titlestring’, and if empty to: filename [+==] path - vim-server-name.
- Show line number on the left side of the document on the active line, and activate relative numbers on the rest of the lines.
- set the path to the shell to use ‘bash’
- Ignore temporary and complied files for c++, Java and Python as well as the configurations folders for the version control systems that I might use.
- When joining lines, don’t insert two spaces after punctuation.
- Don’t redraw while executing macros, this is a good performance configuration.
- Turn backup off. Since most stuff are within version control, I feel that these backup and swap files are just annoying so I disable them all.
- Set default 1 tab == 4 spaces
- Create undo file to keep history after closing the file.
- Remember info about open buffers on close.
- Sometimes I lose track of the line that I am on, so enabling
cursorline
make it much much easier.
- Enable Omni completion.
- Disable the visual bell.
- Set number of colours to be used to 256, as I like it so.
- Clearing uses the current background colour.
- Set vertical Cursor in insert mode.
Plugins Manager
For plugins, I tried many plugins managers, I used to use vundle, and the set it up in my vimrc should have been looking like:
Then I switched to another plugin manager called vim-plug which is faster and do asynchronous updating for the plugins. The setup is also simpler, and works fine with both vim/neovim:
I am not going to discuss my favorite plugins and their configurations here, as it differs from time to time, and depends on the use of vim and the working environment. I also moved all the plugins related stuff into another file plugs.vim
, so I don’t get so distracted with them and their settings.
Mappings
- Neovim terminal mode mappings, to act on terminal buffer similar to a normal vim buffer.
- Many times I want to stay in visual mode when I do a shifting for a block of code, so I remapped the original keys to do the trick.
- Move a line of text up and down using
leader + [jk]
in both normal and visual mode.
- Switch between characters next to each other using
leader + [hl]
for I use it a lot to fix some hastly typos in normal mode.
- Disable highlighted matches from previous search when
leader + ENTER
is pressed.
- I use windows quite often, so this is a smart way to move between windows, using the
ctrl
with combination of the original vim movement keys[hjkl]
.
- Open a new tab with the current buffer’s path. Super useful when editing files in the same directory
- Switch
cwd
to the directory of the open buffer. Coupled with the previous mapping, I get a very pleasant behaviour.
- Open vimrc file in a new tabe mapping.
- Toggle spell checking on and off, when I am writing a paper.
Status Line
I have a very nice status line, which I am proud of, and prefer it over status line fancy plugins. It contains some helper functions and plugins status, so if you don’t use those plugins, just comment the lines for those, Fugitive and Syntastic.
I like to change the color of the statusline to blue when I go insert mode, and this is done by the following lines:
Useful Functions
- Delete trailing white space on save, for all filetypes except markdown, for sometimes, I need to leave two spaces at the end of the line to make a soft line break.
- Convenient command to see the difference between the current buffer and the file it was loaded from, thus the changes you made.
- Return to last edit position when opening files (You want this!)
- Visual mode pressing
*
or#
searches for the current selection.
- Java compile using javac and run using java applied on the current buffer. Useful for small java programs to run on the fly.
- In my status line, I use some functions. One is to get the state of the current buffer, weather it uses tabs or spaces.
- This is to checks if paste mode is enabled, I like to see that in my statusline as well.
- Don’t close window, when deleting a buffer.
- As everyone has his own standards for using either tabs or spaces, this function allow toggling between tabs and spaces, so ease the hussle.
- Show red highlighting on ColumnColor 80.
- Many times when I search for a regex, I wanted to copy all the existing matches and paste them somewhere else, that’s why I use this function.
:CopyMatches
to copy all matches to the clipboard.:CopyMatches x
wherex
is any register to hold the result.- paste from register x with
"xp
or"xP
.
- Append modeline after last line in buffer.
- Realign buffers when iterm goes fullscreen
- Highlight all instances of word under cursor, when idle. Useful when studying strange source code. Type
z/
to toggle highlighting on/off.
All information for vim commands can be found using the help system in vim by typing :help command
or look at the online docs for they are very plain and clear.
The complete set of my vimrc is on my github. There I use one repo for all my dotfiles, bash and tmux configutaions.
Feel free to suggest improvements.