% in command of vim

When % is used in a vim command, it is refer to the current file name.

For example, if my current file is a test.rb, to run it I can use:

: ! ruby %

For more info, run :help cmdline-special inside your vim.

Tags: vim

Breakindent Option in Vim

So, another vim post! Vim has a new option to set now that very little people talk about and it is the set breakindent option.

You know how we use set wrap? And the code just wraps into multiple lines? There’s a slight visual problem(for some people) there, whereby the 2nd line of wrapped at the 0 position of that line.

With set breakindent, the wrap will auto indent itself! The examples don’t make much sense as I can’t think of a code snippet to write at the moment, so, as ridiculous as it is, please ignore it.

Example without breakindent:

if (imagine_a_super_long_text........

Example with breakindent:

if (imagine_a_super_long_text.....

This is just a small visual issue most people can just turn a blind eye to, but if you’re interested, try it out!

Tags: vim

Opening URLs in vim

So, today I got curious as there was a file with multiple urls for me to choose from and I was thinking that using cmd+click on iTerm seems like a hassle..

I then learned of the gx keybinding which opens the URL under the current cursor! It is such a useful keybinding.

It uses the open shell command

An example inside example.html file:

<a href="www.google.com">Click Me!</a>

By having your cursor on the www.google.com, just press gx and it’ll use your default browser to open the URL! As simple as it can get!

Tags: vim

Escaping Bar in Vimrc

Today, I stumbled upon ix.io, a command-line pastebin. After installing the client, I wanted to map it in Vim as well to make it easy to share code snippets.

This tutorial recommended this mapping. The buffer or visual lines get piped into the external command ix. Everything works fine. However, Vim only outputs the pastebin link from ix until you press enter.

noremap <silent> <leader>i :w !ix<cr>

I wanted the link to be piped to pbcopy, so it will appear in my system clipboard. So, I did this instead.

noremap <silent> <leader>i :w !ix | pbcopy<cr>

Restarting Vim gave this error.

Error detected while processing /Users/marcus/.vimrc:
line  288:
E492: Not an editor command:  pbcopy<cr>
Press ENTER or type command to continue

It seems like I needed to escape the bar with <Bar> for it to work as intended.

noremap <silent> <leader>i :w !ix <Bar> pbcopy<cr>

See explanation here.

Tags: vim

Test String with Regex

You can test a string with regex in shell script like this:

email="[email protected]"
if [[ "$email" =~ "[a-z]+@[a-z]+\.com" ]]; then
    echo "Valid email"
    echo "Invalid email"

Tags: shell