Working With Vim Editor In Ubuntu¶
Let's start working with vim editor on ubuntu. vim is a light weight and powerful text editor used with the command line. It is very useful for system administrators. when deploying the application on the server normal GUI based text editors will not work. Because, GUI is absent on the deployment servers. We also have nano editor but it' doesn't have much features. vim has many features that make things easy.
working with vim editor¶
- Install vim on ubuntu
sudo apt-get install vim
- Open a file with vim
vim <file\_name>
-
Close an opened file
- To close the opened file just type ":q" to close the if you didn't make any changes.
- To close the file after making some changes witout saving the then type ":q!".
- To close the file with saving the changes then type ":x"
-
Save the opened file without closing it
- To do it you have to type ":w". It will write all the changes to the file.
-
Display numbers in vim
- After opening the file in vim editor just type ":set number"
-
Moving pointer/cursor
- we can use either arrow keys to move the cursor or use keys "h" to go left, "j" to go down, "k" to go up, "l" to go right.
-
Search for a word
- search for a word we use regex. To search forward type /
- forward search press 'SHIFT' key with * key
- backward search press 'SHIFT' key with # key
- search for a word we use regex. To search forward type /
-
Highlight searched term
- To highlight the search terms we need to type ":set hls".
- To un-highlight then type ":set nohls".
-
Replace a pattern/word in vim
:[range][substitute]/{match_pattern}/{string}/[flags] [count]- Flags
- **[c]**Confirm each substitution.
- [g] Replace all occurrences in the line.
- [i] Ignore case for the pattern.
- Flags
-
Cut-and-paste or copy-and-paste
- Position the cursor at the beginning of the text you want to cut/copy.
- Press v to select visible text , or V to select complete lines, or Ctrl-v or Ctrl-q to select a block.
- Move the cursor to the end of the text to be cut/copied. While selecting text, you can perform searches and other advanced movement.
- To cut line/text press d (delete) and to press y (yank) to copy.
- Now, move the cursor to the location where you want to paste.
- To press afer the cursor then press p and to paste before the cursor press P.
-
Inserting the text into Vim
- To insert the text first we need to python "i" then it will change into insertion mode. After completing the insertion press Esc to get back to normal.
-
Using multiple windows in vim
- press :e filename - edit another file
- press :split filename - split window and load another file
- press ctrl-w up arrow - move cursor up a window
- press ctrl-w ctrl-w - move cursor to another window (cycle)
- press ctrl-w_ - maximize current window
- press ctrl-w= - make all equal size 10
- press ctrl-w+ - increase window size by 10 lines
- press :vsplit file - vertical split
- press :sview file - same as split, but readonly
- press :hide - close current window
- press :only - keep only this window open
- press :ls - show current buffers
- press :b 2 - open buffer #2 in this window
-
word & line completion
- press ctrl-n, ctrl-p - next/previous word completion
- press ctrl-x ctrl-l (ctrl-n/p) - line completion
- press :set dictionary=/usr/share/dict/words, ctrl-x ctrl-k - dictionary completion