Vim: Difference between revisions

From Psygen Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(11 intermediate revisions by the same user not shown)
Line 1: Line 1:
[[File:vimlogo.png|right]]
Vim is a highly configurable text editor built to make creating and changing any kind of text very efficient.
<code>vim ''filename.txt''</code> - opens ''filename.txt'' (from the command line, a GUI for vim is available, as well)
You can also open a file to a specified line number:
<code>vim +42 filename.txt</code> - opens filename.txt in vim, and places the cursor on line number 42
<code>vim -M important.conf</code> Opens "important.conf" in "No Modifications allowed" mode. Great for when you want to look at a file, but don't want to accidentally mess it up.


= Modes =
= Modes =


<code>i</code> goes into insert mode
<code>i</code> goes into insert mode
<code>R></code> goes into replace mode


<code>Esc</code> goes back to command mode.
<code>Esc</code> goes back to command mode.
Line 17: Line 30:
<code>:wq</code> Save the file and exit vim
<code>:wq</code> Save the file and exit vim


<code>:w !sudo tee % </code> Saves the file, even you don't have permission to write to the file (do this instead of closing the file, opening with sudo, and re-doing your changes)
<code>:w !sudo tee %</code> Saves the file, even you don't have permission to write to the file (do this instead of closing the file, opening with sudo, and re-doing your changes)


<code>:q</code Quit vim (Note: if you've made changes, you'll get a message that the file has changed. To quit without saving changes, use: <code>:q!</code> )
<code>:q</code> Quit vim (Note: if you've made changes, you'll get a message that the file has changed. To quit without saving changes, use: <code>:q!</code> )


== Navigating Within the Document ==
== Navigating Within the Document ==
Line 30: Line 43:


<code>N</code> Repeat the last search backward (search backward in the document for the next occurrence of whatever you searched for last)
<code>N</code> Repeat the last search backward (search backward in the document for the next occurrence of whatever you searched for last)
<code>*</code> Place the cursor on a word. The * will take you to the next instance of that word.
<code>#</code> Place the cursor on a word. The # will take you to the previous instance of that word.


<code>CTRL + f</code> scroll forward one screen
<code>CTRL + f</code> scroll forward one screen
Line 35: Line 52:
<code>CTRL + b</code> scroll backword one screen
<code>CTRL + b</code> scroll backword one screen


<code>%</code> go to the "mate", if one exists, of the parenthesis, brace, or bracket.
<code>zz</code> Scroll down the screen to make the current line appear in the middle.
 
<code>gg</code> go to the top of the file


<code>zz</code> Scroll down the screen to make the current line appear in the middle.
<code>G</code> go to the bottom of the file


<code>''line-number''G</code> Go to the specified line number.
<code>''line-number''G</code> Go to the specified line number.
<code>%</code> go to the "mate", if one exists, of the parenthesis, brace, or bracket.


<code>``</code> (Type two back-ticks) Jump to where you last were in the document.
<code>``</code> (Type two back-ticks) Jump to where you last were in the document.


== Manipulating Text ==
== Manipulating Text ==
<code>vim + <i>linenumber</i></code> Open a file at the specified line number (Run from the command line, not from inside vim)


<code>:set number</code> show line numbers
<code>:set number</code> show line numbers
Line 53: Line 71:


<code>:syntax on</code> turns on syntax color highlighting for code and HTML
<code>:syntax on</code> turns on syntax color highlighting for code and HTML
<code>:r ''filename'' </code> inserts the contents of ''filename'' at the cursor
<code>:! ''shell_command'' </code> executes the specified shell command (Example: <code>:! cp -a file.txt file.txt.bak</code> to make a backup of the file before you edit it, without leaving vim.)


=== Selecting Text ===
=== Selecting Text ===
Line 58: Line 81:
<code>V</code> Select a whole line (Press an up or down arrow to select multiple lines.)
<code>V</code> Select a whole line (Press an up or down arrow to select multiple lines.)


<code>v</code Select a range of text.
<code>v</code> Select a range of text.


<code>CTRL + v</code> select a rectangular block
<code>CTRL + v</code> select a rectangular block
Line 85: Line 108:


<code>di"</code> delete the text between the quotes
<code>di"</code> delete the text between the quotes


=== Cut, Copy, and Paste ===
=== Cut, Copy, and Paste ===
Line 103: Line 127:


<code>p</code> Paste after the cursor
<code>p</code> Paste after the cursor


=== Substitution ===
=== Substitution ===


<code>:s<code> substitutes
<code>:s</code> substitutes


