DEV Community


Posted on

How many lines of code does it take to turn vim into a nice editor?

And of course the answer is: it depends. I wish I could say 0... but we don't live in that world. So, it depends on your preferences, how much do you know about vim's features, how do you plan to use it, plugins. A bunch of stuff. Nevertheless I will try to give a you number or several numbers.

Warning: this is entirely based on my own config and experience. Don't take this too seriously.

For quick edits

If I wanted to use vim just for quick edits, maybe on some config files, commit messages or basically anything that uses the EDITOR variable of my shell. I would go for this.

" Don't include vi compatibility
set nocompatible

" Sensible backspace
set backspace=indent,eol,start

" Don't like temp files
set noswapfile 
set nobackup

" preserve state (undo, marks, etc) in non visible buffers
set hidden

" enable incremental search
set incsearch

" Disable search highlight
set nohlsearch

" ignore the case when the search pattern is all lowercase
set ignorecase
set smartcase

" mouse support
set mouse=a

" enable line numbers
set relativenumber

" Disable syntax highlight
syntax off

" Use spaces to indent
set tabstop=2
set shiftwidth=2
set softtabstop=2
set expandtab

" Disable line wrapping
set nowrap

" Set Leader key
let mapleader = "\<Space>"

" Write file
nnoremap <Leader>w :write<CR>

" Begin search & replace
nnoremap <Leader>r :%s///gc<Left><Left><Left><Left>
xnoremap <Leader>r :s///gc<Left><Left><Left><Left>

" Scroll half page and center
noremap <C-u> <C-u>M
noremap <C-d> <C-d>M
Enter fullscreen mode Exit fullscreen mode

If you're new to vim, my apologies for not going into too much detail . I'll make another post for that. I hope the comments are enough for now. Also I want you to remember this:

  • When you open vim you start in "normal mode".
  • i: gets you to insert mode
  • Esc or Ctrl + c: while in insert mode can get you back to normal mode.
  • ZZ: that is shift + z + z in normal mode, it can get you out of vim.
  • :q: : + q + Enter in normal mode can also get you out vim.

This is actually enough to make vim less annoying. You might want to fight me on the syntax off, but I really don't like the default syntax highlight of vim. I would rather turn it off.

Anyway, with 23 lines of code we have a not so annoying vim.

Make it better

Now if you spend more time in vim and you find yourself editing multiple files, then you might want to enable more things.

" Autosave when navigating between buffers
set autowrite

" Automatically re-read file if a change was detected outside of vim
set autoread

" Enable cursorline
set cursorline

" Better color support
if (has("termguicolors"))
  set termguicolors

" Enable syntax highlight
syntax enable

" When opening a window put it right or below the current one
set splitright
set splitbelow

" Keep lines below cursor when scrolling
set scrolloff=2
set sidescrolloff=5
Enter fullscreen mode Exit fullscreen mode

We have 34 lines (35 if you pick a colorscheme). Now vim looks more colorful, and opens the splits in a more "natural" way.

But if we want to make ourselves at home we need some more keymaps. This is the part were preferences and your knowledge on vim play a big role. How many keymaps would I put in a vimrc to make comfortable? According to this, the answer is 29. Based on that file, I have 26 lines of config and 29 lines related to keymaps. Total we have 55 lines of code for a decent experience.

Bigger scope

Going one step further, what if we now use vim to navigate and make changes across multiple files. It's still kind of awkward at that.

To explore a directory vim has a built-in plugin called Netrw. Now, it's not the best thing in the world, but there is hope, I did some research on that. If we invest 52-ish lines we can make it work the way we want (by "we" I mean me).

Changes across multiple files, better known as "find and replace". I did figure out how to do this a few days ago, with and without plugins. It's basically a mix of :vimgrep (or :grep) and then in the quickfix window :cdo s/{your-query}/{the-replacement}/. And that works great... until you need to filter the results of :vimgrep. In order to modify the quickfix list you need to add this line.

set errorformat+=%f\|%l\ col\ %c\|%m
Enter fullscreen mode Exit fullscreen mode

What this does is tell vim how to read it's own quickfix list. In this case is for :vimgrep, the error format for :grep is slightly different.

For those who are curious, I have a demo for you. This is how you "find, filter and then replace" in vim without plugins (the super long way).

see in asciinema

Personally, I use a few plugins to make it a bit easier. But we can skip them and make our own keymaps.

" Go to previous location
nnoremap [q :cprev<CR>

" Go to next location
nnoremap ]q :cnext<CR>

" Manage the quickfix window
nnoremap <Leader>cc :cclose<CR>
nnoremap <Leader>co :copen<CR>

function! QuickfixMapping()
  " Go to next location and stay in the quickfix window
  nnoremap <buffer> K :cprev<CR>zz<C-w>w

  " Go to previous location and stay in the quickfix window
  nnoremap <buffer> J :cnext<CR>zz<C-w>w

  " Make the quickfix list modifiable 
  nnoremap <buffer> <leader>u :set modifiable<CR>

  " Update quickfix window
  nnoremap <buffer> <leader>w :cgetbuffer<CR>:cclose<CR>:copen<CR>

augroup quickfix_mapping
    " Setup keymaps
    autocmd filetype quickfix call QuickfixMapping()
augroup END
Enter fullscreen mode Exit fullscreen mode

With this our line count can grow up to 125 lines of code.

Now make it pretty

The most important thing. How many lines can a good colorscheme have? About 100. If I only leave the most basic stuff of the current theme I'm using, it is about 108 lines.

Total count without plugins

230-something lines. Maybe. It's a lot more if you add comments. Don't believe me? Here is what I have been building this entire time: complete-rc.vim.

What if you want plugins?

Because you might want to use vim fulltime in your work. Now things get really tricky. Now we have to take into account your specific workflow.

I can only speak for myself. In order to use vim fulltime I use 800-something lines of code on my vimrc. Use 30 plugins, which take about 28400 lines. And the funny thing is I don't use many big plugins. No LSP, no fancy autocompletion, linters or formatters. So, if you do need that the line count can go way higher.


How many lines of code does it take to turn vim into a nice editor?

For me, somewhere between 23 and 29200. And lets not forget the external tools like fzf, ripgrep and ctags. Just to make it nice.

Top comments (2)

grahamlyons profile image
Graham Lyons

I've got 10:

set expandtab
set shiftwidth=4
set tabstop=4

set modeline
set modelines=5

set nu
set colorcolumn=80

syntax enable

let g:netrw_liststyle=3
if exists("*netrw_gitignore#Hide")
    let g:netrw_list_hide=netrw_gitignore#Hide()
Enter fullscreen mode Exit fullscreen mode
mculp profile image
Matt Culpepper

ZZ: that is shift + z + z in normal mode, it can get you out of vim.
:q: : + q + Enter in normal mode can also get you out vim.

omg did you finally solve one of the hardest problems in computer science?! 😂