Skip to content

VIM

search all ocurrences of <p> | <p attribute="content">

%g/^.*<p\(\|.*"\)">/

Add translate mark

%s/^.*<p\(\|.*"\)">/&{%trans%}/g

s-ptag
Result

Surround a pattern

For example change numeration to superscript numbers

Search pattern
g/\([1-4][1-4]\+\)/

search-pattern
Search For a Pattern

if already sure for the results then surround it

Surround pattern
s/\([1-4][1-4]\+\)/\\textsuperscript{\1}/g

surround-pattern
Surround Pattern

Append string

for append just add an &

Example for jinja

for check the regex

%g/\(css\/.*.css\)/
%s/\(css\/.*.css\)/{{ pathto('_static\/landing_page\/\1',1)}}/g

How this works just write the group number or string like :

g/(d{1,4})/

In Parenthesis () define the group d is for digits In brackers {1,4} are the range of numbers from 1 to 4

After that just escape all except the search pattern :

g/\(\d\{1,4\}\)/

Now you can search and edit the surrounded enviroment

%s/\(\d\{1,4\}\)/some Edit \1 around the pattern/g

Explanation:

Search numbers of fixed length say 5

/\d\{5\}

match 12345, 123456

As numbers more than 5 digits contain substring of 5 digits, they will be found too.

word boundary start

\&lt;

word boundary end

\&gt;

Then use below to search numbers of exact 5 digits

/\&lt;\d\{5\}\&gt;

match 12345 but not 123456

Use below to search numbers of 5 or more digits.

/\&lt;\d\{5,\}\&gt;

Use below to search numbers of 5 to 8 digits.

/\&lt;\d\{5,8\}\&gt;

Use below to search numbers of 8 or less digits.

/\&lt;\d\{,8\}\&gt;

Shortcut numbers of 1 or more digits

/\d\+

How to generate a number sequence in file using vi or Vim?

Starting with Vim 7.4.754 one can use g Ctrl-a, see :help v_g_CTRL-A

Go to line #4, use Ctrl-v to blockwise select the first character, go down 4 lines, press Shift i, enter 0 (this is 0, followed by Space) and Esc to exit insert mode.

Now use gv to re-select the previously selected area. Press g Ctrl-a to create a sequence.

I start with a 0 here, so I can re-select by gv. If you start with a 1, you need to re-select by hand while omitting the first 1.

Use 2g Ctrl-a to use a step count of 2.

indexing
Sequencial Pattern

Did you use a plugin or some trick to get the text of each key press to show up when you made that animated gif? – slm Tool is called screenkey and can be found on gitlab. – rkta

sudo apt install screenkey

Capitalize first letter of each word in a selection using Vim

You can use the following substitution:

s/\<./\u&/g
  • \< matches the start of a word

  • . matches the first character of a word

  • \u tells Vim to uppercase the following character in the substitution string (&)

  • & means substitute whatever was matched on the left-hand side

  • g means substitute all matches, not only the first

How to search for one of two similar strings in Vim?

/planet\(Awesome\|Terrible\)
/\<planet\(Awesome\|Terrible\)\>
  • \< marks the beginning of a ‘word’ (essentially alphanumerics)

  • \> marks the end of a ‘word’ (essentially alphanumerics)

Replace all characters in a regex match with the same character in Vim

%s/\v(hello)*/\=repeat('-',strlen(submatch(0)))/g

%s/^.\~\+$/\=repeat('-',strlen(submatch(0)))/g

How to Split Vim Workspace Vertically or Horizontally

linuxhandbook.com

Team LHB

6–7 minutes

As you get comfortable with the basics of Vim and start exploring it even more deeply, you discover new things.

New things like splitting the screen while using Vim in a terminal. No need of tmux or screen. Just do it under Vim, natively.

Yes, Vim allows you to have multiple horizontal or vertical splits in your active workspace.

Let me show you how to split Vim and all the necessary keyboard shortcuts to navigate between the split windows.

Create split window

Suppose you opened a file in Vim. Now you want to split the workspace into multiple windows for better productivity. Let’s go over how to create a split window in Vim.

There are two ways you can split a Vim workspace - horizontally and/or vertically.

You can know which window is active based on which window has your cursor.

Split window vertically

Assume that you have opened a file in Vim, and you want to split the screen vertically.

To make a vertical split, enter Normal mode, and run the following command:

creating a vertical split window in Vim

creating a vertical split window in Vim

If you specify a file path, it will open the file in the newly split window, otherwise, the newly split window will open the same file.

A shorter command to vsplit is vs (you can specify a file path with vs as well).

As an alternative way to create a vertical split, you can press the Ctrl + w key combination, and lastly, press the letter ” v ” (v for vertical split).

Split window horizontally

Vim allows you to split the window horizontally as well.

To make a horizontal split, enter Normal mode, and run the following command:

creating a horizontal split window in Vim

creating a horizontal split window in Vim

If you specify a file path, it will open the file in the newly split window, otherwise the newly split window will open the same file.

A shorter command to perform a horizontal split is to use the sp command. It also accepts a file path.

If you are wondering if any key combination exists for creating a horizontal split window, yes. It is ” Ctrl + w ” and then press the letter ” s “.

Close split windows

There are several ways you can close/quit an active split window.

  • :q[uit] - close the current window and the buffer as well

  • :bd[elete] - unload the current buffer and then close the current window

  • :on[ly] - close all other windows but leave all buffers open

closing an open window in Vim using “:q”

closing an open window in Vim using “:q”

Consider the following scenario, I have the Vim workspace divided into four quadrants.

Below are the four operations you can perform and the key combinations.

  • Move to the split window on the left: press Ctrl + w and press h

  • Move to the split window on the down: press Ctrl + w and press j

  • Move to the split window on the up: press Ctrl + w and press k

  • Move to the split window on the right: press Ctrl + w and press l

Resizing split windows

By default, Vim creates splits with a similar width/height. It is good for my OCD, but not really productive when I have a file that I edit most of the time and another file that I rarely edit.

So, let’s go over how you can resize the split windows in Vim.

Resize window

To resize a window, use either of the following methods:

  • Press Ctrl + w key combination [optionally specify a number] and then press the ” + ” (plus) symbol to increase the height of the current window

  • Press Ctrl + w key combination [optionally specify a number] and then press the ” - ” (minus) symbol to reduce the height of current window

  • Press Ctrl + w key combination [optionally specify a number] and then press the ” < ” (greater than) symbol to reduce the width __ of the current window

  • Press Ctrl + w key combination [optionally specify a number] and then press the ” > ” (less than) symbol to increase the width of the current window

resizing window’s width and/or height

resizing window’s width and/or height

Expand the window as much as possible

Below are the key combinations that you can press to expand a vertically split window vertically or a horizontally split window horizontally.

  • Expand vertically - press Ctrl + w and then press the pipe ” | ” character (the character which gets typed when you press the backslash key while holding down Shift)

  • Expand horizontally - press Ctrl + w and then press ” **_** “

expanding window’s height or width

expanding window’s height or width

Reset the size of all resized windows

To reset the size of all split windows, press Ctrl + w and then ” = “. This will resize all the windows and make them equal.

resetting size of all windows to make all even

resetting size of all windows to make all even

More tips to improve your workflow

Let’s go over some tips that I think you might find useful with a workflow that includes creating multiple split windows in a Vim workspace.

Open a new file/buffer

This will open the same file in newly split window. To open another file in new window, use either :e or :open command followed by the file name to make the window split actually usable.

Copy and paste

This is not actually a tip, but when you copy or cut something from a split window and move to another window and try to paste, it will work. No additional configuration is necessary.

Remap keys

If you look closely at navigating between active windows, it is similar to moving your cursor’s direction. Except, there is a need to press “Ctrl + w” every time and then press either h,j,k,l keys.

Below is what I personally use. Instead of pressing “Ctrl + w” and then pressing h,j,k,l keys, you can simply press “Ctrl + [h,j,k,l]”.

If you want to use these key combinations, add the following keys to your ‘vimrc’:

Splitting tweaks

By default, when you create a vertically split window, it will open to the left. To change this default behavior, add the following line to your ‘vimrc’.

Similarly, if you create a new horizontally split window, it will open on the topmost portion of the Vim workspace. To make a new horizontally split window open on the bottom of current window, add the following line to your ‘vimrc’.

Conclusion

This article covers how you can create one or more horizontal or vertical splits in an active Vim workspace. We also go over how you can resize split windows, close an active split window and how to navigate between open windows.

Empty Buffers Delete

Since it is not allowed to affect the buffer list with a :bufdo-argument command (see :help :bufdo), we have to use a more wordy yet fairly straightforward Vim script.

The function below enumerates all existing buffer numbers and deletes those that do not have a name (displayed as [No Name] in the interface) nor any unsaved changes. The latter condition is guaranteed through the invocation of the :bdelete command without the trailing ! sign, in which case modified buffers are skipped.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
function! DeleteEmptyBuffers()
    let [i, n; empty] = [1, bufnr('$')]
    while i <= n
        if bufexists(i) && bufname(i) == ''
            call add(empty, i)
        endif
        let i += 1
    endwhile
    if len(empty) > 0
        exe 'bdelete' join(empty)
    endif
endfunction

If you would like to delete empty buffers completely, including the unloaded ones, consider (with care!) replacing the exe ‘bdelete’ with exe ‘bwipeout’ (see :help :bd, :help :bw).

To test the contents of a buffer, use the getbufline() function. For instance, to be absolutely sure that the buffer contains no text in it, modify the if statement inside the while loop as follows:

if bufloaded(i) && bufname(i) == '' && getbufline(i, 1, 2) == ['']

Note that bufexists() is changed to bufloaded() here. It is necessary because it is possible to get the contents only of those buffers that are loaded; for unloaded buffers getbufline() returns an empty list regardless of their contents.

How to sort in vim | Linux Tutorials

How to sort in vim

Oct 16, 2021

Vim has built-in sorting command which we can use to sort text , also vim supports invoking external command ,thus we also can use system sort to do so, let’s see some examples.

How to sort in vim alphabetically

:sort

How to sort selected lines in vim

First we can enable line number using command :

:set number

Then to sort selected lines by specifying line number

:2,10 sort

This will sort 2nd line to 10th line.

How to sort descending in vim

:sort!

Note : there is no space between sort and !

How to do unique sort in vim

:sort u

How to sort in vim case-intensive

:sort i

Numeric sort in vim

:sort n

How to sort by column in vim

:%!sort -k2

This uses external system sort utility to sort all lines by the 2nd column , by default the field-separator is space.

To sort by column using specific field separator:

:%!sort -t ':' -k3

Here use : as field separator to sort by column number 3.

How to sort by line length in vim

:1,$  ! awk '{ print length(), $0 | "sort -n | cut -d\\  -f2-" }'
  • 1,$ stands for from 1st line the last line, you can specify your owns

You may also have interest to vim cheat sheet

` </sort-vim/>`__


Last update: Nov 20, 2024