I use vim a lot, but my workflow usually forces me to interact with other IDE's, so I am not a god-like vim poweruser, nor am I trying to be anytime soon. Vim is not an IDE for me, nor do I want it to be. It is a fast and light editor that doesn't get in my way.
I am looking for simplicity in both usage and config, not uber power or gold stars in efficiency.
I have been preloading several registers with my snippets up until now, and then keep a cheatsheet for which register has which text block. But I am running out of registers, and one single alpha-numeric letter does not immediately remind me of what I preloaded it with.
What is the easiest way to store any number of raw text blocks, and then retrieve them using a single keyword and 1 hotkey (in either order: keyword<>hotkey).
I don't need language aware snippets (clever keywords will be my solution)
I don't need snippets that are context aware or smart in any way
I don't plan on using a mini-template-language to streamline my snippets, thats excessive.
My snippets should literally paste in the raw text they were defined with, nothing else.
I guess I could just start writing named functions that merely print a block of text into the current buffer. Thats almost all I need, but I was hoping for a little easier way of maintaining/updating my snippet collection.
Are there any minimalist plugins out there that cover my use-case, or do I need to just stick with my .vimrc/python-commands approach?
Minor Update
I didn't realize you could chain so many letters with a mapleader, this may provide the magic I need. Then I will still have to alias back to some larger set of functions (probably use python to define commands).
If you want plugin-free solutions you can use:
abbreviations
:iabbrev obj var foo = {};<Left><Left>
:iabbrev func function foo() {<CR><CR>}<Up><Tab>
:iabbrev lipsum Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Integer diam augue, egestas quis, aliquam ut, venenatis ut, quam. Quisque ut augue. Integer non neque a lectus venenatis fermentum. Morbi quis eros nec elit molestie vehicula. Integer nunc lacus, sodales posuere, rutrum quis, blandit at, mi. Vivamus imperdiet wisi vel mauris. Morbi mattis ante non metus. Sed turpis dui, fermentum ut, aliquam eget, vulputate ullamcorper, pede. Nam non dolor. Etiam lobortis, urna id bibendum convallis, ligula augue auctor eros, a dictum sapien mi a tellus. Proin vel justo. Nunc malesuada turpis a sapien.
You can expand those with <Space> if you don't mind the trailing space or with <C-]> if you do.
See :help abbreviations.
insert mode mappings
You can create any complex mapping you want, either using <leader> or not.
The key (pun not intended), here, is just to choose a rarely used key and use it as a namespace for your mappings. On my French AZERTY keyboard, for example, I have the § key which is totally useless (not used in Vim, not used in French, not used in any programming language I work with). If I wanted to build a library of mappings I would use it as "leader" for those snippets:
:inoremap §obj var foo = {};<Left><Left>
:inoremap §func function foo() {<CR><CR>}<Up><Tab>
:inoremap §lipsum Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Integer diam augue, egestas quis, aliquam ut, venenatis ut, quam. Quisque ut augue. Integer non neque a lectus venenatis fermentum. Morbi quis eros nec elit molestie vehicula. Integer nunc lacus, sodales posuere, rutrum quis, blandit at, mi. Vivamus imperdiet wisi vel mauris. Morbi mattis ante non metus. Sed turpis dui, fermentum ut, aliquam eget, vulputate ullamcorper, pede. Nam non dolor. Etiam lobortis, urna id bibendum convallis, ligula augue auctor eros, a dictum sapien mi a tellus. Proin vel justo. Nunc malesuada turpis a sapien.
Whether you choose abbreviations or mappings, you can save all of them in a dedicated file:
~/.vim/snippets.vim
and source it in your ~/.vimrc:
runtime snippets.vim
If you decide to put that file somewhere outside your ~/.vim/ directory, you can source it with:
source ~/path/to/snippets.vim
edit
About <leader>…
<leader> is really not that special: you can generally think of it as a variable but, just like inserting $foo in a database will insert the value of $foo, registering <leader>something will register {current value of mapleader}something.
Supposing you create a custom <leader> mapping:
let mapleader = ","
map <leader>b :bnext<CR>
Vim registers ,b. If you decide to change you <leader> later in the current session:
:let mapleader = "%"
you'll still have ,b. Only further mappings will use the new <leader>:
map <leader>b :bnext<CR>
You get both ,b and %b.
<leader> means something only when you create a mapping. In usage, it's just ,b or %b.
Related
I want to append a character after any number followed by a dot in Vim using regex.
Example (append -):
1. Contrary to popular belief, Lorem Ipsum is not simply random text.
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.
4022. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Becomes:
1.- Contrary to popular belief, Lorem Ipsum is not simply random text.
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.
4022.- Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Please help.
:%s/\(\d\.\)/\1-/g
It changes this:
1. this 2. will
3_ do 4305. what 5.. you
ask 6*
to this:
1.- this 2.- will
3_ do 4305.- what 5.-. you
ask 6*
The way it works is to match and remember any digit \d followed by a dot \.
(the stuff between \( and \) is remembered for later use - these are called
capture groups). Then replace this with the remembered capture group using \1
and add a -. The g at the end makes happen for every match on each line.
Edit: I changed the title to more accurately present what I am asking for. The sentence immediately below should read "is it possible to make a pattern, such as '.*' treated as a single word for line wrapping purposes?"
Using vimwiki, is it possible to prevent a link from being word-wrapped?
Currently,
Etiam iaculis tempor dui, et mollis ligula accumsan et. Nam mattis cursus lorem sit amet accumsan. [Mauris elementum turpis eget](magna) ultrices molestie. Maecenas sagittis mauris id lacus consectetur, cursus mollis ex pretium. Quisque volutpat tellus ut augue fermentum ultricies eget eu metus. In lacinia sit amet massa ut feugiat.
becomes
Etiam iaculis tempor dui, et mollis
ligula accumsan et. Nam mattis cursus
lorem sit amet accumsan. [Mauris
elementum turpis eget](magna) ultrices
molestie. Maecenas sagittis mauris id
lacus consectetur, cursus mollis ex
pretium. Quisque volutpat tellus ut
augue fermentum ultricies eget eu metus.
In lacinia sit amet massa ut feugiat.
Note the Mauris elementum turpis eget link. The link markdown is broken when the text is reformatted.
Setting a longer textwidth won't solve the issue; I prefer the width I already have, and it will still happen. Link breaking will just happen at different points.
Changing the general word wrap settings won't work either ... they are just the way I like them, except in this one case.
As said in the comment, text-width does limit the column count of your file. You can't turn it on and off based on a line. It is exactly like you would write a small piece of paper, there is just no space left.
Of course, vim has different options to control where exactly it breaks the line (see h formatoptions and h fo-table) , but they aren't context-sensitive. And as soon as your text between [ and ] will be longer than 80 chars, you will have the problem again.
So what are the alternatives?
Don't use text-width but use wrap. This will not limit your column count at all. But will display the lines of text over multiple lines of screen space.
set colorcolumn=80 this does not do anything for you, but highlight the column, so you can wrap the text yourself.
Write a custom function, that does the wrapping for you.
I was looking at this problem the wrong way.
Vimwiki is not parsing markdown link text with multiple lines correctly, but wontfix at the moment.
So, the options described elsewhere in this question are the only choices at the moment.
I answered this myself because I wanted to include an explanation of what was going on and a pointer to the relevant issue.
Apologies in advance if this is the incorrect forum for this question - I am an absolute beginner when it comes to programming of any sort.
It seems that the Expand Selection to Word (Menu > Selection > Expand Selection to Word) functionality in ST3 has been assigned the same keyboard shortcut as the Quick Add Next (Menu > Find > Quick Add Next).
However, even if one clicks on these options manually (ie without making use of the keyboard shortcuts), the effect is the same: clicking on "Expand Selection to Word" simply finds the next matching strong and adds it to the current selection.
So now I'm wondering if I've just fundamentally misunderstood the intended purpose, even though the language seems unambiguous.
Could someone please confirm these functions should behave differently, and
If so, how can I fix the behaviour so that I can make use of the expand selection to word functionality?
Thank you in advance for you time and help!
The short answer to your question is that indeed both of these items are the same thing, placed in two different places and bound to the same key. The reason for that is that the command does multiple things, depending on the situation that you use it in, which makes it fit into both places.
For what it's worth, the operation of this particular command is commonly a stumbling block for newer users because there's a bit of subtlety to what it does (which is outlined below).
For verification that this is the same command;
Use Preferences > Key Bindings to open the key bindings window; in the left hand pane, search for find_under_expand; you see this particular key binding (on MacOS it uses super instead of ctrl) :
{ "keys": ["ctrl+d"], "command": "find_under_expand" },
Use View Package File from the Command Palette, then open Default/Main.sublime-menu and perform the same search; there are multiple hits for the same command in different menu items:
{ "command": "find_under_expand", "caption": "Expand Selection to Word" },
{ "command": "find_under_expand", "caption": "Quick Add Next" },
You can also open the console with View > Show Console, enter sublime.log_commands(True), then trigger the commands and see in the console that the same command is being executed regardless of which of the key bindings or menu commands you use.
Cycling back to the top, the command find_under_expand is often a stumbling block for newer users to Sublime because it does different things depending on the situation. This makes it quite powerful and useful, but can catch you unaware initially.
Specifically, there are a couple of things that the command might do:
If there is at least one caret that has no selection, and all carets with no selection are inside of a word, selection at every "empty" caret is expanded to cover the word under that caret. Thus, this command works as Expand Selection to Word in the menu.
If all carets are not "empty", and all selections contain the same text, the command looks for the next occurrence of the selected text and adds it to the selection by adding a new caret there and selecting it. Thus, this command works as Quick Add Next as well.
For example, here's the content of the standard lorem snippet:
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
If you put the caret in the first dolor on line 1 and hit the key, the text dolor will be selected. This is #1 triggering; the caret has no selection and is in a word, so the selection is expanded. Pressing the key a second time, the dolor in line 4 is added to the selection. This is #2 triggering; the next occurrence of the text is found and selected.
Now we can reset things by pressing Escape to go back to a single selection, and moving the cursor so that we're back to a single caret and no selected text.
Now put the caret in dolor and hit the key to select it again, as before. This time, use a Ctrl+Click on the word amet to add a second caret at that location. Now we have two carets, one selecting the text dolor and one inside of amet. If you hit the key now, you get #1 happening; the amet is added to the selection, but no matter how many more times you press it, the dolor on line 4 is not added to the selection. The #2 item above only triggers when all of the selections contain the same text.
Reset again as above, and this time manually select the word dolor on line 1 by using Shift along with your arrow keys. Pressing the key now may give you a bit of a surprise; part of the word dolore on line 2 is selected and added to the selection. Pressing a third time selects the dolor on line 4, and one more press gives you part of dolore on line 5 as well.
So clearly, this command does both things; it will expand the selection at the cursor out to the current word, but it will also try to find other instances of the selected text and add them as well.
The part of this that often messes with people's heads is that last example. As we saw in the first case, when we let Sublime select the word for us with the key press, continued presses ensure that only that exact text is found and added. However if you manually select the text first, the command selects all instances of the text, even if it's not a whole word.
This is incredibly powerful because you can for example easily rename variables by first expanding the selection to the variable name, then by selecting all instances and typing a new name. There's no worry that you might select partial variable names. However if you need to select partial words, that is available to you as well.
I'm aware of Vim plugins such as vim-markdown-toc, etc. but those apply to markdown files. Are there any approaches/suggestions for auto-generating a tables of contents in plain-text files (.txt, or no extension) in Vim? For example, something like:
CONTENTS
∙ Executive Summary
∙ Technical Review
∙ Biomedical Natural Language Processing
∙ Preliminary Work
... where (for convenience) headers could adopt the markdown syntax:
# EXECUTIVE SUMMARY
Lorem ipsum dolor sit amet, consectetur adipiscing elit. ...
...
# TECHNICAL REVIEW
Lorem ipsum dolor sit amet, consectetur adipiscing elit. ...
...
## BIOMEDICAL NATURAL LANGUAGE PROCESSING
Lorem ipsum dolor sit amet, consectetur adipiscing elit. ...
...
# PROPOSED WORK
Lorem ipsum dolor sit amet, consectetur adipiscing elit. ...
You could use something like that:
:g/^#* /t0
This will copy all this lines to the top. Unfortunaly in the wrong order, reverse it:
:0,4g/^/m0
(You may need to increase the numer 4 here, it is for your example). Then select the lines in visual line mode and replace # with tab or spaces.
:'<,'>s/#/ /g
Then do some formating. You could of course automate this further.
This isn't a programming question per se, since I'm aiming to use this for note-taking -- but there just isn't any community more reliable for in-depth vim knowledge than this one.
What I'd like to do is set a hanging indent in vim - you know, the kind that looks like this:
Lorem ipsum dolor sit amet consectetur
adipisicing elit neque amet fuga
dolores voluptatem aspernatur
explicabo quasi. Nostrum...
Except I know you can do this with
set formatoptions+=2
What I'd really like is for vim to automatically set the width of the indent based on the presence of a special character in the first line. For instance, I might want to set this special character to ":" to format my text like so:
Lorem: Ipsum porro dolorem nostrum
incidunt similique a? Eaque
minus aliquid dolorem veritatis
omnis odit.
Quidem: distinctio quibusdam distinctio
accusamus alias magnam.
Voluptatem: dignissimos exercitationem
deleniti aliquam ratione?
Necessitatibus expedita
praesentium.
In my ideal scenario, this would also be compatible with vim's breakindent setting, which visually indents soft-wrapped lines (officially included in vim as of patch 7.4.338).
Does anyone know if this is possible - or even if this functionality can be achieved with a plugin?