Today's Featured Neovim Tips & Tricks:expand_moreExternal FormattingeditingformattingshellVim's internal formatting features work great in many cases, but sometimes it can be helpful to send a buffer's content to an external program to perform the formatting, then replace the buffer contents with the result. For example, suppose we have the following JSON file in a buffer and want to sort the keys. Initial Conditions { "bananas": 12, "strawberries": 15, "apples": 34, "oranges": 24, "grapes": 33 } COMMAND Top 1:1 :%!jq -S . % We will demonstrate how to achieve this using a command-line mode command that calls jq , a popular command-line JSON processor. Obviously jq needs to be installed on a system for this to work. The command to execute is: :%!jq -S . % Let's break this down to review what is happening: To start, this follows the pattern: :[range]!{cmd} {args} where :% specifies the range as the entire file. Next, !{cmd} indicates that an external command is to be called with the specified range and arguments. In this case the external command to be called is: jq -S . % The details of this command are (mostly) outside the scope of Vim itself, but can be found by executing jq --help which indicates that the jq command is called with the format: jq [options] <jq filter> [file] where: [options] = -S (sort object keys) <jq filter> = . (pass all lines back to stdout) [file] = % (Vim inserts the current filename) Now, when we execute this command we get the following result: After External Formatting { "apples": 34, "bananas": 12, "grapes": 33, "oranges": 24, "strawberries": 15 } NORMAL Top 1:1 7 lines filtered which contains our sorted JSON object. Formatting a SelectionformattingStarting with the cursor in the middle of an un-formatted buffer : Initial Conditions <div> <p>line one</p> <p>line two</p> <p>line three</p> </div> NORMAL 50% 3:1 Enter visual mode : Enter Visual Mode <Esc> <div> <p>line one</p> <p>line two</p> <p>line three</p> </div> NORMAL 50% 3:1 Move down another row: Create the Selection v <div> <p>line one</p> <p>line two</p> <p>line three</p> </div> VISUAL 50% 3:1 and finally format the selected lines: Format Selection j <div> <p>line one</p> <p>line two</p> <p>line three</p> </div> VISUAL 67% 4:1 Note that Vim formatted only the lines contained within the selection (including lines containing the cursors), and didn't touch the other lines. Replacing with Register ContentseditingIt is a common task to search and replace all occurrences of a word within the current buffer. In some cases it can be convenient to use replacement text that is stored in a register. This tip demonstrates how to do so. Starting with the following buffer, note that we have already stored the replacement text in register A. Initial Conditions B eautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. NORMAL Top 1:1 :reg a Type Name Content l "a is way line 0: sourcing "/tmp/.mount_nvimEKK5Pg/usr/share/nvim/runtime/autoload/provider/clipboard.vim" finished sourcing /tmp/.mount_nvimEKK5Pg/usr/share/nvim/runtime/autoload/provider/clipboard.vim continuing in nvim_exec2() Executing command: "'/usr/bin/wl-paste' '--no-newline' '--primary'" Executing command: "'/usr/bin/wl-paste' '--no-newline'" Now, to execute the replacement, we follow the standard replacement pattern:, :s/{pattern}/{replacement} where the replacement text references the contents of a register. Although in NORMAL mode we access a register's contents using "{register} , the search is executed in COMMAND mode. We can access register contents in COMMAND mode as follows: Command Action C-R {regname} insert the contents of a register or object under the cursor as if typed In order to access the contents of register a , we would therefore use a replacement spec of <C-R> a Search and Replace B eautiful is way better than ugly. Explicit is way better than implicit. Simple is way better than complex. Complex is way better than complicated. Flat is way better than nested. S parse is way better than dense. Readability counts. NORMAL 75% 6:1 6 substitutions on 6 lines Executing this command replaces each occurrence of is with the contents of register a , is way . Replace Word Under CursoreditingIt is a common task to search and replace all occurrences of a word within the current buffer. For example, given the following buffer: Initial Conditions B eautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. NORMAL Top 1:1 Suppose one wants to change all occurrences of is better to is way better . The first step in the example if to move the cursor to the word we want to replace: w B eautiful i s better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. NORMAL Top 1:11 then press & to select all occurrences of the word under the cursor. In addition to selecting all occurrences of is , the cursor moves to the next occurrence. * Beautiful i s better than ugly. Explicit i s better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. NORMAL 25% 2:10 The general template for searching and replacing is: :[range]s/{pattern}/{replacement}/[flags] where Template Value Description : Puts Vim into Command Mode [range] % Applies this command to the entire file s/ Indicates that this is a search and replace operation {pattern}/ Not needed, since the words to replace are already selected. {replacement}/ is way Specifies the text to replace the search pattern with. [flags] No flags are specified. Putting this all together and executing the command: Beautiful is way better than ugly. Explicit i s way better than implicit. Simple is way better than complex. Complex is way better than complicated. Flat is way better than nested. S parse is way better than dense. Readability counts. NORMAL 75% 6:1 6 substitutions on 6 lines As expected, each occurrence of is has been replaced with is way .
External FormattingeditingformattingshellVim's internal formatting features work great in many cases, but sometimes it can be helpful to send a buffer's content to an external program to perform the formatting, then replace the buffer contents with the result. For example, suppose we have the following JSON file in a buffer and want to sort the keys. Initial Conditions { "bananas": 12, "strawberries": 15, "apples": 34, "oranges": 24, "grapes": 33 } COMMAND Top 1:1 :%!jq -S . % We will demonstrate how to achieve this using a command-line mode command that calls jq , a popular command-line JSON processor. Obviously jq needs to be installed on a system for this to work. The command to execute is: :%!jq -S . % Let's break this down to review what is happening: To start, this follows the pattern: :[range]!{cmd} {args} where :% specifies the range as the entire file. Next, !{cmd} indicates that an external command is to be called with the specified range and arguments. In this case the external command to be called is: jq -S . % The details of this command are (mostly) outside the scope of Vim itself, but can be found by executing jq --help which indicates that the jq command is called with the format: jq [options] <jq filter> [file] where: [options] = -S (sort object keys) <jq filter> = . (pass all lines back to stdout) [file] = % (Vim inserts the current filename) Now, when we execute this command we get the following result: After External Formatting { "apples": 34, "bananas": 12, "grapes": 33, "oranges": 24, "strawberries": 15 } NORMAL Top 1:1 7 lines filtered which contains our sorted JSON object.
Formatting a SelectionformattingStarting with the cursor in the middle of an un-formatted buffer : Initial Conditions <div> <p>line one</p> <p>line two</p> <p>line three</p> </div> NORMAL 50% 3:1 Enter visual mode : Enter Visual Mode <Esc> <div> <p>line one</p> <p>line two</p> <p>line three</p> </div> NORMAL 50% 3:1 Move down another row: Create the Selection v <div> <p>line one</p> <p>line two</p> <p>line three</p> </div> VISUAL 50% 3:1 and finally format the selected lines: Format Selection j <div> <p>line one</p> <p>line two</p> <p>line three</p> </div> VISUAL 67% 4:1 Note that Vim formatted only the lines contained within the selection (including lines containing the cursors), and didn't touch the other lines.
Replacing with Register ContentseditingIt is a common task to search and replace all occurrences of a word within the current buffer. In some cases it can be convenient to use replacement text that is stored in a register. This tip demonstrates how to do so. Starting with the following buffer, note that we have already stored the replacement text in register A. Initial Conditions B eautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. NORMAL Top 1:1 :reg a Type Name Content l "a is way line 0: sourcing "/tmp/.mount_nvimEKK5Pg/usr/share/nvim/runtime/autoload/provider/clipboard.vim" finished sourcing /tmp/.mount_nvimEKK5Pg/usr/share/nvim/runtime/autoload/provider/clipboard.vim continuing in nvim_exec2() Executing command: "'/usr/bin/wl-paste' '--no-newline' '--primary'" Executing command: "'/usr/bin/wl-paste' '--no-newline'" Now, to execute the replacement, we follow the standard replacement pattern:, :s/{pattern}/{replacement} where the replacement text references the contents of a register. Although in NORMAL mode we access a register's contents using "{register} , the search is executed in COMMAND mode. We can access register contents in COMMAND mode as follows: Command Action C-R {regname} insert the contents of a register or object under the cursor as if typed In order to access the contents of register a , we would therefore use a replacement spec of <C-R> a Search and Replace B eautiful is way better than ugly. Explicit is way better than implicit. Simple is way better than complex. Complex is way better than complicated. Flat is way better than nested. S parse is way better than dense. Readability counts. NORMAL 75% 6:1 6 substitutions on 6 lines Executing this command replaces each occurrence of is with the contents of register a , is way .
Replace Word Under CursoreditingIt is a common task to search and replace all occurrences of a word within the current buffer. For example, given the following buffer: Initial Conditions B eautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. NORMAL Top 1:1 Suppose one wants to change all occurrences of is better to is way better . The first step in the example if to move the cursor to the word we want to replace: w B eautiful i s better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. NORMAL Top 1:11 then press & to select all occurrences of the word under the cursor. In addition to selecting all occurrences of is , the cursor moves to the next occurrence. * Beautiful i s better than ugly. Explicit i s better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. NORMAL 25% 2:10 The general template for searching and replacing is: :[range]s/{pattern}/{replacement}/[flags] where Template Value Description : Puts Vim into Command Mode [range] % Applies this command to the entire file s/ Indicates that this is a search and replace operation {pattern}/ Not needed, since the words to replace are already selected. {replacement}/ is way Specifies the text to replace the search pattern with. [flags] No flags are specified. Putting this all together and executing the command: Beautiful is way better than ugly. Explicit i s way better than implicit. Simple is way better than complex. Complex is way better than complicated. Flat is way better than nested. S parse is way better than dense. Readability counts. NORMAL 75% 6:1 6 substitutions on 6 lines As expected, each occurrence of is has been replaced with is way .