Today's Featured Neovim Tips & Tricks:expand_moreApplying a Normal-mode Command to a RangeeditingformattingNormal mode commands can be used to apply an operation over each line in a range . Suppose you are creating a list in python by yanking and pasting some content from another buffer . After pasting, the items are in place but are missing the trailing , required by python: Initial Conditions a = [ "one" "two" "three" ] NORMAL 33% 2:1 These could be manually added in various ways, but Vim's :normal command provides a handy solution by allowing a normal mode command to be applied to each line in a range. To demonstrate, we first create a visual selection: Select Visual Range v 2j a = [ "one" "two" "three" ] VISUAL 67% 4:1 Note that the range could have also been entered manually. Next create the command to execute. We would like to move the cursor to the end of each line, enter insert mode after the line, then enter the , character. Inserting after the line can be achieve with the A command, so the command to execute for each line is: A, and the complete command is: :normal A, Executing this command in our buffer provides the desired result: Execute the Command a = [ "one", "two", "three" , ] NORMAL 67% 4:12Insert a REST API ResponseeditingshellNeovim registers and command-line mode can be combined to a pretty decent REST API client. This tip shows how. First, open a new buffer and add the API request: Initial Conditions h ttps://jsonplaceholder.typicode.com/todos/1 NORMAL Top 1:1 Now, yank the current line, which places the contents of the current line into register 0 : Yank the API Request String yy h ttps://jsonplaceholder.typicode.com/todos/1 NORMAL Top 1:1 Finally, lets build the command to call the API and insert the response. First, we learned previously that we can use the :read command to call a shell command and insert its output. The call signature is: :read !{cmd} [args] In this case we want to call a REST API, so lets use curl to call the API and receive the response. The command to achieve is: curl -s '{request}' where the -s option tells curl to operate in "silent mode". Finally, we could hard-code the request into the command, but we have already yanked it to register 0 . We can retrieve register contents in command-line mode using C-R followed by the register name, so our complete command is: ":read !curl -s '<C−r>0'" Executing this command produces: Insert the API response h ttps://jsonplaceholder.typicode.com/todos/1 { "userId": 1, "id": 1, "title": "delectus aut autem", "completed": false } NORMAL 88% 7:1 6 more lines if you find yourself using this often, consider creating a macro to save yourself a few keystrokes. Inserting Unicode CharacterseditingInserting unicode characters into a document can be cumbersome, and for many the default process is to do a quick web search to find the desired character, then copy and paste it into the document. While Neovim can't make it easier to remember all of the code points, it does make the process of inserting it into a document slightly easier. Suppose we are shopping with a simple shopping list, and just bought coffee so we want to update the shopping list. Let's move the cursor into position then check that line off: Initial Conditions ✕ milk ✕ coffee ✕ eggs NORMAL 50% 2:1 The first step is to type s (substitute), to delete the current character and enter insert mode . Start a substitution s ✕ milk coffee ✕ eggs INSERT 50% 2:1 Next, we hit C-V , followed by u and the Unicode code point we want to insert. Insert the Unicode <C-v> u2713 ✕ milk ✓ coffee ✕ eggs INSERT 50% 2:2 Finally, hit Esc to return to normal mode : Final Result <Esc> ✕ milk ✓ coffee ✕ eggs NORMAL 50% 2:1Prepend to Lines in SelectioneditingOccasionally while editing a document the need to prepend text to a sequence of lines comes up. For example, suppose you want to comment out some lines in a file by prepending each line with a "#" character. A simple way to achieve this is to leverage normal mode commands to apply an operation over each line in a range . To demonstrate, let's start with the following file: Initial Conditions o ne two three four five six NORMAL Top 1:1 First let move the cursor down a few lines and make a visual selection : Make Selection 2jV2j o ne two three four f ive six V-LINE 71% 5:1 Now that we have selected the lines to modify, let's build our command. First, hit : to enter command-line mode then enter normal to indicate that this command should execute in normal-mode. This tells Neovim that the following command should be executed once for each line in the selection. Finally, we type the same keystrokes that we would execute in normal mode to achieve the desired changes. In this case, we want to execute I to move the cursor to the beginning of each line and enter insert mode , then # to insert this character. :norm I# Before Executing Command one two three four f ive six COMMAND 71% 5:1 :normal I# After executing this command, we can confirm that a "#" has been inserted at the start of each line in the selection. After Executing Command one two #three #four # five six NORMAL 71% 5:1
Applying a Normal-mode Command to a RangeeditingformattingNormal mode commands can be used to apply an operation over each line in a range . Suppose you are creating a list in python by yanking and pasting some content from another buffer . After pasting, the items are in place but are missing the trailing , required by python: Initial Conditions a = [ "one" "two" "three" ] NORMAL 33% 2:1 These could be manually added in various ways, but Vim's :normal command provides a handy solution by allowing a normal mode command to be applied to each line in a range. To demonstrate, we first create a visual selection: Select Visual Range v 2j a = [ "one" "two" "three" ] VISUAL 67% 4:1 Note that the range could have also been entered manually. Next create the command to execute. We would like to move the cursor to the end of each line, enter insert mode after the line, then enter the , character. Inserting after the line can be achieve with the A command, so the command to execute for each line is: A, and the complete command is: :normal A, Executing this command in our buffer provides the desired result: Execute the Command a = [ "one", "two", "three" , ] NORMAL 67% 4:12
Insert a REST API ResponseeditingshellNeovim registers and command-line mode can be combined to a pretty decent REST API client. This tip shows how. First, open a new buffer and add the API request: Initial Conditions h ttps://jsonplaceholder.typicode.com/todos/1 NORMAL Top 1:1 Now, yank the current line, which places the contents of the current line into register 0 : Yank the API Request String yy h ttps://jsonplaceholder.typicode.com/todos/1 NORMAL Top 1:1 Finally, lets build the command to call the API and insert the response. First, we learned previously that we can use the :read command to call a shell command and insert its output. The call signature is: :read !{cmd} [args] In this case we want to call a REST API, so lets use curl to call the API and receive the response. The command to achieve is: curl -s '{request}' where the -s option tells curl to operate in "silent mode". Finally, we could hard-code the request into the command, but we have already yanked it to register 0 . We can retrieve register contents in command-line mode using C-R followed by the register name, so our complete command is: ":read !curl -s '<C−r>0'" Executing this command produces: Insert the API response h ttps://jsonplaceholder.typicode.com/todos/1 { "userId": 1, "id": 1, "title": "delectus aut autem", "completed": false } NORMAL 88% 7:1 6 more lines if you find yourself using this often, consider creating a macro to save yourself a few keystrokes.
Inserting Unicode CharacterseditingInserting unicode characters into a document can be cumbersome, and for many the default process is to do a quick web search to find the desired character, then copy and paste it into the document. While Neovim can't make it easier to remember all of the code points, it does make the process of inserting it into a document slightly easier. Suppose we are shopping with a simple shopping list, and just bought coffee so we want to update the shopping list. Let's move the cursor into position then check that line off: Initial Conditions ✕ milk ✕ coffee ✕ eggs NORMAL 50% 2:1 The first step is to type s (substitute), to delete the current character and enter insert mode . Start a substitution s ✕ milk coffee ✕ eggs INSERT 50% 2:1 Next, we hit C-V , followed by u and the Unicode code point we want to insert. Insert the Unicode <C-v> u2713 ✕ milk ✓ coffee ✕ eggs INSERT 50% 2:2 Finally, hit Esc to return to normal mode : Final Result <Esc> ✕ milk ✓ coffee ✕ eggs NORMAL 50% 2:1
Prepend to Lines in SelectioneditingOccasionally while editing a document the need to prepend text to a sequence of lines comes up. For example, suppose you want to comment out some lines in a file by prepending each line with a "#" character. A simple way to achieve this is to leverage normal mode commands to apply an operation over each line in a range . To demonstrate, let's start with the following file: Initial Conditions o ne two three four five six NORMAL Top 1:1 First let move the cursor down a few lines and make a visual selection : Make Selection 2jV2j o ne two three four f ive six V-LINE 71% 5:1 Now that we have selected the lines to modify, let's build our command. First, hit : to enter command-line mode then enter normal to indicate that this command should execute in normal-mode. This tells Neovim that the following command should be executed once for each line in the selection. Finally, we type the same keystrokes that we would execute in normal mode to achieve the desired changes. In this case, we want to execute I to move the cursor to the beginning of each line and enter insert mode , then # to insert this character. :norm I# Before Executing Command one two three four f ive six COMMAND 71% 5:1 :normal I# After executing this command, we can confirm that a "#" has been inserted at the start of each line in the selection. After Executing Command one two #three #four # five six NORMAL 71% 5:1