Vim

From LearnLinux.tv Wiki
Jump to navigationJump to search
    1. vim
      1. Buffers
            1. List buffers
   :ls
            1. Show open buffers
   :buffers
            1. Switch to buffer 1
   :b1
            1. Switch to the next buffer
   :bn
            1. Switch to the previous buffer
   :bp


            1. Delete the current buffer
   :bd


            1. Delete the current buffer (without saving changes)
   :bd!


    1. Windows
            1. Open a new window
   :new
            1. Open a new window with a particular file
   :new filename


            1. Close a window/split

control+w, q

            1. Open a new window, but with the same file as the other buffer
   :split

or:

   :vsplit


    1. Tabs
            1. Create a new tab
   :tabnew


            1. Create a new tab, open a file
   :tabnew filename.txt


            1. Open files in individual tabs
   vim -p file1 file2 file3


            1. Move tab to position n
   :tabm n


            1. Switch to next tab
   :tabn


            1. Switch to previous tab
   :tabp


            1. Switch to next tab (normal mode)
   gt - Next tab


            1. Switch to previous tab (normal mode)
   gT - Previous tab


        1. Command Mode commands
            1. Create a new buffer:
   :e filename


            1. Move to the next buffer:
   :bn


            1. Move to the previous buffer:
   :bp


            1. Delete a buffer:
   :bd


        1. Normal Mode commands


            1. Show position and status

ctrl-G


            1. Move to the end of the file
   G


            1. Move to the beginning of the file
   gg


            1. Move to the beginning of the paragraph

{


            1. Open a line one line below, enter insert mode
   o


            1. Open a line one line above, enter insert mode
   O


            1. Move to the end of the paragraph:

}


            1. Move to the beginning of the sentence:

(


            1. Move to the end of the sentence:

)


            1. Move to the beginning of the line:

0


            1. Move to the beginning of the next word:

w


            1. Move to the beginning of the previous word:

b


            1. Move to the beginning of the current word:

e


            1. Replace a single character with <x>

r<x>


            1. Move to first declaration of an item

gd


            1. Replace text (but prompt each time)
   :%s/old/new/gc


            1. Delete from the cursor to the next word
   dw
            1. Delete from the cursor to the end of the line
   d$


            1. Delete the current line
   cc


            1. Delete from the cursor to the next word
   ce


            1. Delete from the cursor to the end of the line
   c$


            1. Delete everything in single quotes
   ci'


            1. Delete everything in double quotes
   ci"


            1. Execute a shell command
   :! <command>


        1. Searching
            1. Conduct a search
   /


            1. Search backwards

Use ? instead of /


            1. Repeat the last search

Use ?? or //


            1. Move to next search result:
   n


            1. Move to previous search result:
   N


            1. Find matching parentheses or brackets
   %


        1. Tips & Tricks


            1. Go to a file

Highlight file and type gf


            1. Open a file in a new window
   CTRL-w,f


            1. Open a file in a new tab
   CTRL-w,gf


            1. Move to a specific line
   501G


            1. Run a command multiple times in command mode

Place a number in front of the command. For example:

   64dd


            1. Sort lines alphabetically:

1. v for visual select 2. use arrow keys to highlight lines 3. :sort ui


            1. Indent multiple lines:
   1. v for visual select
   2. user arrow keys to highlight lines
   3. type le 4 (to indent 4 spaces)


            1. Insert output of a command into the file
   `:r! pwd`


            1. Insert existing file
   `:r /etc/passwd`


            1. Useful submodules:
   $ git submodule add https://github.com/tpope/vim-pathogen.git vim/bundle/pathogen
   $ git submodule add https://github.com/scrooloose/syntastic vim/bundle/syntastic
   $ git submodule add https://github.com/scrooloose/nerdtree.git vim/bundle/nerdtree