Getting more out of Vim
Vim is a complex and big editor. In my opinion this is a good thing. As long as it doesn't harm the stability and speed of the program. That Vim isn't very graphical is no problem for me. But I've seen that it can be a problem for some people.
That Vim isn't graphical makes it fast and useful when changing files on servers around the internet. When using an editor that isn't running on the same system as the one you're editing the file on, there can be a problem with delays when transferring the file.
On to the good stuff
There are many articles and cheatsheets for help by using Vim. Most of these articles and cheatsheets are simple and aimed at beginners. I think there should be more articles for advanced users searching for mastery of their tools. In this article I hope to create an article for advanced Vim users. Comments are appreciated
The article The seven habits of effictive text editing gives a high-level overview of the steps you can take to become faster at editing text. These steps are in my opinion interesting for anyone editing text on a daily basis. As a programmer I edit a lot of text files. And by speeding up the process of editing I can get a lot more work done. It also makes the boring parts a bit easier and faster.
There are many ways to automate editing actions in and outside vim. The next list shows a few of them.
- Macros
- Record actions, so that you can replay them later. To start recording a macro type `q` followed by the name of a register; this can be any letter. Then type the commands that you want to record. Type `q` to stop recording the macro. Macros will be saved in the registers. This means you can view the macros with the command `:reg[isters]`.
- Search, repeat
- Search for the place you want to edit with `/` and make the change. Then use `n` and `.` to repeat it for all other occurrences.
- External script
- Write an external script in your favorite scripting language (or with commandline tools) and apply it to some region of your file. There are many ways to select a region in Vim, of which visual line mode (`:help V`) is the easiest. In visual mode use `!` to start an external script. The output of the script will replace the currently selected region.
- Abbreviations
- Abbreviate a piece of text, that is kinda long. I use `stdlog` to expand to `print {*STDLOG} `. This save typing on some constructs. To create an abbreviation type `:abbr name output`, where `name` is the word you want to type and `output` is the value that should actually appear. To save the abbreviations for a next session you should add them to your `~/.vimrc` file.
- Substitute some text by some other text
- This can be done with the `s///` command. To change a *pattern* in the whole file use `:%s/pattern/text/`.
Splitting windows
Splitting windows in Vim can be really useful. I wil show three times when a split screen can save you a lot of scrolling.
- A huge file
- Sometimes you have to edit a huge file and you need to be at two places a the same time. Perform the following steps: (1) split the window (`:split`), (2) move to the right spot in the file, (3) Use `Ctrl-W Ctrl-W` to move between the two splits of the file.
- Two files that are almost the same. (diff)
- When you have to edit two files that are almost the same, it's useful to use `vimdiff` to open the two files. Opening files with `vimdiff` splits the screen horizontally. The splits contain syntax highlighting for the differences.
- Two files (function and use)
- Sometimes you need to add a new function to a library file. It's useful then open the library file in a split (use `:split filename` for this). Other times you may want to view a function you use. You need to use tags for this. When you have a tag file you can open the target of the tag in the other split with `Ctrl-W ]`.
There is still more to come.