Vim: Difference between revisions

From DISI
Jump to navigation Jump to search
(→‎Substitution: added entry regarding substitution)
(→‎Substitution: added info about how to add a ^M carriage return call)
 
Line 52: Line 52:
I issue vim command:  
I issue vim command:  
  :113s/ /^M/g
  :113s/ /^M/g
This looks at line 113 and searches for instances of a single whitespace and replaces it with '^M' which translates to carriage return.  With the /g option, it will do this to all the instances of whitespace on line 13.  So my end product is:  
This looks at line 113 and searches for instances of a single whitespace and replaces it with '^M' which translates to carriage return (I can insert a ^M into the vim cli by pressing Ctrl+v and pressing enter).  With the /g option, it will do this to all the instances of whitespace on line 13.  So my end product is:  


113 openmpi  
113 openmpi  

Latest revision as of 20:23, 15 October 2018

Introduction

Start vim by editing a single file with the command:

vim file

If you input a nonexisting file name, it will start creating a new file.

vim newfile

vim has two modes: edit mode and insertion mode. vim begins in edit mode which does not allow you to type text into the file.
Only in insertion mode can you insert text with the keyboard. You can enter insertion mode by pressing 'i'. While in insertion mode, you can press 'Esc' to return to edit mode.

Edit Mode Commands

x : deletes character that is under cursor
dw: delete from cursor to next word
dd: delete entire line that cursor is on
p : paste previously deleted material
wq or x : save and quit
w : save 
w filename: save as "filename"
q!: quit without saving
v: enters Visual Mode 

To comment out multiple lines at once: With the cursor at the first line you want to comment out type:

ctrl+v

Arrow down to last line you want commented out. Then type:

shift+i
#
esc
enter

To replace every occurrence of a word with another word (for example replace the word ‘foo’ with the word ‘bar’) type:

%s/foo/bar/g

Visual Mode

Visual mode in vim allows you to highlight characters or lines. Once your desired string has been highlighted, you can press 'y' to copy the highlighted string.
Use 'p' to paste the previously copied string.

Substitution

While vim is in an open file, use command ':set number' to see the line numbers of a text file.

The pattern of substitution is:

:s/<search for this pattern>/<replace with this one>

Search for pattern and replace it globally:

:s/<search for this pattern>/<replace with this one>/g

Example: Spacing a list of packages appropriately for ansible. Ansible has very strict spacing requirements. On line 113 of my playbook, I need the packages names to each have a their own line and a dash in front of them. Due to ansible spacing requirements, they must be spaced six spaces away from the left edge the window. I currently have the following string on line 113:

openmpi mpich-3.2 mpich-3.2-devel

I issue vim command:

:113s/ /^M/g

This looks at line 113 and searches for instances of a single whitespace and replaces it with '^M' which translates to carriage return (I can insert a ^M into the vim cli by pressing Ctrl+v and pressing enter). With the /g option, it will do this to all the instances of whitespace on line 13. So my end product is:

113 openmpi 114 mpich-3.2 115 mpich-3.2-devel

This is more along the lines of what I wanted but Ansible is not happy with this. It needs spacing and a dash '-' before each package name. Next I issue the following vim command:

:113,115/^/      - /
   113       - openmpi
   114       - mpich-3.2
   115       - mpich-3.2-devel

At last, I've spaced and dashed my package names appropriately. Ansible should be satisified with this.