Friday, April 27, 2012

Most Useful VI Commands

Once you start working in VI (pronounced "vee-eye"), there is no pointing with a mouse anymore. Instead, you need to learn to use the keyboard.


Below are some vi commands that I found useful when working with files that span hundreds if not thousands of lines of code:

To go to the very first line, type: :1 and hit "enter"
To display line numbers, type: :set number and hit "enter"
To replace all occurrencies of string X with string Y, type: :%s/X/Y/g and hit "enter"; don't forget to substitute the X and Y with actual strings and escape all the special characters if they are part of either of those strings.
If you only want to replace string X with string Y within a specific block of code, this is how you do it: mns/X/Y/g and hit "enter"; m and n are the beginning line number and the end of block line number.
To search for a specific string, type: /your search string goes here
This is case-sensitive
To make your search case insensitive, type: :set ic
To search backward, type: ?your search string goes here
To repeat the search forward, type: :n
To repeat the search backward, type: :N
To delete a block of text in vi, type mx at the beginning of the block and type d`x at the end of the block.
Also, to switch from command mode to input mode, type i
To switch from input mode to command mode, hit the Esc key

Happy coding!

No comments:

Post a Comment