linux:vim-editor

Differences

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

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
linux:vim-editor [2017/08/29 09:32] – [Moving Around in Command Mode] michaellinux:vim-editor [2019/05/21 11:20] (current) michael
Line 13: Line 13:
 This guide will be very introductory. There are entire books written about Vi/Vim, but I want to make sure you know at least the basics so you can get up and operating with it. This guide will be very introductory. There are entire books written about Vi/Vim, but I want to make sure you know at least the basics so you can get up and operating with it.
  
----- 
  
 ===== Configuring Vim ===== ===== Configuring Vim =====
 Our first step is to configure Vim. Since we'll generally be working with code or configuration, we'll want line numbering and syntax highlighting on. Vim can be configured by editing the .vimrc file in your home directory. Our first step is to configure Vim. Since we'll generally be working with code or configuration, we'll want line numbering and syntax highlighting on. Vim can be configured by editing the .vimrc file in your home directory.
  
 +<WRAP center box 100%>
 ==== Step 1 - Open a Terminal ==== ==== Step 1 - Open a Terminal ====
 Open up a terminal session such as putty and ensure you are at your home directory with the command: Open up a terminal session such as putty and ensure you are at your home directory with the command:
Line 26: Line 26:
 The terminal should show that you are in /Users/$yourusername. If it doesn't, enter the command: ''**cd**'' The terminal should show that you are in /Users/$yourusername. If it doesn't, enter the command: ''**cd**''
 This will change the directory to your home directory. This will change the directory to your home directory.
 +</WRAP>
 +
 +----
  
 +<WRAP center box 100%>
 ==== Step 2 - Edit the vimrc File ==== ==== Step 2 - Edit the vimrc File ====
 **Your .vimrc file is where your Vim configurations are stored.** As you use Vim, you will start to have more custom configuration in your .vimrc file. I keep a copy of my complex .vimrc on my GitHub, but in general, when you are using Vim on a remote host, unless you are an authorized user, you won't have a custom .vimrc so it's important to be familiar with basic behavior. **Your .vimrc file is where your Vim configurations are stored.** As you use Vim, you will start to have more custom configuration in your .vimrc file. I keep a copy of my complex .vimrc on my GitHub, but in general, when you are using Vim on a remote host, unless you are an authorized user, you won't have a custom .vimrc so it's important to be familiar with basic behavior.
Line 47: Line 51:
 On the bottom left of the screen, we can see we are in insert mode. Now we can type in our configuration. You will want the following lines in the file: On the bottom left of the screen, we can see we are in insert mode. Now we can type in our configuration. You will want the following lines in the file:
  
-  * <wrap em>syntax on</wrap> +  * ''syntax on'' 
-  * <wrap em>set wrapmargin=8</wrap> +  * ''set wrapmargin=8'' 
-  * <wrap em>set number</wrap>+  * ''set number''
  
 {{linux:differences-vim-to-vi-vimrc3.jpg?nolink&600|}} {{linux:differences-vim-to-vi-vimrc3.jpg?nolink&600|}}
  
 **Syntax on** enables built-in syntax highlighting for many programming languages and configuration files. **Set wrapmargin=8** gives us an 8-character buffer before the end of the terminal and makes the screen more legible. **Set number** simply turns on line numbering. **Syntax on** enables built-in syntax highlighting for many programming languages and configuration files. **Set wrapmargin=8** gives us an 8-character buffer before the end of the terminal and makes the screen more legible. **Set number** simply turns on line numbering.
 +</WRAP>
 +
 +
 +----
  
 +<WRAP center box 100%>
 ==== Step 3 - Write Your Changes & Quit ==== ==== Step 3 - Write Your Changes & Quit ====
 We will now need to press the **''esc''** key in order to change Vim's mode back to command mode. The "INSERT" text at the bottom left of the screen should disappear when you are in command mode. We will now need to press the **''esc''** key in order to change Vim's mode back to command mode. The "INSERT" text at the bottom left of the screen should disappear when you are in command mode.
