How to Check if a Plugin Installed in Vim

Learning Machine

Explaining Every Line of vim-sensible

Because not all of Sensible Vim might be sensible for you

Rionaldi Chandraseta

Photo by Teslariu Mihai on Unsplash

There are 3 possible reasons why you clicked on this story. First, it pops up in your screen as a recommended story. You are not using Vim, but you are curious about Vim and Sensible Vim.

Alternatively, you have been obsessed with Vim for years and you thought "I already know all the lines inside it, but it's an article about Vim so I will read it anyway."

Finally, you are in the same stage of learning Vim as me and you have searched about an annotated version of Sensible but haven't found one.

First, I will give a quick explanation about vimrc file. Vimrc file is a configuration file for Vim that enables you to customise Vim, install plugins, and so on. It is usually found in ~/.vimrc, but you can create it if it's not there. For Windows, you can find more information about finding the vimrc file here.

I was searching for a vimrc file that is filled with useful defaults to act as a base for my future vimrc. First, I want to share a recurring answer I found on multiple posts and discussions. Quoting from this post by Doug Black,

Don't put any lines in your vimrc that you don't understand.

I believe that is a really sound advice. A programmer wouldn't put any extra or unnecessary lines inside their code, this should be the case too with vimrc files.

Along my search, I stumbled upon this Reddit post stating that Sensible is a good starting point for a vimrc file.

Compared to other alternatives I got, which was either quite similar with Sensible or over 300 lines, I decided to dive deeper into this as a starting point.

Here's the full code for Sensible Vim.

Code for vim-sensible by tpope

Some of the hardest-to-understand lines are commented, but most of them don't have any explanations at all. It's understandable though, for those more familiar with Vim, it would not make sense to put comments on these lines.

Maybe it will look like this for them.

            if speed > 80.0:        # checks if speed is more than 80.0
speeding = True # set speeding flag to True
n_violation += 1 # add 1 to variable n_violation

Anyway, I'm not a Vim expert, so I wanted the over-annotated version for me to better understand what these lines actually do.

Here's what I found out, feel free to skip through some of the trivial explanations.

Line-by-Line Explanation

            if exists('g:loaded_sensible') || &compatible
finish
else
let g:loaded_sensible = 'yes'
endif

The very first bit is to set a vim variable of loaded_sensibleto 'yes', presumably for compatibility with other plugins that check if vim-sensible is used.

            if has('autocmd')
filetype plugin indent on
endif

The line will check if autocmd is enabled. Most Vim will come with it, but some distributions such as vim-small and vim-tiny will come without autocmd by default.

In this case, checking for autocmd is needed because the filetype detection system needs it to work properly.

filetype plugin indent on is a short form of these commands.

            filetype on
filetype plugin on
filetype indent on

The first command turns on filetype detection for Vim to help set syntax highlighting and other options. The plugin part will load plugins for specific filetype if they exist. The last bit will load indent file for specific filetype if they exist too.

For example, if you want to activate certain plugins for only Python language, then you can create a file ~/.vim/ftplugin/python.vim. Put all the plugins and commands you want specifically for Python inside that file.

A good practice is to separate the indent configuration inside another file (~/.vim/indent/python.vim). However, I usually just put the indents inside the plugin file.

            if has('syntax') && !exists('g:syntax_on')
syntax enable
endif

Similar with previous if command, this one checks if Vim is compiled with syntax and if the global variable syntax_on already exists. The goal is to avoid turning on syntax multiple times if it was already enabled.

            set autoindent          

autoindent option will use the current indentation when creating a new line in Insert mode, both through normal Return or o/O.

            set backspace=indent,eol,start          

If you have been using Vim for a while, you would notice that sometimes your Backspace key will not work properly in Insert mode. This would happen when you try to backspace over automatically inserted indents, line breaks, and start of insert.

Setting these backspace options will enable you to perform backspace normally over them in that particular order.

            set complete-=i          

The -= operator in Vim means it will remove the options if it's already set. In this case, it will remove the i option from autocomplete.

The i option states that it will "scan current and included files", which might pollute the autocomplete results if your current file includes a lot of other files. As such, it makes sense to disable this option.

            set smarttab          

smarttab will insert n blanks according to either shiftwidth, tabstop, or softtabstop if they are configured and will also delete a shiftwidth blanks at the start of the line instead of one blank by default.

            set nrformats-=octal          

nrformats is short for number formats, which helps define what kind of format will be considered as number for Vim. If you type 13 and then hover over it in Normal mode and press Ctrl+A, then it will increment it to 14. Or you can use Ctrl+X to decrement it to 12.

The octal option will cause 007 to be incremented to 010 due to using base 8. In normal usage, this is not the expected behaviour since not a lot of people are using base 8 in their daily work. By disabling it, 007 will be incremented to 008.

            if !has('nvim') && &ttimeoutlen == -1
set ttimeout
set ttimeoutlen=100
endif

