Introduction to the book This book was created with the sole purpose of covering a cookbook or a cheatsheet-style recipe for powering your vim usage. A certain level of basic comfort and mastery is assumed throughout. By leveraging video this book aims to make things easy for the reader. Also having written in book form with plenty of text to go with the video the learning should be smooth and easy to grasp. Let us dive in. How to exit vim Before we get started with the course, the key question I wish to answer is how to exit vim. Whilst doing a Google search for vim questions I found this on top of the list. So let us first address that. I show in the below video 3 ways. Ctrl-Z which suspends the job (works only in command mode) :wq which saves and exits :q! which exits without saving. Most people struggle due to not knowing if you are in command mode or insert mode. That is a problem in vi. In vim however, you can clearly see that at the bottom left of the screen. Reading text files into vim buffer Vim is very powerful for dealing with other files in the file system. By writing and reading between temporary files mostly in directory we can easily copy paste instead of using the copy buffer supported by vim which uses the keystroke or the or keys. /tmp v y yy Once you read the contents of a file you can do various things with it. You can go to a particular line and insert the external file. Next we see how we can fetch output of a command into vim buffer. Reading shell command output into vim Reading files into vim buffer is cool. But what about reading output of a shell command into vim? This could be used with and switch like this. curl -q :r !curl -q <url> There are various tricks we can use too. Writing a part of buffer to a file In this chapter we are simply exploring how vim can help you write out portions of the buffer to the external file. We can simply select using the key or simply use colon command for line ranges. v Of course this means that you can write out the entire file to external file too. In case the file already exists, then vim will complain. You can google for a solution or choose a different file. (Let us keep things simple). Deleting lines In order to delete lines the simple motion on the line will do fine. Keep pressing and deleting lines one by one. dd The colon command with line range works too. :4,80d will delete lines 4 to 80 including both lines. In the video we see how we can delete easily and also using the stroke and which helps us get rid of text as well. will help us delete portions of lines too. v x v Search and replace Vim is mostly used for search replacement of variable names in programming or in editing in which some string is to be replaced with some other string. Vim supports regular expressions and there are several special characters. So if your search string contains a or or then these are regular expression characters and in order to avoid trouble, I simply do this. / \ * :%s#<search>#<replace># This will ensure that the special characters are not interpreted as such and that they are deemed as regular text. Of course, search and replace also operates on regions just like any other vim command that operates on ranges(mostly line ranges). Moving around the vim editing buffer In vim, moving around the buffer is pretty cool. Usually people think of this as voodoo. You mostly move around using the , , and keys. But vim allows you to move around with the 4 arrow keys on your keyboard as well. h k j l Then a full screen forward is a and a full screen backward is . But you also have for half screen forward and so on. Ctrl-F Ctrl-B Ctrl-d Then you have big movements like to go to top end of the buffer and to go to the very end. gg G You can also use as a command to operate using movement. g Then you have the and to move forward and backward regardless of where the cursor is. That is quite useful too. Ctrl-E Ctrl-y Though I use less of that in my editing. Tabbed operation - open each file in own tab Vim introduced multiple tab editing long ago around version 7.1 or so if I am not mistaken. Nowadays I almost always edit multiple files using the command line. vim -p * This opens limited files in separate tabs. If there are 50 files then 50 tabs can't be opened. In such cases you must depend on the next chapter to go to next files pressing colon command. :n Multi-tab editing is pretty cool and I use it often. You can also use this to power editing once you memorize how to move across tabs. Tabbed operation - operate on all tabs You can move across tabs using keys in the next chapter. But the ability to operate on multiple tabs all at once comes in handy. You can simply prefix the operation with the :tabdo <oper> method. That way you can do search and replace or deleting the first few lines or last few lines or whatever you wish to do. Nowadays all Vim releases support tabs and please do not forget to power your editing using tabs. I still remember the days when Vim did not have tabs, now that is passe. Switching between tabs Switching between tabs is really easy and it has an eerie similarity to how you would switch panes in . tmux But switching tabs is easily demonstrated below. The keys to remember are gt gT These two are pressed in to switch tabs forward or backward. command mode Tabbed editing is really nice and takes very less time to master. Editing files without using tabs In case you prefer to edit vim without using tabs or when using scope or some code referencing tool or when using you have to learn how to use the make :e # or the two commands we see, :n and :N to go to the next file and the previous file respectively. Since the file names are given at the bottom of the buffer things are quite easy. Power editing - Multiple undo and redo Multiple undo and redo are pretty cool. But they were not a part of Vi editor by Bill Joy. This is what prompted Bram Moolenaar to write vim in the first place. But nowadays this is a standard feature in all web-based drawing tools, Google docs and so on. Pressing will undo and both in command mode will redo. Your brain will go into a tizzy trying these keystrokes continuously. u Ctrl-r But remember that colon commands are not considered for this. Only editing keystrokes. Print source code into PDF or HTML As a programmer using vim for everyday coding, you rarely have a need to show your code to someone or to make a PDF to share with someone. Use the colon command for that. hardcopy Used like this, :hardcopy > %.ps This will make a which can then be converted to PDF using or something similar. postscript file ps2pdf And the colon command is really amazing since the syntax highlighting and dark mode are preserved as is. :TOhtml Remember to :se background=dark before printing to html. Using yank registers Using yank registers is part and parcel of vim editing. All the time you are copying text and cutting, deleting and pasting multiple times. The colon command lists all the copy buffers which you can use to retrieve some accidental edit or deletion. :reg Obviously, this changes between vim sessions. If you have multiple vim edits simultaneously the reg will change between them. Use file reading and writing to copy paste between them if on same machine. Using the v command to copy The command is supposed to mean . But it is really convenient to copy text and to cut also. v visual You can copy any region of text and the highlighted view makes it so easy to work with Vim. Unlike the magic and other things that do not show any visual feedback. dd Once you copy you can paste with as usual. p The command also makes it incredibly easy to write to external text files as we saw in one of the initial chapters. v It also helps us operate on the selected region and do various colon commands. Enable syntax highlighting and line numbers I can never work with source code or config files without syntax highlighting. Vim made this a great convenience and there are syntax files for every language, syntax and file type. I am typing this book in markdown and vim clearly shows errors due to syntax highlighting. If you are not using syntax highlighting for coding, you are missing something. I also use line numbers for the same reason. Coding can be intense labor and very demanding on your nerves. With syntax highlighting you can simplify and focus. Of course, there are multiple themes for highlighting colors. You can find the full list here. $ ls /usr/share/vim/vim*/colors blue.vim darkblue.vim default.vim delek.vim desert.vim elflord.vim evening.vim industry.vim koehler.vim lists morning.vim murphy.vim pablo.vim peachpuff.vim ron.vim shine.vim slate.vim tools torte.vim zellner.vim In my case is . This will change based on each version. vim* vim82 To set a particular theme all you have to do is use the colon command, :colorscheme industry and you can enjoy Vim editing even more. Spell checking Spell checking is an amazing feature of vim which I use along with to ensure that I don't have a lot of highlights shown in my source code text. autocmd filetype But when you are typing e-mails in mutt or something or writing an article for your blog, then spell checking catches typos really well. All you have to do is: :se spell And I normally have this in my file. But I also have for similar reasons. ~/.vimrc :se textwidth=72 You don't want the text files that do not wrap at line boundaries, usually 72 characters. We are not talking of vimrc customizations in this book. That is a separate topic on its own. But spelling checks are best enabled using autocmd which is also something I am not dwelling upon. A simple colon command can turn spell check on and off just like line numbers as we saw previously. Indentation of source code Source code editing is close to impossible when indentation is not correct. And languages like Python will not even compile or work without proper indentation. In order to help you with this, the keystroke in vim provides indentation for most languages. = But for javascript I use and use the colon command and pipe it using the shell. js-beautify For most languages, however, the key strokes , , , will indent the whole file. gg v G = The indent keystroke only works with motion commands and not colon commands. Joining multiple lines Joining multiple lines I learned by accident. Like most vim users. We use for moving around the buffer and when the caps lock is on, it ends up joining lines. j But that is useful in case we want to do shell scripting or something which does not work with line breaks between command invocations. I also do not know if WYSIWIG editors give this functionality. This is yet another cool feature of Vim in my view. Case inversion using ~ A simple keystroke can reverse the case. Make upper case lowercase and vice versa. ~ This is used with motion commands and so, using is ideal. Or you could use the command too. v g This chapter was written with a view of introducing motion commands. Most people do not use motion commands and use colon commands more often. But motion commands are quite fast and get your job done quicker. These things help you become more efficient at editing. Which is what vim is all about after all... Detecting tabs and newlines When writing Python code we often run into various errors due to tabs looking no different from spaces. Or we might wonder if the line wrapped around or if there is really anew line. In such cases this colon command :se list saves our day. Once we finish our investigation you can turn it off using :se nolist Another useful tip is after doing a search and you want to highlight matching strings. :se nohlsearch will come in handy for that. Conclusion In this short cookbook we went through various power tools that vim provides to improve our editing efficiency. Using videos and text in conjunction must have helped you gain a better understanding of Vim's power instead of text only tutorials or man pages. By the way $ vimtutor is a powerful tutorial on its own merit but does not talk of advanced topics. And :help This colon command is pretty cool too. Purchase this ebook PDF from gumroad Gumroad book link