Difference between revisions of "Vim"

From HPC Wiki
Jump to navigation Jump to search
m
m
 
(24 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 +
[[Category:Basics]]
 +
== General ==
  
 +
Vi or Vim (VI IMproved) is a visual text editor that can be run inside a [[shell]]. This has the advantage, that you can quickly write or edit text files in the [[shell]], even over an [[ssh]] connection without needing a graphical user interface. Alternatively there is the option to edit files locally (on your desktop computer) in your favourite editor or IDE (e.g. Notepad++, VSCode, ...) and then transfer the files to the remote (super)computer. Visit the [[File_Transfer|file transfer]] page for more information on that topic. As these tansfers can be a lot of effort, just to fix a missing <code>;</code> or typo, Vi/Vim can come in handy, as they are available on practically every Unix system.
  
== General ==
+
While it's usage is far from intuitive, a few basics are generally helpful. For a more complete coverage of this editor, why people use it and why you should probably do that too, please take a look at the [[#Absolute_Basics|<code>vimtutor</code>]], the [[#References|References]] or the internet in general. There are lots of great resources, presenting reasons for using Vi/Vim and giving detailed instructions for its effective usage.
  
Vim is a visual text editor that can be run inside a [[shell]]. This has the advantage, that you can quickly write or edit text files in the [[shell]], even over an [[ssh]] connection without graphical user interface. An alternative to using a shell-based editor over [[ssh]] is to transfer the file in question via [[scp]], [[ftp]] (or [[Getting_Started#File_Transfer_or_How-to-get-your-data-onto-or-off-the-supercomputer|File-Transfer]] in general) then use an editor or IDE with a graphical user interface (like Notepad++, VSCode, or whatever you fancy) locally and after that transfer the file back to the remote (super)computer again. As this can be a lot of effort, just to fix a missing ; or typo, vim can come in handy.
+
== Modes ==
  
While it's usage is far from intuitive, a few basics are generally helpful. For a more complete coverage of this editor, why people use it and why you should probably do that too, please look at the Vimtutor, the [[#References|References]] or the internet in general. There are lots of good resources out there concerning vim.
+
In Vi/Vim there are six modes from which the four most important ones are listed below.
  
== Vim Modes ==
+
{| class="wikitable"
 +
! Mode Name !! Description !! How to Reach the Mode from Normal
 +
|-
 +
|normal || For navigation and manipulation of text. This is the mode that vim will usually start in, which you can usually get back to with pressing <code>ESC</code>. || you are already there or <code>ESC</code>
 +
|-
 +
|insert || For inserting new text. Insert mode works more or less like you would expect a normal text editor to work with arrow keys for navigation, backspace for deletion and characters for text. || <code>a,A,i,I,o,O</code>
 +
|-
 +
|visual || For navigation and manipulation of text selections, this mode allows you to perform most normal commands, and a few extra commands, on selected text. || <code>v,V,Ctrl-V (Vim-only)</code>
 +
|-
 +
|command-line || For entering editor commands. || <code>:</code>
 +
|-
 +
|search || Searching forward and backward. || <code>/,?</code>
 +
|}
  
In Vim there are two modes. Normal mode and Insert mode. You switch to normal mode with the 'ESC' key and into insert mode by pressing 'i'. Insert mode works more or less like you would expect a normal text editor to work with arrow keys, backspace and typing deleting/producing characters. More information about what normal mode is good for etc. can be found in the [[#References|References]] or the internet.
+
More information about different modes and their uses can be found in the [[#References|References]] or the internet.
  
 +
== Basic Commands ==
 +
To open yourfile.txt in Vi/Vim simply enter
  
== Save/quit/help ==
+
<syntaxhighlight lang="bash">
From normal mode (press Esc to get there) you can type
+
$ vim yourfile.txt
 +
</syntaxhighlight>
  
 +
From normal mode (press <code>Esc</code> to get there) you can type commands.
 +
 +
==== File handling ====
  
 
{| class="wikitable" style="width: 40%;"
 
{| class="wikitable" style="width: 40%;"
| :q || to quit vim
+
| <code>:w</code> || save the current file
 +
|-
 +
| <code>:w filename</code> || save the current file to <code>filename</code>
 
|-
 
|-
| :w || to save the current file
+
| <code>:q</code> || quit Vi/Vim
 
|-
 
|-
| :q! || to quit vim without saving (discarding changes)
+
| <code>:q!</code> || quit Vi/Vim without saving (discarding changes)
 
|-
 
|-
| :x || to quit vim with saving changes
+
| <code>:wq</code> or <code>:x</code> || to quit Vi/Vim with saving changes
 
|}
 
|}
  
For a tutorial you can call the  
+
==== Inserting and replacing text ====
$ vimtutor
+
 
and follow the instructions on screen.
+
{| class="wikitable" style="width: 40%;"
 +
| <code>a</code> || append text after cursor position
 +
|-
 +
| <code>A</code> || append text at the end of the current line
 +
|-
 +
| <code>i</code> || insert text before cursor position
 +
|-
 +
| <code>I</code> || insert text before first character on the current line
 +
|-
 +
| <code>o</code> || insert a new line below current line and place cursor on it
 +
|-
 +
| <code>O</code> || insert a new line above current line and place cursor on it
 +
|-
 +
| <code>rX</code> || replace character under cursor by X
 +
|-
 +
| <code>R</code> || change into replace mode
 +
|}
 +
 
 +
== Tutorial ==
 +
 
 +
For a tutorial you can call the
  
 +
<syntaxhighlight lang="bash">
 +
$ vimtutor
 +
</syntaxhighlight>
  
 +
and follow the instructions on screen.
  
 
== References ==
 
== References ==
Line 36: Line 83:
  
 
[https://wiki.gentoo.org/wiki/Vim/Guide Vim/Guide in the Gentoo wiki]
 
[https://wiki.gentoo.org/wiki/Vim/Guide Vim/Guide in the Gentoo wiki]
 +
 +
[http://www.viemu.com/a-why-vi-vim.html Why should you use vi (or vim)]
  
 
[https://vim-adventures.com/ Vim Game]
 
[https://vim-adventures.com/ Vim Game]

Latest revision as of 15:51, 3 September 2019

General

Vi or Vim (VI IMproved) is a visual text editor that can be run inside a shell. This has the advantage, that you can quickly write or edit text files in the shell, even over an ssh connection without needing a graphical user interface. Alternatively there is the option to edit files locally (on your desktop computer) in your favourite editor or IDE (e.g. Notepad++, VSCode, ...) and then transfer the files to the remote (super)computer. Visit the file transfer page for more information on that topic. As these tansfers can be a lot of effort, just to fix a missing ; or typo, Vi/Vim can come in handy, as they are available on practically every Unix system.

While it's usage is far from intuitive, a few basics are generally helpful. For a more complete coverage of this editor, why people use it and why you should probably do that too, please take a look at the vimtutor, the References or the internet in general. There are lots of great resources, presenting reasons for using Vi/Vim and giving detailed instructions for its effective usage.

Modes

In Vi/Vim there are six modes from which the four most important ones are listed below.

Mode Name Description How to Reach the Mode from Normal
normal For navigation and manipulation of text. This is the mode that vim will usually start in, which you can usually get back to with pressing ESC. you are already there or ESC
insert For inserting new text. Insert mode works more or less like you would expect a normal text editor to work with arrow keys for navigation, backspace for deletion and characters for text. a,A,i,I,o,O
visual For navigation and manipulation of text selections, this mode allows you to perform most normal commands, and a few extra commands, on selected text. v,V,Ctrl-V (Vim-only)
command-line For entering editor commands. :
search Searching forward and backward. /,?

More information about different modes and their uses can be found in the References or the internet.

Basic Commands

To open yourfile.txt in Vi/Vim simply enter

$ vim yourfile.txt

From normal mode (press Esc to get there) you can type commands.

File handling

:w save the current file
:w filename save the current file to filename
:q quit Vi/Vim
:q! quit Vi/Vim without saving (discarding changes)
:wq or :x to quit Vi/Vim with saving changes

Inserting and replacing text

a append text after cursor position
A append text at the end of the current line
i insert text before cursor position
I insert text before first character on the current line
o insert a new line below current line and place cursor on it
O insert a new line above current line and place cursor on it
rX replace character under cursor by X
R change into replace mode

Tutorial

For a tutorial you can call the

$ vimtutor

and follow the instructions on screen.

References

Interactive Vim Tutorial

Vim/Guide in the Gentoo wiki

Why should you use vi (or vim)

Vim Game