vim usage

简介:

Q.How do I open and edit multiple files under VIM text editor running under Ubuntu Linux / UNIX operating systems to improve my productivity?

A. Vim offers multiple file editing with the help of windows. You can easily open multiple files and edit them using the concept of buffers.

Understanding vim buffer

A buffer is nothing but a file loaded into memory for editing. The original file remains unchanged until you write the buffer to the file using w (other file saving related) command.

Understanding window

A window is noting but a viewport onto a buffer. You can use multiple windows on one buffer, or several windows on different buffers. By default, Vim starts with one window, for example open /etc/passwd file, enter:
$ vim /etc/passwd

Open two windows

Start vim as follows to open two windows,split horizontally:
$ vim -o /etc/passwd /etc/hosts
OR
$ vim -o file1.txt resume.txt

(Fig.01: split horizontal windows under VIM)
The -O option allows you to open two windows, split vertically, enter:
$ vim -O /etc/passwd /etc/hosts

How do I switch or jump between windows?

This operation is also known as moving cursor to other windows:

  1. Press CTRL + W + <Left arrow key> to activate left windows

  2. Press CTRL + W + <Right arrow key> to activate right windows

  3. Press CTRL + W + <Up arrow key> to activate to windows above current one

  4. Press CTRL + W + <Down arrow key> to activate to windows down current one

  5. Press CTRL-W + CTRL-W (hit CTRL+W twice) to move quickly between all open windows

How do I edit current buffer?

Use all your regular vim command such as i, w and so on for editing text.

How do I close windows?

Press CTRL+W CTRL-Q to close the current windows. You can also press [ESC]+:q to quit current window.

How do I open new empty window?

Press CTRL+W + n - Create a new window and start editing an empty file in it.

How do I split current window in two?

Press CTRL+W+ s - to split current window in two.

How do I open exiting file in a new windows?

Press [ESC]+:new /path/to/file. This will create a new window and start editing file /path/to/file in it. For example, open file called /etc/hosts.deny, enter:
:new /etc/hosts.deny

(Fig.02: Create a new window and start editing file /etc/hosts.deny in it.)

(Fig.03: Two files opened in a two windows)

How do I  resize Window?

You can increase or decrease windows size by N number. For example, increase windows size by 5, press [ESC] + 5 + CTRL + W+ +.

To decrease windows size by 5, press [ESC]+ 5 + CTRL+ W + -.

Moving windows cheat sheet

Key combination Action
CTRL-W h move to the window on the left
CTRL-W j move to the window below
CTRL-W k move to the window above
CTRL-W l move to the window on the right
CTRL-W t move to the TOP window
CTRL-W b move to the BOTTOM window

How do I quit all windows?

Type the following command (also known as quit all command):
:qall
Note: If any of the windows contain changes, Vim will not exit. The cursor will automatically be positioned in a window with changes.

You can then either use ":write" to save the changes:
:write
or ":quit!" to throw them away:
:quite!

How do save and quit all windows?

To save all changes in all windows and quite , use this command:
:wqall
This writes all modified files and quits Vim.  Finally, there is a command that quits Vim and throws away all changes:
:qall!

Further readings:

  • Refer "Splitting windows" help by typing :help under vim itself.

Series NavigationHow do I Be More Productive In Vim?

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!




{ 9 comments… read them below or add one }



  • 1Bash 07.09.08 at 9:21 pm


  • I find the vertical split to be more useful.

    Type

    :vsp

    for vertical split.



  • 2Ramesh | The Geek Stuff 07.10.08 at 5:50 am


  • Bash,

    Yeah I agree with you. Vertical split is especially very good if you a wide screen monitor.

    I absolutely love the multiple window feature of the vim and it definitely enhances the productivity to a great extend.

    Ramesh
    The Geek Stuff



  • 3mohan 07.10.08 at 6:10 am


  • vimdiff file1.txt file2.txt

    will give you vertical split.



  • 4Tim 07.10.08 at 11:30 am


  • Your text states “You can then either use “:write” to save the changes, or “:quit!” to throw them away.”

    But your command says “:quite!”

    May wanna fix that ;)



  • 5Eric Lin 07.14.08 at 2:10 am


  • When you are in split mode, you can type

    :set mouse=a

    to enable mouse command and then you can use your mouse to drag the window boundary to resize the all windows.

    Cheers



  • 6Aman 07.30.08 at 6:54 am


  • Hi,

    If I have multiple files opened, say by
    $ vim file1.txt file2.txt file3.txt

    Now, :ls shows all the open buffers

    When I do :q ,all the buffers are closed, but is there a way to close a particular buffer, say only file2.txt



  • 7Chris 10.02.08 at 3:47 pm


  • Aman,

    It’s easy to close one buffer. In fact, there are a lot of ways to navigate through many open buffers. Here’s some

    Using your example:
    $ vim file1.txt file2.txt file3.txt

    you currently have 3 open buffers. Let’s say you are currently editing file1.txt

    :bn – go to next buffer (in this case, file2.txt)
    :bp – go to previous buffer (will go backwards to file3.txt)
    :bd – delete current buffer (file1.txt will be closed and file2.txt will become the current buffer) (can also take a filename or number as an argument)
    :b  – go directly to a file

    FILENAME
    If you are editing file2.txt and want to close file3.txt, type:

    :bd file3.txt

    You will still be editing file2.txt, but file3.txt will no longer be open.

    NUMBER
    in vim,
    :ls – this command lists your open files and assigns an identifying number to each one.

    In your example, it would look like
    :ls
    1  “file1.txt” line 0
    2  “file2.txt” line 0
    3  “file3.txt” line 0

    :b 3
    will go directly to file3.txt (which is buffer 3)

    :bd 2
    will delete the file2.txt buffer

    I think this is a good starting point. Google something like “vim buffers” for more info!

    Chris



  • 8Syed K 06.04.09 at 11:26 am


  • Very useful

    Thanks



  • 9Laxman 08.02.09 at 11:45 am


  • Thanks dude
    I generally used gvim instead of vi/vim. I really like to woke with multi-file in split windows. This web page help me to switch window using keystrokes only which i have needed.










本文转自 h2appy  51CTO博客,原文链接:http://blog.51cto.com/h2appy/1546092,如需转载请自行联系原作者
目录
相关文章
|
Shell Linux 开发工具
【异常解决】vim编辑文件时提示 Found a swap file by the name “.start.sh.swp“的解决方案
【异常解决】vim编辑文件时提示 Found a swap file by the name “.start.sh.swp“的解决方案
511 0
|
7月前
|
Shell 应用服务中间件 开发工具
bash: vi: command not found
bash: vi: command not found
|
Linux 开发工具
Vim不常见但是很实用的命令技巧
Vim不常见但是很实用的命令技巧
90 0
|
Shell Linux 开发工具
centos7 -bash: vim: command not found
centos7 -bash: vim: command not found
221 0
|
SQL 开发工具 Perl
[20180417]vim小技巧.txt
[20180417]vim小技巧.txt --//今天调试plsql,发现跟踪到的sql语句在跟踪文件是在一行的,开始以为是开发写成这样,实际上PL/SQL写的语句 --//到跟踪就变成一行,这样就太长,这样为了更好观察我必须设置wrap,执行如下:set wr...
1078 0
|
Python Java Perl
|
机器学习/深度学习 开发工具 Ubuntu