Showing posts with label vim. Show all posts
Showing posts with label vim. Show all posts

Thursday, February 5, 2026

Using splits in vim

Using split is a way that user can open multiple documents in one vim session, but all the documents will be displayed in one display, split across the screen.


For example, we want to copy some content from /etc/passwd, into a file called /tmp/mylist

1. open /etc/passwd
$ vim /etc/passwd

2. To create splits 
  • to create vertical split and divide vim into 2 panels side by side, with the new panel opening /tmp/mylist
:vsp /tmp/mylist
  • To create a horizontal split and divide vim into 2 parts, bottom and up, with the new panel opening a file called /tmp/mylist, use this command
:sp /tmp/mylist
  • You can also create an empty buffer on the new horizontally split panel
:new 
  • And to create an empty buffer on the new vertically split panel
:vnew

3. To move between panels
  • ctrl-w h or ctrl-w left arrow to move left
  • ctrl-w j or ctrl-w down arrow to move down
  • ctrl-w k or ctrl-w up arrow to move up
  • ctrl-w l or  ctrl-w right arrow to move right

4. To close, use below commands 
  • :q to close current panel
  • :only to close other panels except the current one
  • :qa to close all panels
  • :qa! to force close all panels without saving

5. To resize the panels
  • ctrl-w + or ctrl-w - to increase or decrease the current panel's height
  • ctrl-w > or ctrl-w < to increase or decrease the current panel's width
  • ctrl-w _ to maximize the current panel's height
  • ctrl-w | to maximize the current panel's width
  • ctrl-w = to make the panels' width and height uniform
6. To open multiple files in split mode
  • vim -o file1 file2 to open file1 and file2 in horizontal split
  • vim -O file1 file2 to open file1 and file2 in vertical split

Monday, February 2, 2026

Using buffers in vim

Using buffer is simply a way to open multiple documents in a single vim session.


For example, you want to open /etc/passwd, and copy some of it contents into a new file called /tmp/mylist. 

What you would do:

1. open /etc/passwd
$ vim /etc/passwd

2. while in vim, open a new buffer 
:e /tmp/mylist

3. To list all opened buffers, we can use :ls. This is also a way you can know the buffer number
:ls

4. To move to a buffer number
:b <buffer number>

5. We can also use some part of the file name to move to the buffer that the file is opened
:b passwd

6. We can also use :bn to go to the next buffer in the list, or :bp to go to the previous buffer in the list

7. To copy contents between buffer
  • highlight the text that you want to copy in the current buffer (v)
  • then use the usual yank (y) to copy the highlighted texts
  • switch to the target buffer using (:bp), (:bn), (:b number) or (:b filename)
  • paste using paste command (p)

Monday, December 16, 2019

Replacing Single Line Spacing with Double Line Spacing in VIM

Thanks to this post, I learned on how to replace blank lines in my text document into 2 blank lines, to make it neat and clear.

The original document, with single line spacing



My objective is to replace all single blank lines representing new lines, into 2 new lines instead. To do that in vim, I just need to press "escape" to enter command mode and type the command below, which is ":%s/^$/\r/g", where
%s is to replace all occurrence of the selection in a line
^$ for single blank lines
\r carriage return, which will print double blank lines



The result should be like below 


Save and quit as usual, and you get yourself a neater document with double line spacing. Enjoy :)

Tuesday, September 24, 2013

Changing background color theme in vim

Sometimes, when you open a document in vim, the color of the font and background is not easy on the eye, like below:













In order to encounter this, you can change the backgroud of vim to use light background, by using
:set background=light
or
:set bg=light
in vim to become like below:












You can change the backgound back to dark by using
:set background=dark
or
:set bg=dark
in vim.

That's all, hope this is going to be useful to you.


Tuesday, December 18, 2012

Searching in vim with ignorecase on

This is for searching any string in vi or vim, regardless of the case. All you need to do is to press

/\c
while you are in vim followed by your pattern to start this ignore case search. For example, if you  have a list like below:

Miu
MIu
MiU
mIU

and search in vim using
/\cmiu
, vi will list every line as the search output, even though the cases are different. try it :)


Friday, November 26, 2010

Using markers in vi/vim

This feature is useful if for example we want to delete a few lines or characters but we are too lazy to count the lines or characters to use dd or dw command. We can set up to 26 marker using lowercase a-z as the marker's name. The main usage of marker is to mark any location in file.

How to use (we put 'a' as the marker's name):

ma - mark current cursor position as marker named 'a'
`a (backquote a) - move to character marked as 'a'
'a (quote a) - move to first non blank character (line) containing marker 'a'
`` (backquote backquote) - move to last operated marker or toggles with last cursor position, if no marker is set, cursor moves to beginning of file (BOF)
'' (quote quote) - move to beginning og line (BOL) operated marker or toggles with BOL of last cursor position, if no marker is set, cursor moves to BOF

Example on using marker:

If you want to indent 5 lines starting from current line: majjjjj>'a
If you want to delete five words using marker: mawwwwwmb`ad`b

That's all friends :)

Tuesday, May 13, 2008

Replacing words in vi

To replace word in vi, the below steps can be used(replace OLD with NEW). Please make sure you are in command(normal) mode:

  1. to replace first occurrence of OLD to NEW on current line
    • :s/OLD/NEW
  2. to replace all occurrence of OLD to NEW on current line
    • :s/OLD/NEW/g
  3. to replace all occurrence of OLD to NEW between two line numbers (# are the line numbers)
    • :#,#s/OLD/NEW/g
  4. to replace every occurrence of OLD to NEW on current file
    • :%s/OLD/NEW/g

Tuesday, March 25, 2008

Using tab in vi and vim

The new version of vi and vim supports tab function. Make sure your vi and vim version is 7.0 and above to do this. To open new tab, run :tabnew or :tabe in normal mode. T open a file in new tab, use :tabnew filename or :tabe filename. To move between tab, use ctrl+pgup and ctrl+pgdown. You can also use gt to and gT to move between tabs. To move to specific tab number use igt where i is the tab number. To list all the tabs created, use :tabs.

Picture shows tabs in vi