'''Example:'''
'''Example:'''
Line 115: Line 140:
        
        
<code>:%s/something/something_else/gc</code> Does the same thing as the first example, but asks for confirmation (that's what the “c” is there for.)
<code>:%s/something/something_else/gc</code> Does the same thing as the first example, but asks for confirmation (that's what the “c” is there for.)


= References =
= References =
Line 122: Line 146:
     <li>[http://man.he.net/?topic=vim&section=all vim man page]</li>
     <li>[http://man.he.net/?topic=vim&section=all vim man page]</li>
     <li>Run <code>vimtutor</code> at the command line for an interactive vim tutorial</li>
     <li>Run <code>vimtutor</code> at the command line for an interactive vim tutorial</li>
    <li>[http://www.vim.org/ vim Website]</li>
    <li>[http://vim.wikia.com/wiki/Vim_Tips_Wiki vim tips wiki]</li>
    <li>[http://vimhelp.appspot.com/vim_faq.txt.html vim FAQ]</li>
</ol>
</ol>

Latest revision as of 04:01, 29 September 2017

Error creating thumbnail: Unable to save thumbnail to destination

Vim is a highly configurable text editor built to make creating and changing any kind of text very efficient.

vim filename.txt - opens filename.txt (from the command line, a GUI for vim is available, as well)

You can also open a file to a specified line number:

vim +42 filename.txt - opens filename.txt in vim, and places the cursor on line number 42

vim -M important.conf Opens "important.conf" in "No Modifications allowed" mode. Great for when you want to look at a file, but don't want to accidentally mess it up.

Modes

i goes into insert mode

R> goes into replace mode

Esc goes back to command mode.

Commands

Saving and Exiting vim

ZZ Save and exit vim

:w Write the file to disk, stay in vim

:wq Save the file and exit vim

:w !sudo tee % Saves the file, even you don't have permission to write to the file (do this instead of closing the file, opening with sudo, and re-doing your changes)

:q Quit vim (Note: if you've made changes, you'll get a message that the file has changed. To quit without saving changes, use: :q! )

Navigating Within the Document

/string search forward for string

?string search backward for string

n Repeat the last search forward (search forward in the document for the next occurrence of whatever you searched for last)

N Repeat the last search backward (search backward in the document for the next occurrence of whatever you searched for last)

* Place the cursor on a word. The * will take you to the next instance of that word.

# Place the cursor on a word. The # will take you to the previous instance of that word.

CTRL + f scroll forward one screen

CTRL + b scroll backword one screen

zz Scroll down the screen to make the current line appear in the middle.

gg go to the top of the file

G go to the bottom of the file

line-numberG Go to the specified line number.

% go to the "mate", if one exists, of the parenthesis, brace, or bracket.

`` (Type two back-ticks) Jump to where you last were in the document.

Manipulating Text

:set number show line numbers

:set nonumber hide line numbers.

:syntax on turns on syntax color highlighting for code and HTML

:r filename inserts the contents of filename at the cursor

:! shell_command executes the specified shell command (Example: :! cp -a file.txt file.txt.bak to make a backup of the file before you edit it, without leaving vim.)


Selecting Text

V Select a whole line (Press an up or down arrow to select multiple lines.)

v Select a range of text.

CTRL + v select a rectangular block


Undo / Redo

:earlier 15m reverts the document to how it was 15 minutes ago. You can substitute any valid time instead of 15m.

u undo the latest change

U Undo all changes in the last modified line.

CTRL + R Redo last change (undo the last undo... kinda...)


Deleting

d Deletes the selected text (also the same as "cut", as in you can paste the deleted text)

de does the same thing as diw, however, you can delete every next word by pressing the period key ( . )

dd delete the entire current line

diw delete the current word

di" delete the text between the quotes


Cut, Copy, and Paste

y Copies (yanks) the selected text.

yy Copies the whole line

d Deletes the selected text (also the same as the cut command in most other programs, as in you can paste the deleted text)

ciw cuts the current word

ci" cuts the word inside the quotes

C cuts to the end of the line

P Paste before the cursor

p Paste after the cursor


Substitution

:s substitutes

Example:

:%s/something/something_else/g This finds the word “something” and replaces it with “something_else” in the entire document.

:s/something/something_else/g Does the same thing, but for only the current line

:%s/something/something_else/gc Does the same thing as the first example, but asks for confirmation (that's what the “c” is there for.)

References

  1. vim man page
  2. Run vimtutor at the command line for an interactive vim tutorial
  3. vim Website
  4. vim tips wiki
  5. vim FAQ