# Beyond the basics By now you have used enough `vim` to see it's tremendous text editing potential. Out of the box it works very well, but with some additional configuration and a few plugins installed it get's even better. We'll go over all basic aspects of tweaking your `~/.vimrc` file from some welcome changes, some aesthetic to a complete IDE. For IDE features we'll focus on `python3` and `bash` but I'll leave some links to point you in the right direction for other languages. The point of this chapter is not to race towards the end but to understand what every step adds to your working comfort. I highly advise to take the changes to your vimrc *slowly* and see if you actually *like* the change. If you don't like a feature you don't have to use it, but it's still good to be aware of where you can take vim for future reference. ## Which vim to install A simple `sudo apt install vim` installs a *basic* version of vim but when you're on a server and need to do some scripting it's a better idea to install `vim-nox`. What's the difference? We can get some information about both packages via `sudo apt-cache show vim vim-nox`. Classic vim will give you the following description: ``` Description-en: Vi IMproved - enhanced vi editor Vim is an almost compatible version of the UNIX editor Vi. . Many new features have been added: multi level undo, syntax highlighting, command line history, on-line help, filename completion, block operations, folding, Unicode support, etc. . This package contains a version of vim compiled with a rather standard set of features. This package does not provide a GUI version of Vim. See the other vim-* packages if you need more (or less). ``` And vim-nox the following: ``` Description-en: Vi IMproved - enhanced vi editor - with scripting languages support Vim is an almost compatible version of the UNIX editor Vi. . Many new features have been added: multi level undo, syntax highlighting, command line history, on-line help, filename completion, block operations, folding, Unicode support, etc. . This package contains a version of vim compiled with support for scripting with Lua, Perl, Python 3, Ruby, and Tcl but no GUI. ``` I would *always* install vim-nox but this is personal preference. Some people like the graphical versions of vim, or `gvim`, but I'm not a fan. Maybe try the vim-gtk package to see if you like it. Do keep in mind that the program you'll invoke is **always** `vim` no matter which version you install! For graphical vim you'll invoke `gvim`. **From here on out I expect a vim-nox installed!** ## Basic improvements You'll probably always want to include the following in your vimrc. You can [download](./assets/vimrc_basic) the file if you want to try it out but do take the time to read the comments. ``` " ---------------------------------------------------------------------------- " basic essentials " ---------------------------------------------------------------------------- " don't make vim vi compatible (if not set you miss out on a lot of features!) " you'll see this option set in most configuration files found online set nocompatible " enable filetype recognition plus indent and plugin (pretty much mandatory) filetype plugin indent on " enable syntax highlighting syntax on " backspace can be a tricky thing and this setting makes it work a lot better set backspace=indent,eol,start " when tab completing on the expert line you don't want to miss out on EDIT vs " edit or nerdtree vs NERDTree and this setting ignores case completely set ignorecase " highlight your search patterns (very handy when building regexes) set hlsearch " highlight the search pattern as-you-go (tremendously helpful when " constructing regexes) set incsearch " always show a status line at the bottom of your vim which shows some basic " information about the file, which line you're at etc set laststatus=2 " show files in statusbar when opening via expert mode set wildmenu " also show all possible expert mode commands in the statusline set wildmode=full " reverse numbering (in the sideline) so you don't have to manually count how " many lines you have to yank set rnu " it's also nice to still have your absolute line number in the sideline set nu " can do copy paste from the clipboard set clipboard=unnamedplus ``` You can test out the vim configuration above by downloading it to your home directory and telling vim explicitly you want *that* vimrc by invoking `vim -u ~/vimrc_basic test.py` where `test.py` is just an example. Each setting has some information about what it does but vim comes with built-in help. You can invoke this help by pressing `:help laststatus`. ## Getting help ![vim help](./assets/vim_01.png) Now, what has happened here? Vim opened it's own documentation in a new *horizontal split* and moved the cursor there. At first this is very intimidating but there are just a few command you need to know to manage this situation perfectly. Don't worry about messing up the documentation, it's opened in read only mode. * you can read the documentation by pressing `j` to go down and `k` to go up * to navigate splits you use `CRTL-w j` to go down, `CRTL-w k` to go up, `CTRL-w l` to to right and `CRTL-w h` to go left * to close the documentation you need to be *inside* the documentation split and press `:close` which means it will close the split you're in (if your cursor is on the **other** split you can use `:only` which does the opposite) The `CTRL-w` affair is a shortcut to vim's window navigation. You can read up a bit more in the manual at `:help window` but below is the gist of it. It might sound a bit confusing at the start but think of `tmux` and it's *panes, splits and windows* and it might make some more sense. ``` Summary: A buffer is the in-memory text of a file. A window is a viewport on a buffer. A tab page is a collection of windows. A window is a viewport onto a buffer. You can use multiple windows on one buffer, or several windows on different buffers. A buffer is a file loaded into memory for editing. The original file remains unchanged until you write the buffer to the file. A buffer can be in one of three states: *active-buffer* active: The buffer is displayed in a window. If there is a file for this buffer, it has been read into the buffer. The buffer may have been modified since then and thus be different from the file. *hidden-buffer* hidden: The buffer is not displayed. If there is a file for this buffer, it has been read into the buffer. Otherwise it's the same as an active buffer, you just can't see it. *inactive-buffer* inactive: The buffer is not displayed and does not contain anything. Options for the buffer are remembered if the file was once loaded. It can contain marks from the |viminfo| file. But the buffer doesn't contain text. ``` If you actually read the documentation you must have noticed you can make tabs, as in firefox tabs, in vim! I tend to mostly use buffers, based on [this](https://stackoverflow.com/questions/26708822/why-do-vim-experts-prefer-buffers-over-tabs) philosophy but you do you. As for window navigation I add the following to my vimrc but you will probably not like it too much. This remaps the arrow keys, which you should **not** be using for navigation text anyway, to window navigation. If the `nnoremap` makes no sense try `:help mapping`. ``` " use the arrows for buffer navigation nnoremap nnoremap nnoremap nnoremap ``` ## Switching quickly from config file to config file A lot of information about vim online is geared towards programmers and not system administrators but I have a trick for you that will probably help you quite a bit when modifying configuration files. You have to get comfortable with opening and navigating buffers first so a quick rundown on the basics. To do this exercise properly first prepare the following files by executing the commands below. ``` echo "I am a first file" > ~/first_file.txt echo "[unit]\nDescription=looks like a config file\nManual=maybe systemd\n" > ~/test.service echo "import datetime\n\nd = datetime.datetime.now()\nprint(d)" >python_test.py ``` ### Part one 1. Open up vim with my basic vimrc you downloaded above and you should have a blank vim in front of you. 2. Type `:edit ~/first_file.txt` to open up the first file. 3. Type `:edit ~/test.service` to open up the second file. 4. Type `:edit ~/python_test.py` to open up the third file. You have now opened up all three files in one vim but how do you switch from one to the other? They are loaded in memory and are called buffers. To list all buffers currently opened in vim you type `:buffers` and you should see a list like the one below. ``` :buffers 1 "first_file.txt" line 1 2 # "test.service" line 1 3 %a "python_test.py" line 1 Press ENTER or type command to continue ``` To go back to the buffer you where are just hit enter but if you want to **switch** to a different one type `:b 1`. This will take you to the first buffer. As you are opening and editing files in the same instance of vim you can yank and paste from one to the other! Try playing around with this and make some changes to some files. At some point you'll be confronted with the following message. ``` E37: No write since last change (add ! to override) ``` This means you're trying to switch to an other buffer but you have unsaved changes in the one you're currently at. I like to add the following to my vimrc to automatically save a buffer upon leaving. Do keep in mind it can be a bit dangerous to overwrite files you're not too certain about. ``` " automatically save buffers set autowrite set autowriteall ``` Once you feel comfortable move on to the next part. ### Part two OK, quit vim completely and go to your home directory, where you created the three files before. Open vim up again with my basic configuration but instead of `:edit` use `:find tes` where is your tab complete key. Vim should list all files matching the `tes` pattern inside the folder you're in. We can supercharge this behaviour by modifying the `path` vim will search in. To see which paths are searched by default type `:set path` and you'll probably see `path=.,/usr/include,,`. This is nice from a *programmers* point of view but we can change it to suit our administrator's point of view better. To completely override we can change the setting by typing `:set path=/etc/**` and vim will now search our `/etc` folder **recursively**. Try it out with `:find ssh` to see it's full power! This is a setting you can either fix in your vimrc, or change in the fly, your choice. **Don't be greedy and set it to the root of your hard drive. This will slow vim down way too much because there are just too many files and folders!** ## IDE like features without plugins We already have syntax highlighting via the `set syntax=on` feature but we can also have autocomplete for quite a lot of scripting languages out of the box. This is one of the main reasons we install vim-nox and not vim! The shortcut to achieve it is a double whammy `` which triggers omnicomplete. Have a look at `:help omnifunc` to learn more about it but first, a hands on example. 1. Open the python file we made before with `vim -u ~/vimrc_basic ~/python_test.py` 2. Navigate to the end of the file, go into **insert** mode and type `datetime.` 3. Remain in insert mode after the `.` and hit `` 4. Stay calm and read on. ![autocomplete](./assets/vim_02.png) The screenshot above is probably very much like what you're confronted with. The *dropdown* menu is a context aware autocomplete menu meaning these are all functions, methods, classes or variables belonging to this module. The horizontal split window at the top shows the documentation of the menu item you have selected. You can navigate this list either with the *arrows* or with `` to go down and `` to go up. I personally don't like the documentation jumping up and down on my screen so you can add the following to remove it all together. Now to be able to view the documentation we'll need to install some plugins. I have not found a clean way of hiding the preview window *and* adding a shortcut to show documentation. ``` set completeopt-=preview ``` ### Complete more things Vim can do a lot more than just complete python code. When you press `` you'll see a menu at the bottom along these lines. ``` -- ^X mode (^]^D^E^F^I^K^L^N^O^Ps^U^V^Y) ``` Every `^CHARACTER` is a different mode of autocomplete! Try out the `^K` one just for fun. It will probably say this the following. ``` 'dictionary' option is empty ``` We can *set* a dictionary, which is just a list of words, to autocomplete from. Let me walk you through an example. 1. Open up a blank text file with my basic vimrc. 2. Go into **insert** mode and type in `hipp`. 3. Stay in **insert** mode and type `` which will show the same error message as before. 4. Exit insert mode with `ESC` 5. Set the dictionary by typing `:set dictionary=/usr/share/dict/american-english` (you should have this file) 6. Go into **insert** moder after `hipp` and type `` again. Nice no? ## Beyond vanilla vim TODO