Eli-Golin
10/9/2016 - 7:01 AM

Vim

Vim

x perator - Cutting the character uder the curso or hte selection of characters.
            Can be used by it self.
yy - Copies the entire line.
p - Pastes the content after the cursor's position.
P - Pastes the content before cursor's position.
i - Puts into insert mode after the cursor's position.
a - Puts into insert mode before the cursor's postion.
f - finds specified character to the right of current cursor's position.
t - Same as f but searches until the specified character.
o - Moves cursor to the next line and enters insert mode.
O - Moves cursor to the previous line and enters an insert mode.
s - Substtutes the character or a selection of characters.
S - Substitutes the entire line.
u - Undoes the last edit.
~ - Swaps character casing.
. - Repeats last insert edit.
dd- Deleted the current line.
D - Deletes from the cursor to the end of line.
gx - Opens a URL under your cursor in the web browser.
:edit! - Setting the buffer to the earliest saved state
* Allow us to dictate direction od the operator.

O - Moves cursor to the start of hte line.
$ - Moves cursor to the end of the line (inclusive new line).
g_- Moves cursosr to the end of hte line (exclusive new line).
b - Moves cursosr backword through each word.
e - Moves cursor to the end of the word.
w - Moves cursor to the start of the next word.
gg- Moves cursor to the start of hte buffer.
G - Moves cursor to the end of the buffer.
% - Moves cursor to the next bracket (or parenthesis).
( - Moves cursosr to the previous sentence.
) - Moves cursor to the next sentence.
{ - Moves cursor to the start of the paragraph.
} - Moves cursor to the end of the paragraph.
[( - Moves cursor to the previous available parenthesis.
]) - Moves cursor to the next available parenthesis.
[{ - Moves cursor to the previous available bracket.
]} - Moves cursor to the next available bracket.


* Altough usually operators and motions are used within the NORMAL mode, 
  they might have usage in COMMAND_LINE mode as well. In this case they might 
  have a different meaning.
  For example:
  % - In COMMAND-LINE mode refers to the entire file. So if used with d(delete)
      deletes the entire file: :%d
  . - The dot character in COMMAND-LINE mode refers to the current line number.
      So we can use it for example to delete from the current line to the start 
      of the file: :.,Od
All commands are made of count, operators, motions.

[count]{operator}{[count] motion|text object}

[] - mean optional value.
| - means you can use oen of he piped options.

* [count] - Indicates how many times the following operator executes.
  {motion | text object} - Either a "motion" or a "text object" can follow the {operator}
  and if it is a motoin it can also receive an optional count which informs Vim how many
  times the motion should be applied.
  
Count - An optional numerical value to indicate the number of execution.
Operator - The command to be executed.
Motion - The direction the operator should be applied.
Text Object - The area the operator should be applied to.
Previewing Files.

* One of VimL's features (Vim Language) is file name modifier.
  It takes the for, of %{:option}.
  % - Is an alias to the file name.
  There're many options available.
  The :p option - Converts the file name into fully quilified name.
  Check it this by haven a file opened in Vim and typing:
  :echo expand('%:p') - Will display in the Vim's command prompt the fully qulified 
  name of the current buffer's file.
   :!firefox %:p - Will open the current file in firefox browser.
   
Switching Working Directories.
When you open a file with Vim it can edit only file located under the current directory.
To switch working directory we can type:
:cd ~/Code/ProjectB - We have changed our working directory so Vim thinks we're
                      in ProjectB directory.
If we want to go back to the previous directory and it's path is long to type 
we can move to it's buffer by typing
:bp
and then execute
:cd %:p:h - cd (change directory) % - current file name :p - to fully qulified name :h - without the head (the file name itself).
Scratch Buffer - Buffer whose sole role is to be disposable.

To crate scratch buffer we have first to create a new buffer and then set 
some specific attributes that convert our buffer accordingly.

:set buftype=nofile (Changes the buffer so that Vim understands that it is not related to any file).
:set bufhidden=hide (When the buffer isn't visible in the window, not to uload it just hide it.)
:setlocal noswapfile (Tells Vim not to use swap file which are created by default to prevent data corruption).
You can inject content from other file with Vim.

The ":read" command accepts two arguments:
1. Set of options that allow us to specify the ile format and encoding (mostly not used)
2. A file path to the file to be loaded.

Examples:
1. If the current buffer is opened on file1.txt, then
   :read file2.txt  - will paste the whole content of file2.txt under the current
                      cursor's position on file1.txt
2. We can specify the actual position (line number) at which the content of the read file
   should have been pasted.
   :8 read file2.txt - Will paster th whole content of file2.txt after the 8th's
                       line of hte current open buffer.
3. We can paste the result of an execued script (it's stdout) into the current opened 
   buffer.
   :read !ls ~
   
dos eol - <cr><nl> or <nl>
mac eol - <cr>
unix eol - <nl>

Example of explicitely setting file's format.
:set fileformat=dos 
w - Write the buffer to it's source file.
:wq - Write the buffer to it's source file and quite.
:x - The same as :wq but only writes to buffer if modifications are made.
:5,9wq - Will only write the changes between lines 5-9, and then closes the buffer.

If multiple buffers were modified we have few options:
1. Quit Vim and ignore any modified buffers
   :qa!
2. Write all modified buffers and then quit
   :wqa
3. Write chnages to all modified buffers, but let you know of any problems before
   attempting to quit
   :conf wqa
4. Write all modified buffers and override even those that are read only.
   :wqa!
If you want to be able to move between unsaved buffers you should execute:
:set hidden or append ! to each command.

:bn (buffer next) - Moves us to the next buffer in the list.
:b5 - Will move us to buffere number 5.
:bp (buffer previous) - Moves us to the previous buffer in the list.
:ls - Lists all buffers.
:b# - Moves us qickly to the alternate buffer.
:bf (buffer first) - Takses us to the first buffer in the list.
:bl (buffer last) - Takes us to the last buffer in the list.
:bm (buffer modified) - Takes us to the next modified buffer.
When launching Vim:
vim file1.txt (open single file)
vim file1.txt file2.txt (open listed files)
vim *.txt (open with regular expression).

Once within Vim:
:e /path/to/your/file.txt

:e <C-d> - Listing all available directories/files from the current directory
           you were in prior launching Vim
:e <Tab> - Cycling through available items.
Vim provides us with few ways to create a new empty buffer.

:new - It will create a new empty buffer within a horizontal split window.
:enew - Also creates a new empty buffer but within the current window (without a split).
:vnew - It will create a new empty buffer within a vertical split window.
:tabnew - Will create a new empty buffer in a new tab.

Crating new Files from templates:
Vim provides the tools to let utilized custom templates based on the type file you happen 
to be working on.
You an load s static template file that you've created previously, and Vim will automatically
adjust hte 'look and feel' to the template type.
Afterwards you can start editing the new opened buffer.

:autocmd BufNewFile ~/test.html
Modes:

Esc - Switch between modes (Returning to "normal" mode)
1. Normal - The node you're in when vim started.
2. Insert (i) - Mode where we're able editting the content of hte buffer.
3. Visual (v) - Mode where we can select some content of our buffer for editting.
4. Replace (R) - Mode where we can write text above hte existing one, though replacing it.
5. Command-Line (:) - Executing Vim's commands.