Making an optimized configuration file
To allow for syntax highlighting, auto indentation, etc. in vim, open a new Terminal window and enter these commands to open vim’s configuration file:
cd /usr/share/vim
sudo vim vimrc
Press the ‘i‘ key to switch vim to Insertion Mode, then enter these lines below the ‘set backspace=2‘ line:
set ai " auto indenting
set ruler " show the cursor position
syntax on " enable syntax highlighting
set background=dark " make color suitable to dark background
set history=100
set hlsearch " highlight the last searched term
filetype plugin on " use the file type plugins
nmap <C-T> :tabprev<CR> " key mapping to move between files when open in tabs: Ctrl+T
nmap <C-B> :tabnext<CR> " Ctrl+B
" When editing a file, always jump to the last cursor position
autocmd BufReadPost *
\ if ! exists("g:leave_my_cursor_position_alone") |
\ if line("'\"") > 0 && line ("'\"") <= line("$") |
\ exe "normal g'\"" |
\ endif |
\ endif
Press the Escape key to take vim out of Insertion Mode, then press ‘:‘ (colon) followed by ‘x‘ to save the file and exit.
Open/Create a script file with vim, for example:
vim hello.php
You’ll see that vim now automatically indents code, displays the cursor position, has syntax highlighting, etc.
To open multiple files in tabs, replace vi with vim
You can add the following line into your .profile or .bash_profile :
alias vi="vim -p"
Alias allows us to change the way of calling vim, when typing vi, bash shell will replace vi command with vim -p. Therefore, we can open multiple files in tabs.
After saving the file, run
source ~/.profile
vi file1 file2 file3
As we already added key mappings in .vimrc, you can switch between files with 'Ctrl+T' and 'Ctrl+B'.
The :tabf command allows you to search for a file in your current path and open it in a new tab. For instance, if you want to open a file called inventory.txt that's in your current path, you could run:
:tabf inven*
That will search for a file that matches the string inven and any number of characters after it. If only one file is found, Vim will open it in a new tab. If you have several files that match, Vim will complain that too many files match, and you'll have to narrow the search a little. The :tabn command will do autocompletion of file names in your path, so you can just type the first few characters of a filename and hit Tab to find the right file.
Reference to this link for more: Vim tips: Using tabs
To open multiple files in splits
If you are already editing a file in vim and want to split the screen and edit a second one, you can use either of these commands:
:sp filename Splits the screen horizontally and opens filename in the new area.
:vsp filename Splits the screen vertically and opens filename in the new area.
Any split can be further split -- you can edit as many files in a single instance of vim as you like.
Use the following commands to move back and forth through your files and manage the size of the splits.
The prefix for all the below commands is <c-w> -- that is, hold down Ctrl and type w. Press <c-w> before typing any of the below commands.
<C-W> Moves to the next split.
k Moves to the split above the current split.
Up Moves to the split above the current split.
j Moves to the split below the current split.
Down Moves to the split below the current split.
h Moves to the split on left side of the current split.
l Moves to the split on right side of the current split.
+ Increases the size of the current split by one line. (Try combining this with counts -- e.g. 5<C-W>+ will make the split five lines bigger.)
- (Minus sign) Decreases the size of the current split by one line.
_ (Underscore) Maximize the current split (that is, make it take up as much room as possible.)
We can add the below key maps to .vimrc file to make working with splits easier:
map <C-J> <C-W>j<C-W> "go to the split below the current one
map <C-K> <C-W>k<C-W> "go to the split above the current one
map <C-H> <C-W>h<C-W> "go to the split on left of the current one
map <C-L> <C-W>l<C-W> "go to the split on right of the current one
Saving and quitting
When working with multiple files, each tab / split acts something like an individual instance of vim -- so :w and :q work on the current file. If you want those commands to apply to all the files instead of just the current one, add 'a' to them -- for instance, :wa saves all the open files, :qa quits all open files (and exits vim), and :wqa saves and then closes all open files.
No comments:
Post a Comment