Posts tagged with “vim”

How to: move the cursor to the first non-whitespace (non-blank) character on the current line in Vim

  1. use:^ (shift + 6)

This moves the cursor to the first non-blank character of the current line.

  1. _ (underscore)

This also moves the cursor to the first non-blank character on the same line the cursor is on.

By the way, the 0 command moves to the absolute start of the line, including any leading whitespace.

How to do a case insensitive search in vim

/\cYourSearchKeywords case insensitive /\CYourSearchKeywords case sensitive, which is the default behaviour

A few other ideas:

  • \c can appear anywhere in the pattern, so if you type a pattern and then decide you wanted a case-insensitive search, just add a \c at the end.

  • add set ignorecase for case-insensitive searching in my vimrc, and I can use \C to do a case-sensitive search

  • There's also set smartcase which will automatically switch to a case-sensitive search if you use any capital letters

    • Remember! set smartcase applies only when set ignorecase is already active

Reference

How to use vim macro to speed up your complex find/replace operations in vim / ideavim

Yesterday, I need to replace a string ClassName with List<ClassName> many times in a few files, and the ClassName varies. Manually doing it again and again is tedious and mistake prone, which is what I hate. Here's the solution

First, record a macro,

  1. move cursor on any letter of the target ClassName
  2. qa
  3. ysiw>
  4. Insert
  5. List
  6. Esc
  7. q

Second, replay the macro, move cursor to another occurrence of ClassName,

@a

Third, replay the same macro as many times as you want: move cursor to any other occurrences, simply type

@@

The feeling is so good to let a computer do what you want it to do!

and here's the Reference where I learnt how to use vim macro. TL;DR;

Don't change the inode when editing a file with Vim

I keep my config files on github, and I use hard-link for most of the config files. It's convenient, as I can check the new changes easily and submit some of the changes when necessary. However, the default behavior of vim troubles me. It always changed the inode when I save the config file!

tldr; the solution is: put the following line in your .vimrc

set backupcopy=yes

PS Though this way fixed Vim's behavior, I sadly found that git pull will change the config file's inode as well. So there is actually no feasible solution. I have abandoned this hard link approach. If you have better solutions to maintain all your config files in one repository, please let me know!

Rider Terminal tips

  1. Stop Leaving the Terminal when you press the Escape key. It is really annoying when you are a vim fan like me! here's the solution

Go to "Settings | Tools | Terminal" and click "Configure terminal keybindings".

Find "Plug-ins | Terminal | Switch Focus To Editor" action and change its keyboard shortcut (by default "Escape") via the context menu.
Keybindings are IDE-wide, so there is no need to change them for each project.

  1. Using msys2 bash as the embedded terminal: at Settings > Tools > Terminal
Environment Variables: CHERE_INVOKING=1
Shell path: c:\msys64\usr\bin\bash.exe --login

Unfortunately, the environment variables above work only on the current project; It is definitely a tedious process that you have to repeatedly set it up for every project. So I eventually found a better way to achieve the same goal without setting up the Environment variables in Rider. Here is the answer Reference:

Shell path:  C:\msys64\msys2_shell.cmd -defterm -here -no-start

TBC...