The nvim variable is used to check if you are running NeoVim or normal Vim. ttimeoutlen sets the timeout for key codes, such as Escape, Ctrl, Shift, etc. By default, the value of ttimeoutlen is -1, which will be changed to 100, but this line sets it explicitly instead.

What's the use of ttimeoutlen? Let's say you set a mapping for Cmd+Shift+Up. The key code for that command is ^[[1;6A. On the other hand, the key code for Esc is ^[. If you set ttimeoutlen to 5000, every time you hit the Esc key, it will wait 5 seconds before registering the Esc command. This is due to Vim thinking there is a chance that you will press the rest of the key codes within the 5 seconds window.

There might be a case where it's useful to set the ttimeoutlen longer, but personally I didn't find them useful for my use case.

            set incsearch          

Incremental search or incsearch allows Vim to directly go to the next matching result as you type your search keywords. Without this setting, you need to press Enter to make Vim go to the search result.

            " Use <C-L> to clear the highlighting of :set hlsearch.            if maparg('<C-L>', 'n') ==# ''
nnoremap <silent> <C-L>
:nohlsearch<C-R>=has('diff')?'<Bar>diffupdate':''<CR><CR><C-L>
endif

Thankfully this code come with an explanation, because I will definitely spend quite a while before figuring out what it meant. If you have hlsearch enabled, then pressing Ctrl+L will clear the highlighting because it will not go away on its own.

            set laststatus=2          

Default value for laststatus is 1, which means Vim's status bar will only show if there are 2 or more Vim windows open. By setting the value to 2, now the status bar will always be there even if you only open 1 Vim window.

            set ruler          

Show the line and column number of the cursor position. Sometimes it's a bit redundant since most plugins for status bar have built-in ones.

            set wildmenu          

When invoking autocomplete in command-line, having wildmenu turned on will show the possible completion above the command-line. Easiest way to see the effect is to type :! in Normal mode and then press Tab to trigger the autocomplete.

            if !&scrolloff
set scrolloff=1
endif

The scrolloff value ensures that you have at least n lines above and/or below your cursor at all time. This line checks if you have set the scrolloff value or not, and set it to 1 if you haven't.

Default value for scrolloff is 5, so this setting actually reduces that to 1.
Some people set the value to a very large number (999) to keep the cursor in the middle of the screen, but I find it a bit disorienting to have the screen refreshes and move by one line every time I created a new line. That said, I also set mine to 1.

            if !&sidescrolloff
set sidescrolloff=5
endif

If scrolloff is for the lines above and below, sidescrolloff is for the number of characters on the left and right side of the cursor. The default value is 0. You can also set this to 999 to keep the cursor in the middle of the line.

            set display+=lastline          

If you open a file with a really long line and it doesn't fit the screen, Vim will usually replace it with a bunch of "@". By setting it to lastline, Vim will show as much characters as possible and then put "@@@" on the last columns.

The line uses += to avoid overriding the setting if you have set it to truncate, which will show the "@@@" in the first columns instead.

            if &encoding ==# 'latin1' && has('gui_running')
set encoding=utf-8
endif

The ==# operator means case-sensitive comparison, which is different than == since it depends on :set ignorecase. This line will change the encoding from latin1 to utf-8 if Vim has GUI running.

            if &listchars ==# 'eol:$'
set listchars=tab:>\ ,trail:-,extends:>,precedes:<,nbsp:+
endif

listchars is used to specify how specific characters are displayed when using the command :list. The default is 'eol:$', so this line will not overwrite your setting if you have configured your own listchars.

Tabs will be replaced with > followed by a number of \ until the end of the tab. Trailing spaces will be replaced with -. Extend character > will be displayed in the last column and precede character < will be displayed in the first column if the current line does not fit on-screen. Finally, nbsp stands for non-breakable space character, which will be replaced with +.

            if v:version > 703 || v:version == 703 && has("patch541")
set formatoptions+=j " Delete comment character when joining commented lines
endif

Fortunately, this is also a commented line. Add the j option for formatoptions if Vim version is newer than 7.3 or version 7.3 with patch 541.

This line will delete comment character (e.g. "#", "//") when you are joining commented lines. The default value for formatoptions in Vim is tcq, but this is a bit misleading since it could change based on the opened file type.

            if has('path_extra')
setglobal tags-=./tags tags-=./tags; tags^=./tags;
endif

The setglobal command is a bit different from the usual set command. By using set, we set both the local and global value of an option, while the setglobal only set the global value.

Global value is the default value that is applied to all of Vim. Local value only applies to the current window or buffer.

tags command defines the file names for the tag command. What the line does is to remove ./tags and ./tags; from the current tags. The ^= command prepends the value ./tags; into the current tags.

For example, if the current value for tags is ./a, ./b and we execute setglobal tags ^= ./tags;, it will be changed into ./tags;, ./a, ./b. The ;means that Vim will stop looking for the tag file if it is already found in ./tags.

            if &shell =~# 'fish$' && (v:version < 704 || v:version == 704 && !has('patch276'))
set shell=/usr/bin/env\ bash
endif

=~# operator means match with regular expression, so in this case it checks if you are using fish shell. There are some known issues with Vim when it's running on fish, mainly because fish is not fully POSIX compatible.

This line will set your shell to bash if you are using older version of Vim (older than 7.4) or using version 7.4 without patch 276 to avoid errors in running some commands that could not be executed with fish shell.

            set autoread          

This will make Vim automatically read a file when it detects that the file has been changed outside of Vim. However, if the file is deleted, it will not be re-read to keep the content from before it was deleted.

            if &history < 1000
set history=1000
endif

Set the number of history that is kept to minimum 1000. By default, Vim will only remember 50 for each type of history. There are five types of history in Vim.

  • : commands
  • search strings
  • expressions
  • input lines (input() function)
  • debug mode commands
            if &tabpagemax < 50
set tabpagemax=50
endif

Set the maximum tab pages to be opened using the -p command line argument or the :tab all command to at least 50 from the default value 10.

            if !empty(&viminfo)
set viminfo^=!
endif

Viminfo is a file that is written automatically by Vim as sort of a "cache" to remember information such as command line history, search string hitory, last search pattern, global variables, etc.

This line will prepend the ! value, which will save and restore global variables that start with uppercase letter and don't have any lowercase letter in them.

Alternatively, if you want to run Vim in a kind of private mode, you can disable the viminfo functionality by using the command set viminfo=.

            set sessionoptions-=options          

Removing the options value from sessionoptions will disable saving of options, mappings, and global values when you use :mksession to make a session.

Tim Pope himself explains that he disable it because remembering global options will override changes made to the vimrc, which he doesn't want.

            set viewoptions-=options          

Similar with the previous line, this line will disable the same things when using :mkview to make views.

I am not too familiar with views, but this page explains how not using this line could cause Vim to jump to a different working directory when loading a saved view.

            " Allow color schemes to do bright colors without forcing bold.            if &t_Co == 8 && $TERM !~# '^Eterm'
set t_Co=16
endif

t_Co is the number of colours used by the terminal. This will increase the colours to 16 when the current number of colours is set to 8 and current terminal is not Eterm.

Most modern terminal doesn't need this though, since usually it will be set to 256 by default. You can check this by typing :echo &t_Co.

            " Load matchit.vim, but only if the user hasn't installed a newer version.            if !exists('g:loaded_matchit') && findfile('plugin/matchit.vim', &rtp) ==# ''
runtime! macros/matchit.vim
endif

matchit is a Vim feature that enables us to jump between opening and closing parentheses, HTML tags, and more. You can try this by pressing % in Normal mode on top of an opening bracket and it will move your cursor to the matching closing bracket.

This line will enable matchit if it is not yet enabled or the existing one is still the older version.

            if empty(mapcheck('<C-U>', 'i'))
inoremap <C-U> <C-G>u<C-U>
endif
if empty(mapcheck('<C-W>', 'i'))
inoremap <C-W> <C-G>u<C-W>
endif

The mapcheck command checks if you have set a custom mapping for specific key in certain mode. In this case, it checks if you have mapped Ctrl+U and Ctrl+W in Insert mode before creating a new mapping for them.

These two lines will prevent accidental deletion without the possibility of doing undo with both Ctrl+U and Ctrl+W. By doing Ctrl+G u before the actual Ctrl+U or Ctrl+W, we can recover our deleted text with the undo operation (u in Normal mode), which is not possible without these remaps.

Final Thoughts

After understanding, or at least trying to understand, all of these lines, decided to not install Sensible as a plugin, but instead steal some of the useful lines.

One of my favourites is the set incsearch paired with set hlsearch to enable incremental search and highlight the search result incrementally too.

There are some I omitted, such as setting the shell to bash if using fish shell since I'm using zsh shell myself.

At first, I started my search for the best vimrc file to built on top of. Along the search, I found out that a lot of other people also searched the same thing. However, after searching more about Vim configuration and gaining basic understanding of it, I decided to build one from scratch since I use Vim not only for coding but also for writing.

If any of the explanation is wrong or not clear enough, please feel free to post a comment to give correction or ask for clarification on any lines.

One last tip, if you are ever unsure of what a line does, you can search Vim's manual by typing :h 'commandname' to show the help for a specific command. It's a good practice to know what the commands do and its effect in your vimrc file.

Learning Machine is a series of stories about things happening in the world of Machine Learning that I found interesting enough to share. Oh, and sometimes it will be about the fundamentals of Machine Learning too. Follow me to get regular updates on new stories.

How to Check if a Plugin Installed in Vim

Source: https://towardsdatascience.com/explaining-every-line-of-vim-sensible-b776affec51a

0 Response to "How to Check if a Plugin Installed in Vim"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel