Smarter searching
Although searching with vim would appear to be straight forward, what happens if you misspell a word, or are not completely sure how it is spelt? With the standard search you are stuck, however vim has a search mode called incremental search which starts searching as soon as you start typing the word you wish to find. This is incredibly useful but is not enabled by default. Luckily all you have to do is enter the following in the command window
:set incsearch
to enable incremental searching. Now when you enter search mode vim will start matching straight away and highlight each match that is found until you find what you are looking for. Once you have found what you wanted you can simply hit return or ESC to exit the search.
However there is still one issue is still outstanding even with incremental search enable, matching is still case sensitive. Therefore Match or match or matcH would need to be searched for separately. As luck would have it you can also setup vim to be non-case sensitive by enabling the following options in the command window
:set ignorecase
:set smartcase
Those coming from vi will recognise the ignorecase option but what is smartcase? Well smartcase, when set in conjunction with ignorecase, sets vim to search for any case unless uppercase is used in the search pattern. If this is the case then vim assumes that you want to perform a case-sensitive search. So this gives you options when you perform a search.
If you find these useful and want them permanently enabled you need to edit, or create, a .vimrc file in your home directory, and add the following:
set incsearch
set ignorecase
set smartcase
On most Linux system you should find the default vimrc file located in /etc. So if you are creating your own version it is usually best to copy the default version into your home directory, rename it to .vimrc and then edit.
Working with files
vim can be made to act like a file browser, simply enter:
:e .
Vim will now display the contents of the current directory, which you can transverse using the arrow keys or navigation keys. To open a file within vim you simply navigate to the file you wish to open and press enter. Directories can be traversed in the same manner by simply navigating to the directory you wish to open, pressing enter and you will be presented with the contents of the directory. Should the contents of a directory be large you can also perform usual vim searches.
Of course within vim you can open any files without using the file browser by simply entering the name of the file you wish to view. But what if you are not completely sure of the name? Well, vim has another trick up it’s sleave, file completion. This works much in the same way as shell file completion. You simply enter a few character of the file in question and press the tab key for vim for complete the filename. If there are multiple files that match the characters you have entered you simply press tab until you come to the file you want to edit and then press return.
File completion is fully configurable, this is vim after all, by modifying the wildmode parameter either within your vimrc file or within vim itself. To see all the possible settings enter
:help wildmode
within vim. By default this is set to full mode and acts as outlined previously.
The previous examples all work on filenames themselves, but what if you wanted to find a file, or files, that contain a common string? Obviously from the shell command line you would use grep to show you all the files than contain a common string, and you can do the same with vim. You simply run up vim, and then run:
:grep <string> *
and vim will find all files that contain the string and open them. You then move between the files using:
:cn – go to next file
:cp – go to previous
:cc – show current match
When you view each file vim starts at the location of the match in that file and you use :cc to go back to that match should you want to look at it again. You use :cn, :cp, :cc instead of the usual :n and :N as this is using Vim error handling and therefore vim stores the matching files differently.
Editing multiple files within one vim session
Vim, like vi, can edit multiple files at the sametime and use the :n (next) and :N (previous) keys to tranverse over these files. But you can also use split screen mode to edit files are well, by entering:
:split
Now the screen is split in half with a horizontal rule. You can use further split commands to split the screen into further segments. Then to move between the windows you use CTRL-W and the arrow key, up to go up a window down to go down a window.
This makes editing multiple files easy, especially if you want to view both files are the same time.
If splitting the screen is not to your liking, or you have multiple files you wish to edit vim version 7 or greater has a new feature which may help you out, tabbing. This works by adding a tab to, by default, the top of the screen with the file you are editing as the label. To use tabbing within vim you enter:
:tabnew <filename>
Don’t forget that you can also use the auto completion feature of vim when using the tabnew command. Vim will open this file is a new tabbed window ready for you to edit. Another, possibly quicker, method is to supply all the files you want to open at the shell command line using the -p flag:
vim -p file1 file2 file3 file4
To navigate between tabs you have the following commands:
gt – next tag
:tabn – next tag
:tabp – previous tag
:tabfirst – first tag
:tablast – last tag
As mention the tabs are displayed at the top of the screen. If you find this a distraction you can turn it off by running:
:set showtabline=0
And switch it back on using:
:set showtabline=2
If you prefer not to see the tabs at all you simply add the following to your .vimrc file
set showtabline=0
With tabbing display switched off there will of course be times when you want to remind yourself of all the tabs you have running. The following command provides such a summary:
:tabs
Vim will display a list of tabs and the file associated with it within the command window.
Lastly, one of the most using features of tabbing is the ability to perform an action on all of the tabs that are open via the tabdo command. As a simple example the following replaces the text fred with joe in all open tabs:
:tabdo %s/fred/joe/g
Spell checking
Purists looks away, but yes, vim now comes with a built in spell checking you can use. This is usually disabled by default, to enable spell checking enter the following:
:setlocal spell spelllang=en_gb
You would replace en_gb with language region code as you require. Vim will now highlight words that are incorrectly spelt in red, and rare or uncapitalised words in blue. To navigate the words vim thinks are incorrect you use the following common key combinations:
]s to go to the next misspelled word
[s to go the previous misspelled word
z= display suggestions for correct spelling
zg add word as a correctly spelt word
There are many other key combinations and options available for you to explore with vim spell checking, run
: help spell
to see the extended documentation vim provides.
Visual Mode
With vi if you wanted to edit a section of code or text within a file you would place the cursor on the last line that you wish to edit and run the following command to place a marker:
:ma
If you wanted to copy this section then you would place the cursor at the start of the section and enter:
:y’a
Which would yank all the code or text from the cursor to the marker ‘a’ you just placed. Then you would move the cursor to where you wanted to place the section and enter:
:p
With vim this all works fine, but there is a slightly quicker way of achieving this using vim’s visual mode. In visual mode you highlight the text you wish to edit and then type an editing command. There are three modes available, line by line, character and block mode. Line by line and character are probably the most useful but block mode may be handy if you need to edit text tables. These modes are available via the following key combinations:
SHIFT-v – line by line mode
v – character mode
CTRL-v – block mode
So now with vim’s visual mode, to copy a section of code, as per the vi example you would simply enter visual mode and highlight the code you wish to copy then run:
:y
To yank the highlighted code into an unnamed buffer, move the cursor to where you wish to locate the code and then run:
:p
To place a copy of the code there. Slightly simpler, and slightly quicker