In this article, we will go through building some of our own keybinds that’ll unleash the real power of vim not only as a text editor but something much much larger. We will go through basic navigation through the g command, keybinds for editing HTML files as well as running external commands right from vim to compile save and run our code. We will decide this article into several sub-topics, feel free to jump to the relevant portion. If you have yet to learn the basics of vim here is an awesome guide to doing so.
Navigation using ‘g’
If you’re one of those tired souls that just can’t get to the next line in vim using the j and k keys when there’s a line wrap, then navigating through the g-key is the ultimate solution. Just press the g key before any directional keys and you can move to the immediate next line even if there is a line wrap! To move to the beginning of the line press g0 and similarly to move to the end of the line press g$.
Another quick trick is capitalization and uncapitalization through the g key. First select any word in visual mode then press gu to uncapitalize the whole word, similarly to capitalize the whole word press gU. We know the ~ character inverts the capitalization, but in typical use-case we generally want the whole word to be capitalized or uncapitalized then this is the solution.
Another use for the g key is to press g first in normal mode then press Ctrl+g this will give you the word count of your currently opened file!
Custom keybinds to quickly edit HTML files
First and foremost we will add in a keybind to quickly jump to places we will be marked with a special series of characters, this will greatly improve mobility and productivity. So here, we will set up a keybind so that whenever we press Space 2 times in a row, the cursor will take us to the places where this special (<++>) set of characters will be placed beforehand, and also put us in the insert-mode. To do this go to your vimrc file and add in the following block of code:
nnoremap <Space><Space> <Esc>/<++><Enter>"_c4l
Now, we will add in some keybinds for HTML editing. First, let us make a keybind for the emphatic tag.
autocmd Filetype html inoremap ;i <em></em><Space><++><Esc>FeT>i
This will create an emphasis tag and put the cursor in between the tags, also it will generate the special set of characters at the end of the tag so that we can quickly hit escape and press space twice to skip to the next portion code.
Keep in mind this keybind will work on insert-mode only, also the filetype plugin should be on, and the rest of the keybinds also work similarly. For bold text we have the following command:
autocmd Filetype html inoremap ;b <b></b><Space><++><Esc>FbT>i
For the paragraph tag we have:
autocmd Filetype html inoremap ;p <p></p><Enter><Enter><++><Esc>2ki
This will put a blank line in between the paragraphs, so as to clearly let the programmer visualize how the code will look once it is rendered in a web browser.
Save, Compile and Run your code within Vim
Using this set of instructions and keybinds we will be able to save compile and run our code from within Vim itself. Just like most code-editors, but without the bloat! Here we will be compiling with mono, you can use whatever compiler works for your language. Personally, I use this same setup to compile and render my LaTex files (i just substituted the compiler for pdflatex and the file extensions as .tex).
Keeping in mind a developer generally works in multiple languages, we have not set these keybinds globally, it is recommended to morph and change these as necessary, and enter the maps through the vim command interface for the current file you’ll be working on.
To compile and run a file named Program.cs we will use the following keybinds:
map ;c :!clear && mcs Program.cs && mono Program.exe<CR>
Now, we can certainly refine this keybind by saving the file first and then compiling and running it so that we don’t get the output of the old file:
map ;c :w<CR>:!clear && mcs Program.cs && mono Program.exe<CR>
To run multiple files (of the same extension) we can use the following configuration:
map ;c :w<CR>:!clear && mcs % && mono %:r.exe<CR>
Here the % means the current file in the buffer, also the :r means to delete the extension of the file, as we will run the .exe file that we generated.
Integrating Rsync with Vim
As a developer, you might take backups of your files on a separate pc periodically. Here we will set up a keybind that will back up our files in a separate pc (in our LAN network). I will be backing up my vimwiki folder to my laptop that is always connected to my home Wifi. You can set up your own folder for your specific use case.
First, we will need to know the IP address of the storage PC, to do this in Linux use the ifconfig command to find the IP Address, it will be of the form:
Now install rsync through your package manager, for Ubuntu and Debian based system this will be:
sudo apt install rsync
Now that you have the setup ready, type in the following block of code into your vimrc file:
map <C-r> :!rsync -avz /path/to/your/folder/or/file email@example.com.XX:~/path/to/destination/folder/
Now, whenever you press Ctrl+r you will be required to pass in the password for the ssh session in your storage PC, and rsync will write all the changes to your storage PC.
Now that you know all the things you can do with Vim, it is finally time to really dive deep into your own configs and keybinds. Feel free to search up vim articles on this website! Thanks for reading.
- Vim Tutorial – All You Need, To Get Started with VIM
- Nano vs VIM editor – What’s the difference between nano and vim editors?