-
Notifications
You must be signed in to change notification settings - Fork 2
Vim Command line Texteditor
#Vim
One tool that is very useful in the bash command line environment is Vim, which is a text-editor that is executed in the terminal. Vim is very handy when you want to quickly edit a text file or script. Below is a quick introduction to Vim, consisting of a list of commands that hopefully grows as more users are contributing.
Vim is executed on the command line with:
vim file-to-be-edited
In vim you can navigate through the lines with the arrow-keys on your keyboard. The following list shows commands in order to edit the respective line.
##Vim modes
In vim, you enter different modes in order to do different things. The most commonly used are:
###Normal mode
This is the mode vim starts in. In order to get back to this mode from one of the others, hit esc. From the normal mode, although you don't type directly into the document from this mode, you can do a bunch of useful things like enter commands, delete lines, navigate and search the file.
: to enter command line
esc cancels out of the command line
:wq, w to write (save) and q to quit and enter to execute. Alternately, :x or ZZ will save and exit in one command. :q! will quit without saving. Instantly.
dd deletes the current line
u undo (equivalent of the classic ctrl-z)
:set number shows line numbers in the left margin.
loaded numbers A very useful thing you can do in normal mode is to enter a number with the number keys (not on the : command line) before entering insert or visual mode in order to repeat the insert- or visual action the specified number of times. As an example, if you want to highlight the first 1000 characters of the file, place the marker at the start of the file, write 1000 in normal mode then press v and then the right arrow key. This function can also be used with the insert mode to repeat something you write specified number of times after exiting insert mode by pressing esc
/ searches the document for a pattern that you enter after the /
:%s/x/y/g search and replace. Works like "sed" on the shell command line. Searches the document for x and replaces it with y, globally. To give confirmation before changing, use gc instead of g.
###Insert mode
Press i in normal mode to entering the insert mode. You will see -- INSERT -- written at the bottom left of the screen when in insert mode. Insert mode is for editing the contents of the text document, like any other text editor. When you are done editing, push esc
###Visual mode
Press v to enter visual mode. Your will see -- VISUAL -- written at the bottom left of the screen when in visual mode. It is used for highlighting text, row by row, so that you can remove or cut/copy/paste larger chunks of text. Navigate your cursor with the arrow keys to modify the highlighted area or enter vim with a "loaded number".
For simple copy-paste functionality, Vim uses the terms "yank" and "put". Hotkeys for this are y and p.
An interesting feature in vim is "visual block", accessed by typing ctrl-v, that can be used for selecting a block of text from a files. This can be useful for e.g. deleting characters 3-6 on rows 14-21, or when copying a column of text from a tab-separated file.