Welcome to the Slackware Documentation Project

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
slackbook:vi [2012/08/20 14:32 (UTC)]
kookiemonster created
slackbook:vi [2012/09/16 15:11 (UTC)] (current)
mfillpot [Sources] updated author list and tags
Line 4: Line 4:
  
  
-Scattered all around your computer are thousands of text files. To a new user, this may seem inconsequential,​ but almost everything in Slackware Linux uses a plain-text file for configuration. This allows users to make changes to the system quickly, easily, and intuitively. In chapter 5, we looked at a few commands such as cat and less that can be used to read these files, but what if we want to make changes to them? For that, we need a text editor, and vi is up to the task.+Scattered all around your computer are thousands of text files. To a 
 +new user, this may seem inconsequential,​ but almost everything in 
 +Slackware Linux uses a plain-text file for configuration. This allows 
 +users to make changes to the system quickly, easily, and intuitively. 
 +In chapter 5, we looked at a few commands such as 
 +**//cat//** and **//less//** that 
 +can be used to read these files, but what if we want to make changes to 
 +them? For that, we need a text editor, and 
 +**//vi//** is up to the task.
  
-In short, vi is one of the oldest and most powerful text editors still used today. It's beloved by system administrators,​ programmers,​ hobbiests, and others the world over. In fact, nearly this entire book was written using vi; only the next chapter on emacs was written with that editor. 
  
-A little further explanation is needed to learn exactly what vi is today though, as Slackware Linux technically doesn't include vi. RatherSlackware includes two vi "​clones",​ elvis(1) and vim(1). These clones add many additional features to vi such as syntax highlightingbinary editing modes, and network supportWe won't go too deeply into all these details. By defaultif you execute vi on Slackware Linux, you'll be using elvis, so all examples in this chapter will assume that is what you are using. If you've used another Linux distribution before, you may be more familiar with vim. If so, you might wish to change the symlink for /usr/bin/vi to point to /usr/bin/vim, or add an alias to your shell'​s startup scripts. vim is generally considered to be more feature-rich than elvis, but elvis is a much smaller program and contains more features than most users will ever need.+In short, **//vi//** is one of the oldest and most 
 +powerful text editors still used today. It's beloved by system 
 +administratorsprogrammershobbiests, and others the world overIn 
 +factnearly ​this entire book was written ​using 
 +**//vi//**; only the next chapter on 
 +**//emacs//** was written with that editor.
  
-vi is very powerful, but also somewhat cumbersome and challening for a new user to learn. However, mastering vi is an important skill for any self-respecting system administrator to learn, as vi is included on nearly every Linux distribution,​ every BSD system, and every UNIX system in existance. It's even included in Mac OS X. Once you've learned vi, you'll not have to learn another text editor to work on any of these systems. In fact, vi clones have even been ported to Microsoft Windows systems, so you can use it there too. 
  
 +A little further explanation is needed to learn exactly what
 +**//vi//** is today though, as Slackware Linux
 +technically doesn'​t include **//vi//**. Rather,
 +Slackware includes two vi //"​clones"//,​ **//​elvis//​**(1)
 +and **//​vim//​**(1). These clones add many additional
 +features to vi such as syntax highlighting,​ binary editing modes, and
 +network support. We won't go too deeply into all these details. By
 +default, if you execute **//vi//** on Slackware
 +Linux, you'll be using **//​elvis//​**,​ so all
 +examples in this chapter will assume that is what you are using. If
 +you've used another Linux distribution before, you may be more familiar
 +with **//​vim//​**. If so, you might wish to change
 +the symlink for ''/​usr/​bin/​vi''​ to point to
 +''/​usr/​bin/​vim'',​ or add an alias to your shell'​s
 +startup scripts. **//vim//** is generally considered
 +to be more feature-rich than **//​elvis//​**,​ but
 +**//​elvis//​** is a much smaller program and contains
 +more features than most users will ever need.
 +
 +
 +**//vi//** is very powerful, but also somewhat
 +cumbersome and challening for a new user to learn. However, mastering ​
 +**//vi//** is an important skill for any
 +self-respecting system administrator to learn, as 
 +**//vi//** is included on nearly every Linux
 +distribution,​ every BSD system, and every UNIX system in existance. ​
 +It's even included in Mac OS X.
 +Once you've learned **//vi//**, you'll not have to
 +learn another text editor to work on any of these systems. In fact,
 +**//vi//** clones have even been ported to Microsoft Windows
 +systems, so you can use it there too.
 ===== The Different Modes of vi ===== ===== The Different Modes of vi =====
  
-New users are often frustrated when using vi for the first time. When invoked without any arguments, vi will display a screen something like this.+New users are often frustrated when using **//vi//** 
 +for the first time. When invoked without any arguments, ​ 
 +**//vi//** will display a screen something like 
 +this. 
 + 
 + 
 +<​code>​
  
