Skip to main content

VIM Faster Navigation

Sometimes even the simplest of tasks like navigating through a single file can be optimized. Vim offers several methods of navigation within a file, which can adapt to the contents of the file and how it is organized. Some of these methods are obvious, while others are more complex.






Mostly, the files we are editing are well structured. If our files are text, then this structure can be in the form of paragraphs, sentences, and words, or at other times code with functions, blocks, and code lines.



Vim supports jumping around the file, according to the structure in the file, and has key bindings that make it easy to go to the exact place in the file where you want to go.



Moving within a text file



You are working on a normal text file and in the middle of a sentence you realize that you have forgotten to make the first letter in the paragraphs uppercase. You could of course, use the arrow-keys or the h/j/k/l navigation keys to move to the beginning of the paragraph to correct this. It would, however, be faster to just press the following in normal mode:



{

You are now placed at the beginning of the paragraph—or in the empty line just above it (if one exists). Now, you go into normal mode by pressing the Esc key and then use the movement command { to go to the beginning of the paragraph. The same can be applied if you want to go back to the end of the paragraph—you simply have to use the counterpart of {, namely:



}



Again, Vim helps you move faster and offers you a pair of commands to move to the beginning and end of the current sentence. The commands are as follows:



( : Move to the beginning of the sentence.

) : Move to the end of the sentence.





w : Move to the beginning of the next word.

b : Move to the beginning of the previous word.

e : Move to the end of the word.



These commands can be combined such that if you want to go to the end of the next word you simply press:



we



Moving in a Code file

Compared to text files, code does not have any paragraphs or sentences to navigate through. It does, however, frequently contain a lot of structures and blocks, each of which has a very specific contextual meaning within the code. An example could be the simple code block:

If( a == b) {

print "a and b are the same";

}

Here the line with print is within the context of the if block surrounding it.

Because Vim is the favorite editor of many programmers, it offers a lot of movement commands to use when you are working with code. Common for all of them is that the parts of the code you want to jump between need to have a contextual connection to each other.

A simple example could be a construction like the #if-#else-#endif construction from the C programming language. Here we have a beginning (#if), an end (#endif) and a midpoint (#else).

So if you are in the line with #if and press the following command:

%

you will go to the #else part. Pressing it again, will take you to the #endif, and yet another execution of the command will get you back to the #if.

Vim does not know all programming language constructs, but by default it knows most of the contextual constructs of the C programming language. Besides this, it knows the normal block construction methods from most programming languages—the use of parentheses and brackets (for example, '{' is block start and '}' is block end).





So for Vim to find the beginning of the function, it simply has to find the outermost bracket pair and then go to the

opening bracket.

function myExample() {

...many lines of code...

/* cursor is placed at the beginning of this line */

...many lines of code...

}

In the above example, the % command would take us to the closing bracket and pressing it again would take us to the opening bracket. But what if the cursor was actually placed inside another pair of brackets? In that case, the % command would only move the cursor to them, and not the beginning of the function.

Again Vim has some handy commands for you:



[[ and ][ : Move backwards/forward to the next section beginning (e.g., start of a function)

[] and ]] : Move backwards/forward to the next section end (e.g., end of a function)



Executing these commands multiple times in a row takes you to the beginning/end of the next/previous section, and therefore gives you a convenient way of cycling through the functions in a file.







[{ : Move to the beginning of the block

]} : Move to the end of the block

[/ : Move to the beginning of the comment block

]/ : Move to the end of the comment block

Comments

Popular posts from this blog

How to configure multipath Debian CentOS for IBM Storage

This detailed how to guides to achieve high availability and performance on Debian and CentOS for accessing storage space at IBM DS8300 Data Storage Systems. Tested on Debian GNU/Linux 5.x Lenny 64 bits and CentOS 5.3 64 bits running on 8 cores blades, with Host Bus Adapters Qlogic and Emulex Light Pulse Fiber Channel in deployed systems at SERPRO . Observations showed that Debian Lenny has the best performance, for our app load profile and hardware. Also, there are listed a number of previously not clearly documented critical pitfalls to avoid. STUDY whole articles, hints, implications, and cited resources before planning your deployment. Every detail matters . Before start, you must have LUNs at IBM DS8300 storage configured for high availability and performance as explained at the article How to configure maximum performance storage space for Debian GNU/Linux on IBM DS 8300 Data Storage Systems . Multipath and storage basic concepts In order t...

Squid Access Lists

Access Lists There are a number of different access lists: http_access : Allows HTTP clients (browsers) to access the HTTP port. This is the primary access control list. http_reply_access : Allows HTTP clients (browsers) to receive the reply to their request. This further restricts permissions given by http_access , and is primarily intended to be used together with rep_mime_type acl for blocking different content types. icp_access : Allows neighbor caches to query your cache with ICP. miss_access : Allows certain clients to forward cache misses through your cache. This further restricts permissions given by http_access , and is primarily intended to be used for enforcing sibling relations by denying siblings from forwarding cache misses through your cache. cache : Defines responses that should not be cached. url_rewrite_access : Controls which requests are sent through the redirector pool. ident_lookup_access : Controls which requests need an Ident lookup. always_dire...

ipsec tunnel pfSense and Centos

pfSense 1.2.3 -------- external ip: 1.1.1.1 internal ip: 172.20.1.20 internal network: 172.20.1.0/24 Centos 5.5 -------- external ip: 2.2.2.2 internal ip: 172.20.2.1 internal network: 172.20.2.0/24 pfSense config from a reset. Firewall rule to allow all ipsec communication (all protocols). pfSense ipsec config -------------------- Mode: Tunnel Interface: WAN (I'm not sure this should be WAN, but changing it to LAN makes no difference) Local subnet: 172.20.1.0/24 Remote subnet: 172.20.2.0/24 Remote gateway: 2.2.2.2 Phase 1 Negotiation mode: agressive My identifier: My IP adress Encryption algorithm: 3DES Hash algorithm: SHA1 DH key group: 2 Authentication method: Pre-shared key Pre-Shared Key: secret Phase 2 Protocol: ESP Encryption algorithms: Rijndael (AES) Hash algorithms: SHA1 PFS key group: 2   Centos ipsec config ------------------- /etc/sysconfig/network-scripts/ifcfg-ipsec0 TYPE=IPSEC ...