Comfortable editing with VIM

Tomado del blog de Tobias Schlitt

Next occurrence of a word

If you hit*in command mode and your cursor resides on a word, you are taken to the next occurrence of the word. This is quite nice, if you like to know, where a function is called again.

Find matching brace

VIM 7.0 luckily highlights matching parenthesis, if your cursor resides on a brace, but sometimes you need to quickly jump to that matching brace. You can achieve this by hitting the%sign in command mode.

Repeat the last change

It often occurs, that you need to perform 1 change several times, but not often enough to write a short script or to address the changes with a complex regex. In those cases you can perform the change once, move the cursor to the next place and hit the.(dot) char, in command mode.

Enhanced functionality with external programs

This tip is quite commonly known, but I repeat it here, because it’s really helpful: Although VIM has a huge set of useful functions, it does not have everything. If you don’t find a utility in VIM, you can simply use extern shell tools to perform certain tasks, by using:!<command>in command or visual mode. For example sorting a range of lines can easily be achieved with the following 2 steps: 1. Select the lines to sort in visual mode. 2. Type:!sortand hit return.

Indenting and unindeting

Especially when editing code, it happens often, that you have to indent/unindent a code block. If you use VIM’s auto-indenting features (like used by my .vimrc), VIM can do that job for you, too. Simply select the lines to (un-)indent in visual mode and hit<to unindent 1 level or>to indent 1 more level. This also works (as usual in VIM) with quantifiers like 4> to indent 4 more levels.

Remote file editing

Often you have to quickly edit a file on a remote server. Usually you SSH to that server or mount a remote file system locally, edit the desired file and quit the session again. VIM allows you to do this in 1 step, using SCP. On your shell typevim scp://user@server//path/to/the/fileand VIM will edit the file directly through SCP.

Recover the last visual selection

It often happens, that you selected some text in visual mode, e.g. to perform a regex on it. Occasionally your regex is incorrect and you have to revert the changes or they don’t even happen. What you then need is to re-select the text portion again, which might take quite a long time, if you deal with large files. Hittinggvin command mode, takes that work from you and selects the last visual selection again.

Grep within VIM

Refactoring happens quite often in dynamic projects. “Real” IDE’s offer a lot of tools for this task. While VIM does not, you can enhance your refactoring process a lot with using regex and this tiny tip: To grep through a lot of files for certain strings and then perform changes on each occurrence, vim offers the:grep command.:grep <string> <files>and VIM will store the list of changes for you and :cn will take you to the next occurance of your string. The :grep command utilizes your systems instance of grep, or a custom command stored in the grepprg configuration variable. There is also an internal implementation of grep available in VIM, which you can use with:vimgrep /<regex>/<flags> <files>. The benefit of the external implementation is, that you can easily customize the command to use, e.g. if you need to ignore certain directories while grepping recursively (for instance .svn dirs). You can also get a list of all occurrences of your grep command, using :ll, which is actually a feature of VIMs quickfix tool. Simply select the next entry you want to jump to and hit return in the location list.

Placeholders in commands

Who doesn’t know the case, where you need to open a file in the same directory, the currently edited file resides in? Sure, you can simply use:split <file>, but if your current working directory is far away from the edited file, you possibly will have to type a long long path again. VIM offers you, to replace certain placeholders in commands automatically, most important:%is replaced with the actual file name (so,:split %will open a new window editing the current file – I know that just:splitdoes the same, but it’s a nice example to show what%does). So, how does this help in the described case? The replacement text can also be modified by certain modifiers, like:h, which removes the last portion of a file path (the file name). For instance if you currently edit/home/dotxp/dev/ez/ezc/trunk/ConsoleTools/src/table.phpand want to split to/home/dotxp/dev/ez/ezc/trunk/ConsoleTools/src/output.phpyou can do this easily using:split %:h/output.php. Another useful modifier is:s?<regex>?<replacement>?, which obviously uses a regex to modify the given path. Unlikely the usual replacement command:s, you need to specify the global modifiergbefore the regex instead of doing so after it. A short example::split %:gs?trunk?/releases/1.1/?will split to the file table.php of ConsoleTools release 1.1.

Emergency help

Appending an exclamation mark (!) to a command (:!) forces VIM to execute the command, even if it might be dangerous to do so. Quite useless, but still funny is:help!.

Rewrap my text

I reached a stage, where do not only edit my source code using VIM, but mainly every other kind of text data (like documentation, todo lists, etc.), because I’m simply so used to its features. When editing plain text, the automatic wrap features (e.g. textwidth) of VIM are quite useful. In some cases, you might edit text later and therefore destroy a nicely wrapped text block. VIM can easily re-perform the wrapping for you: Select the text block in visual mode (using <SHIFT>+<v>) and hit<g><q>.

Uppercase letters

There are a lot of helpful shortcuts on your keyboard, which you might not know about. Every VIM user should now, that<d>deletes something,<y>(yank) copies something and<c>changes something (delete and start inserting) in VIM (all in command mode). Furthermore, most people know that e.g.<d><w>deletes the next word, etc.. What I did not know for a long time was, that<D>, <Y&gt; and<C>are shortcuts to perform the desired operation until the end of the current line. For instance, simply type<C>to change the contents of the current line from your cursors position on.

Tags: , ,

Leave a Reply