Line 70: Line 79:
 That looks a whole lot better. That looks a whole lot better.
  
-----+</WRAP> 
  
 ===== Moving Around in Command Mode ===== ===== Moving Around in Command Mode =====
Line 78: Line 88:
 __Some additional movement keys:__ __Some additional movement keys:__
  
-  * **e** will move you forward to the end of a word +  * ''e'' will move you forward to the end of a word 
-  * **w** will move you forward to the beginning of a word +  * ''w'' will move you forward to the beginning of a word 
-  * **b** will move you backward to the beginning of a word +  * ''b'' will move you backward to the beginning of a word 
-  * **$** will move you to the end of a line +  * ''$'' will move you to the end of a line 
-  * **0** (zero) will move you the beginning of a line +  * ''0'' (zero) will move you the beginning of a line 
-  * **G** will move you to the end of a file +  * ''G'' will move you to the end of a file 
-  * **gg** will move you to the start of a file+  * ''gg'' will move you to the start of a file
 </WRAP> </WRAP>
  
Line 90: Line 100:
 There are, of course, many more ways to move around a file, but these should cover most use-cases. There are, of course, many more ways to move around a file, but these should cover most use-cases.
  
----- 
  
 ===== Searching a File ===== ===== Searching a File =====
Line 100: Line 109:
 In order to search and replace, we use the **:%s/search/replace/** syntax. You will need to enter the colon. In order to search and replace, we use the **:%s/search/replace/** syntax. You will need to enter the colon.
  
-  * <wrap em>:%s/tcpdump/ls/</wrap> will search the entire file and replace every instance of tcpdump with ls. +  * '':%s/tcpdump/ls/'' will search the entire file and replace every instance of tcpdump with ls. 
-  * <wrap em>:%s/myPrivEscalationScript/ls/c</wrap> will search the entire file and replace each instance only if you confirm it.+  * '':%s/myPrivEscalationScript/ls/c'' will search the entire file and replace each instance only if you confirm it.
  
 Vim also supports regular expressions in the same way that [[https://null-byte.wonderhowto.com/how-to/hack-like-pro-linux-basics-for-aspiring-hacker-part-10-manipulating-text-0148539/|grep]] does. Vim also supports regular expressions in the same way that [[https://null-byte.wonderhowto.com/how-to/hack-like-pro-linux-basics-for-aspiring-hacker-part-10-manipulating-text-0148539/|grep]] does.
  
----- 
  
 ===== Saving, Quitting & Shell Escaping ===== ===== Saving, Quitting & Shell Escaping =====
Line 111: Line 119:
 Exiting Vim is always a problem for people just starting out. In order to exit, use these commands: Exiting Vim is always a problem for people just starting out. In order to exit, use these commands:
  
-  * **:w** will write the file +  * '':w'' will write the file 
-  * **:wq** will write the file and quit +  * '':wq'' will write the file and quit 
-  * <wrap em>:q!</wrap> will exit the editor and discard all changes +  * '':q!'' will exit the editor and discard all changes 
-  * **:w** someFileName will write the changes to a file called someFileName+  * '':w someFileName'' will write the changes to a file called someFileName
  
 In some cases, we might want to escape to a shell to browse directory trees or look at other files. In order to execute a system command in Vim, we use the command: In some cases, we might want to escape to a shell to browse directory trees or look at other files. In order to execute a system command in Vim, we use the command:
  
-  * **:!command**+  * '':!command''
  
-This will execute whatever command we put after the bang. This can be a shell ''**:!bash**'', which we can exit to return to Vim, or we could ''**:!ls /etc**'' to view the contents of the /etc directory.+This will execute whatever command we put after the bang. This can be a shell ''**:!bash**'', which we can exit to return to Vim, or we could '':!ls /etc'' to view the contents of the /etc directory.
  
  
  
  • linux/vim-editor.1503991962.txt.gz
  • Last modified: 2017/08/29 09:32
  • by michael