Welcome to tui.ninja!


Today's Featured Neovim Tips & Tricks:

expand_more
Filed In:formatting

Starting 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 Selectionv <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 Selectionj <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.
Filed In:editingkeymap

Vim's built-in J command can be used to join the current line with the next line, which can be a very handy tool for keeping text "clean" when editing. Let's take a quick look at the default behavior. Starting from the following buffer , execute J : Before Before Default Line one Line two Line three NORMAL Top 1:6 After After DefaultJ Line one Line two Line three NORMAL Top 1:9 Note that the second line has merged with the first, but the cursor jumped after joining the lines, which is a bit disorienting. We would prefer to join lines without having the cursor move, so in this tip we will develop a simple keymap that achieved this. For comparison, here is a quick demo of the buffer before and after the improved keymap: Before Before Improved Line one Line two Line three NORMAL Top 1:6 After After Improved<A-j> Line one Line two Line three NORMAL Top 1:6 Let's set this up, step by step. Since the default J moves the cursor, our goal is to have the cursor return to the original location, which we can achieve by setting a mark to record the cursor position before we execute J . We can use any lower-case letter, let's use z: Set Markmz Line one Line two Line three NORMAL Top 1:6 Now that we have set the mark , execute the join by executing J : Join LinesJ Line one Line two Line three NORMAL Top 1:9 As we saw before, the cursor jumped. Now, we simply jump back to the mark we previously set using `z : Jump to Mark`z Line one Line two Line three NORMAL Top 1:6 Now that we have compiled the steps required to get the behavior we want, let's combine the steps into the following keymap: vim.keymap.set("n","<A-j>","mzJ`z") Note that for demonstration purposes this mapping sets the alternate keymap to A-J. If you prefer this functionality over the default, then you can also override the default keymap directly by using J instead of <A-J>.
Filed In:formatting

When editing text documents, paragraphs can often become split up into lines of varying lengths. For example, starting from the following text: Initial Conditions This is a single paragraph with lines that have been split up into different lengths. NORMAL Top 1:1 One solution is to combine Vim's text formatting command gw with the paragraph text object selector ip : gw{motion} tells Vim to format the lines that covered by {motion}, and the ip [[text object]] tells Vim to apply this formatting over the inner paragraph. Reflowgwip This is a single paragraph with lines that have been split up into different lengths. NORMAL Top 1:1 The lines of the paragraph are combined, then split at word boundaries to produce a sequence of lines with lengths that follow the width set by vim.opt.textwidth.
Filed In:editing

It 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 Beautiful 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 Beautiful 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: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 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 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 is way better than implicit. Simple is way better than complex. Complex is way better than complicated. Flat is way better than nested. Sparse 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.