-<​code>​~ 
 ~ ~
 ~ ~
Line 27: Line 74:
 ~ ~
 ~ ~
-                                     Command</​code>​+
 +                                     Command 
 +</​code>​
  
-At this point, the user will being typing and expect the keys he presses to appear in the document. Instead, something really strange happens. The reason for this is simple. vi has different operation "​modes"​. There is a command mode and an insert mode. Command mode is the default; in this mode, each keystroke performs a particular action such as moving the cursor around, deleting text, yanking (copying) text, searching, etc. 
  
 +At this point, the user will being typing and expect the keys he
 +presses to appear in the document. Instead, something really strange
 +happens. The reason for this is simple. **//vi//**
 +has different operation //"​modes"//​. There is a command mode and an insert
 +mode. Command mode is the default; in this mode, each keystroke
 +performs a particular action such as moving the cursor around, deleting
 +text, yanking (copying) text, searching, etc.
 ===== Opening, Saving, and Quitting ===== ===== Opening, Saving, and Quitting =====
  
-Ok, so you've decided that you want to learn how to use vi. The first thing to do is learn how to open and save files. Opening files is actually pretty easy. Simply type the filename as an argument on the command-line and vi will happily load it for you. For example, vi chapter_11.xml will open the file chapter_11.xml and load its content onto the screen, simple enough. But what if we've finished with one document and wish to save it? We can do that in command mode using the [:wcommand. When in command mode, pressing the : key temporarily positions the cursor on the very bottom line of the window and allows you to enter special commands. (This is technically known as ex-mode after the venerable ex application which we will not document here.) The command to save your current work is [:w]. Once this is done, vi will write your changes to the buffer back into the file. If you wish to open another document, simply use the [:e other_documentcommand and vi will happily open it for you. If you've made changes to the buffer but haven'​t saved it yet, [:ewill fail and print a warning message on the bottom line. You can bypass this with the [:e!command. Most ex-mode commands in vi can be "​forced"​ by adding ! to them. This tells vi that you want to abandon any changes you've made to the buffer and open the other document immediately.+Ok, so you've decided that you want to learn how to use  
 +**//vi//**. The first thing to do is learn how to 
 +open and save files. Opening files is actually pretty easy. Simply type 
 +the filename as an argument on the command-line and  
 +**//vi//** will happily load it for you. For 
 +example, vi chapter_11.xml will open the file 
 +''​chapter_11.xml'' ​and load its content onto the 
 +screen, simple enough. But what if we've finished with one document and 
 +wish to save it? We can do that in command mode using the //:w// 
 +command. When in command mode, pressing the <​key>'​:'</​key> ​key 
 +temporarily positions the cursor on the very bottom line of the window 
 +and allows you to enter special commands. (This is technically known as 
 +ex-mode after the venerable ​**//ex//** application 
 +which we will not document here.) The command to save your current work 
 +is //:w//. Once this is done, **//vi//** will 
 +write your changes to the buffer back into the file. If you wish to 
 +open another document, simply use the //:e other_document// 
 +command and **//vi//** will happily open it for you. 
 +If you've made changes to the buffer but haven'​t saved it yet, 
 +//:e// will fail and print a warning message on the bottom line. 
 +You can bypass this with the //:e!// command. Most ex-mode 
 +commands in **//vi//** can be //"​forced"​// by adding 
 +<​key>'​!'</​key> ​to them. This tells **//vi//** 
 +that you want to abandon any changes you've made to the buffer and open 
 +the other document immediately.
  
-But what if I don't like my changes and want to quit or start over? That's easily done as well. Executing the [:e!] command without any arguments will re-open the current document from the beginning. Quitting vi is as simple as running the [:q] command if you haven'​t made any changes to the buffer, or [:q!] if you'd like to quit and abandon those changes. 
  
 +But what if I don't like my changes and want to quit or start over?
 +That's easily done as well. Executing the //:e!// command
 +without any arguments will re-open the current document from the
 +beginning. Quitting **//vi//** is as simple as
 +running the //:q// command if you haven'​t made any changes to
 +the buffer, or //:q!// if you'd like to quit and abandon those
 +changes.
 ===== Moving Around ===== ===== Moving Around =====
  
-Moving around in vi is perhaps the hardest thing for a new user to learn. vi does not traditionally use the directional arrow keys for cursor movement, although in Slackware Linux that is an option. Rather, movement is simply another command issued in command-mode. The reason for this is rather simple. vi actually predates the inclusion of directional arrow keys on keyboards. Thus, movement of the cursor had to be accomplished by using the few keys available, so the right-hand "home row" keys of h, j, k, and l were chosen. These keys will move the cursor about whenever vi is in command mode. Here's a short table to help you remember how they work.+Moving around in **//vi//** is perhaps the hardest 
 +thing for a new user to learn. ​**//vi//** does not 
 +traditionally use the directional arrow keys for cursor movement, 
 +although in Slackware Linux that is an option. Rather, movement is 
 +simply another command issued in command-mode. The reason for this is 
 +rather simple. ​**//vi//** actually predates the 
 +inclusion of directional arrow keys on keyboards. Thus, 
 +movement of the cursor had to be accomplished by using the few 
 +keys available, so the right-hand ​//"home row"// keys of 
 +<​key>'​h'</​key>​<​key>'​j'</​key>​<​key>'​k'</​key>​, and 
 +<​key>'​l'</​key> ​were chosen. These keys will move the cursor about 
 +whenever ​**//vi//** is in command mode. Here's a 
 +short table to help you remember how they work.
  
 ^Command ^ Result ^ ^Command ^ Result ^
Line 47: Line 144:
 |l|Move the cursor one character right| |l|Move the cursor one character right|
  
-Moving around is a little more powerful than that though. Like many command keys, these movement keys accept numerical arguments. For example, 10j will move the cursor down 10 lines. You can also move to the end or beginning of the current line with $ and ^, respectively. +Moving around is a little more powerful than that though. Like many 
 +command keys, these movement keys accept numerical arguments. For 
 +example, ​**10j** will move the cursor down 10 lines. You 
 +can also move to the end or beginning of the current line with 
 +<​key>'​$'</​key> ​and <​key>'​^'</​key>​, respectively.
 ===== Editing A Document ===== ===== Editing A Document =====
  
-Now that we're able to open and save documents, as well as move around in them, it's time to learn how to edit them. The primary means of editing is to enter insert mode using either the i or a command keys. These either insert text at the cursor'​s current location, or append it after the cursor'​s current location. Once into insert mode, you can type any text normally and it will be placed into your document. You can return to command mode in order to save your changes by pressing the ESC key.+Now that we're able to open and save documents, as well as move around 
 +in them, it's time to learn how to edit them. The primary means of 
 +editing is to enter insert mode using either the <​key>'​i'</​key> ​or 
 +<​key>'​a'</​key> ​command keys.  These either insert text at the 
 +cursor'​s current location, or append it after the cursor'​s current 
 +location. Once into insert mode, you can type any text normally and it 
 +will be placed into your document. ​ You can return to command mode in 
 +order to save your changes by pressing the <​key>'​ESC'</​key> ​key. 
 +===== Vi Cheat Sheet =====
  
-===== vi Cheat Sheet ===== +Since **//vi//** can be difficult to learn, I've 
- +prepared a short cheat sheat that should help you with the basics until 
-Since vi can be difficult to learn, I've prepared a short cheat sheat that should help you with the basics until you begin to feel comfortable.+you begin to feel comfortable.
  
 ^ Command ^ Result ^ ^ Command ^ Result ^
Line 80: Line 188:
 |X|Delete the previous character| |X|Delete the previous character|
 |u|Undo the last action| |u|Undo the last action|
-|:​s'​old'​new'​g|Replace all occurances ​of '​old'​ with '​new'​ (current line only)| +|:​s'​old'​new'​g|Replace all occurrences ​of '​old'​ with '​new'​ (current line only)| 
-|:​%s'​old'​new'​g|Replace all occurances ​of '​old'​ with '​new'​ (all lines)| +|:​%s'​old'​new'​g|Replace all occurrences ​of '​old'​ with '​new'​ (all lines)| 
-|/​asdf|Locate next occurance ​of asdf|+|/​asdf|Locate next occurrence ​of asdf|
 |:q|Quit (without saving)| |:q|Quit (without saving)|
 |:w|Save the current document| |:w|Save the current document|
 |:w file|Save the current document as '​file'​| |:w file|Save the current document as '​file'​|
 |:x|Save and quit| |:x|Save and quit|
 +====== Chapter Navigation ======
 +
 +**Previous Chapter: [[slackbook:​working_with_filesystems|Working with Filesystems]]**
 +
 +**Next Chapter: [[slackbook:​emacs|Emacs]]**
 +====== Sources ======
 +<!-- If you copy information from another source, then specify that source -->
 +  * Original source: [[http://​www.slackbook.org/​beta]] \\
 +<!-- Authors are allowed to give credit to themselves! -->
 +  * Originally written by Alan Hicks, Chris Lumens, David Cantrell, Logan Johnson
 +<!-- * Contrbutions by [[wiki:​user:​yyy | User Y]] -->
 +
 +<!-- Please do not modify anything below, except adding new tags.-->
 +<!-- You must also remove the tag-word "​template"​ below. Otherwise your page will not show up in the Table of Contents -->
 +{{tag>​slackbook vi text_editor}}

In Other Languages
QR Code
QR Code slackbook:vi (generated for current page)