vim

Numbered Marks in Vim

Numbered marks are '0 to '9. Unlike normal marks, these cannot be set directly. Instead, '0 refers to the cursor position before Vim was exited. '1 returns the cursor position during the Vim session before that, and so on.

Read more about marks.

Tags: vim

Info on Viminfo

The .viminfo file is used by Vim to keep track of persistent information across sessions. This includes things like command like history, registers and global marks.

Sometimes, when you find yourself running Vim in multiple sessions at the same time, these information might not be synchronised properly. This is because Vim only reads and writes .viminfo when it launches and quits respectively. To manually read and write .viminfo, use :rviminfo and :wviminfo.

A good use case is global marks. When you save a global mark in Vim Session #1, you won’t be able to jump to it in Vim Session #2 yet. To solve the problem, we do :wviminfo in Vim Session #1 to force Vim to record the new global mark. Then, we do :rviminfo in Vim Session #2 to allow Vim to register the new global mark.

Tags: vim

Effective Cursor Moving in Vim

Aside from the usual w, we can use W to navigate between words.

For instance, it would take a lot repetitive w keys to reach &&:

dispatch(GeosActions.getSegmentationRequest(request)) && ...
-
dispatch(GeosActions.getSegmentationRequest(request)) && ...
        -
dispatch(GeosActions.getSegmentationRequest(request)) && ...
         -
dispatch(GeosActions.getSegmentationRequest(request)) && ...
                    -

However, it only takes one W key to reach it:

dispatch(GeosActions.getSegmentationRequest(request)) && ...
_
dispatch(GeosActions.getSegmentationRequest(request)) && ...
                                                      _

The uppercase versions of e and b are also useful.

See all the right moves.

Tags: vim

Difference of ^ and _ in Vim

In vim, both ^ and _ can be used to move to the first non-blank character of the line.

However, ^ doesn’t work with motion. This mean that 3^ doesn’t move to the non-blank character of the third line. It ignore the motion and only move to the non-blank character of the first line.

Consider that your cursor is at this position:

* This is the first line
            -
* This is the second line
* This is the third line

with 3^, the cursor will move to the first non-blank character of the first line:

* This is the first line
-
* This is the second line
* This is the third line

However, with 3_, since it works with motion, the cursor will move to the first non-blank character of the third line:

* This is the first line
* This is the second line
* This is the third line
-

For more details, refer to :help _ and :help ^ when using vim.

Tags: vim

Checking for Vim Shell

Sometimes, it’s easy to forget when you’re using :sh from Vim. One way to check whether the current shell was started from Vim is the $VIMRUNTIME environment variable.

Normal shell:

$ echo $VIMRUNTIME
 

Shell from Vim:

$ echo $VIMRUNTIME
/usr/local/share/vim/vim81

See original question here.

Vim Motions for HTML Tags

When working with HTML tags in Vim, it and at can be quite useful. it means “inner tag block”, while at means “a tag block”.

To change “Hello World” to “Goodbye”, we just need to position our anywhere in the <p> tag, and do cit then Goodbye:

<div>
  <p>Hello World</p>
   ^ (citGoodbye)
</div>
---
<div>
  <p>Goodbye</p>
</div>

To delete the entire <div> block, we can position our cursor on the <div> tag, and do dat:

<div>
^ (dat)
  <p>Goodbye</p>
</div>
---

Comma in Vim

,           Repeat latest f, t, F or T in opposite direction
            [count] times. See also |cpo-;|

Tags: vim

Tabs in Vim

In Vim, a buffer is a file in memory. A window is a viewport of buffer. A tab is a layout of windows. Oftentimes, you would find yourself splitting the screen into multiple windows for several buffers.

However, this screen is just one tab page. To preserve the window layout, we can do :tabnew or :tabe <file>. This will create a new tab page for us, leaving the first tab page and its windows intact. You can also use C-w T on any existing window to move it to a new tab. Use gt or :tabn to navigate to the next tab page, and gT or :tabp to go back to the previous tab page.

Tabs are useful when you need to do something out of context. For instance, while you code in the first tab, you open a new tab for help with :tab help ,. You could also dedicate a tab just for diff viewing.

N%

Use 50% to go to the halfway point of the file.

See :h N% for the entry in Vim’s help.

Tags: vim

Opening URLs in Vim

vim https://www.chunkhang.com/

Tags: vim

Vi Mode in any shell (Zsh, Fish and even Bash!)

VI Mode in any shell

Assuming that you love using VIM and the terminal, here’s a cool mode that can be activated on Zsh or any shell rather. (I am using Zsh, so, I will talk about it)

Enable vim mode on Zsh by adding this line into your .zshrc.
bindkey -v
Basically this means to enable vim mode

When this is enabled, refresh your shell and you can insert texts after pressing i and if you want to run any text-object related moves, just press Esc and the normal hjkl, b, w, ciw, diw, ^, 0, $ will work! Exactly like in vim!

Help Grep

Aside from the usual :help command, you can also use :helpgrep to search for lines containing a certain pattern.

See :help helpgrep to read up on how to navigate through all matches.

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

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

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........
....still_continuing....)

Example with breakindent:

if (imagine_a_super_long_text.....
    ....still_continuing...)

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

% 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

Go to File in Vim

Even if you have tags configured, sometimes, ctrl-] might not bring you to the source of the keyword. So, it might be useful to use gf to go to the file directly.

import HeaderActions from '../../redux/HeaderRedux'
                                  ^ (gf)

Tags: vim

Vim onoremap option

Alright guys, back to another feature that vim has to offer! Recently, I’ve been using :vert term <command> for some short commands to run instead of using :sh command and found that they have a mapping known as tnoremap which basically stands for Terminal Normal Remap.

I dived into the help for tnoremap and found a much more awesome option vim had which was onoremap!

It basically allows vimmers to define their own test objects, as an example: instead of doing:

nnoremap ciq ci'

we can do:

onoremap iq i'

The onoremap automatically makes it so that you can run diq, ciq, yiq and so on! Super fluent way of having a shortcut for the quote/'! It basically allows you to self-define your text objects which is kind of sweet!

In my .vimrc right now, I have:

onoremap iq i'
onoremap iQ i"
onoremap aq a'
onoremap aQ a"
onoremap , t,

This brings a whole lot of new functionalities that I never knew before this! so, d, gives the command of dt, or y, gives yt,

It is a little bit useful but overall the quote onoremap is the best use I have of it so far!

Try it out and let me know your onoremaps!

Tags: vim

Deleting Words in Insert Mode

While typing in Insert Mode, you can use Ctrl-W to delete one word to the left of the cursor, or use Ctrl-U to delete everything to the left.

hello world and goodbye
           ^ (Ctrl-W)
hello and goodbye
hello world and goodbye
               ^ (Ctrl-U)
goodbye

Tags: vim

Non-Greedy Matching in Vim

Suppose we have the following text in Vim:

<span>Hello World</span>

Using /<.*> to search would match not only both <span> and </span> tags, but the words inside them as well, because .* is greedy. To only match the tags alone, we need to use /<.\{-}>. See :help non-greedy for the documentation on .\{-}.

Tags: vim

Formatting JSON in Vim

You probably know that we can use something like :%!python -m json.tool to format a JSON file that’s opened in Vim. However, I always find this command hard to recall. Instead of dedicating a mapping for this, we can actually rely on equalprg instead. Let’s set this autocmd in .vimrc:

augroup vimrc
  autocmd!
  autocmd FileType json setlocal equalprg=python\ -m\ json.tool
augroup END

Now, when we open a JSON file, we can use the familiar = key to format it nicely.