Monday, September 19, 2016

Clone a Git Repository Into a Specific Folder

Let's take a look at a sample git repository URL:
https://github.com/apolunin/java8-impatient
It can be cloned via the following command:
git clone https://github.com/apolunin/java8-impatient
The contents of the repository will be put in the 'java8-impatient' folder which will be created in a current working directory. What if we want a different one? Fortunately this is very easy to do in Git. Here is a quick but useful tip which shows how to clone a Git repository into a different folder:
git clone https://github.com/apolunin/java8-impatient my-impatient-repo
This time the contents of the repository will be put in the 'my-impatient-repo' folder under the current working directory.

Change File Type in Vim

Recently I had to write a few lines of code in assembly language. Vim and GAS were chosen as suitable tools to use. I downloaded vim-gas plugin, installed it using vim-plug (Vim plugin manager that I prefer) and started coding. Unfortunately Vim detected file type as 'vmasm' by default, not as 'gas', so syntax highlighting was incorrect. In this post I describe several solutions to this problem.

Set File Type Manually

The first obvious solution which comes to mind is to set file type manually. This can be done via the following Vim command:
:set filetype=gas
This definitely works, but setting the file type manually every time it is opened seems awkward and impractical. Fortunately there are better alternatives.

Override File Type Using Auto Commands

There are several automatic commands (autocmd) which can be added to .vimrc in order to set file type:
au BufRead,BufNewFile *.s   setfiletype gas
au BufRead,BufNewFile *.s   set filetype=gas
The first option didn't work for me, because it is used to set the file type only if it hasn't been detected yet. As I mentioned earlier, in my case file type was detected as 'vmasm'.

The second option worked as expected and Vim detected the file type as 'gas'.

Override File Type Using Modelines

Another way to influence Vim filetype detection is to use modelines. Looks like the modelines are disabled by default. In order to turn them on the following lines should be added to .vimrc:
set modeline
set modelines=5
The first line enables modelines and the second one sets the max number of modelines which Vim takes into account. Look at the Vim docs for more details.

The following modeline can be added to the *.s file in order to help Vim to detect file type as 'gas':
/* vim: set filetype=gas: */
Apparently modelines approach requires adding a special line in every *.s file while autocmd approach can be configured solely in .vimrc. So I decided to stick to the latter which suits my needs just fine.

That's all there is to it.
Thanks for reading!