There's probably a better way to do this, but this is what I'm trying to do. I have several html files for which I need to update meta tags prior to updating the content. I'm trying to automate this by using a keyboard mapping similar to what follows:
nnoremap <leader>mt /<meta name="developer"<cr>f";;lct"$username
This seems to work with the exception of the $username portion. How can I make the mapping evaluate the variable?
I do have a few tags to update per page, but don't know how else to do this. If someone has a better method, I'd be more than happy to hear it. The content that I'm searching through is similar to the following:
<meta name="owner" content="someowner">
<meta name="developer" content="somedev">
<meta name="date" content="2012-07-26">
<meta name="expires" content="2013-07-26">
The date would be the date that I opened the file to edit, while the expires would be one year from the date
NOTE: This is gvim on Windows if that changes things.
$username does refer to the environment variable username, but to use this in a mapping, you have to stick to the (somewhat unintuitive, but perfectly consistent) interpretation rules of Vim.
The ct" mean change until double-quote, so what follows is in insert mode. To insert an environment variable (or any other Vim expression), use <C-R>=:
nnoremap <leader>mt /<meta name="developer"<cr>f";;lct"<C-R>=$username<CR>
This will evaluate the environment variable on each mapping invocation. (And stay in insert mode; append <Esc> to go back to normal mode!) Alternatively, you could also bake the value into the mapping itself:
execute 'nnoremap <leader>mt /<meta name="developer"<cr>f";;lct"' . $username
I don't know if this is the best solution, but for the user name on a unix machine you can call the 'whoami' command as external command from vim and store it's return value in a variable for later reuse
For the dates, there is a strftime() function in vim. You could add an auto command for the VimEnter event and store the time/date at that moment in a variable too, to reuse it later.
Related
As soon as I try to access a folder/file containing an emoji in its name from my Lua 5.2 script, for example like this:
os.execute('start "" "' .. path .. "\\scripts\\menu\\📄 My Scripts" .. '"')
The Windows' Command Prompt simply refuses to open it with the following error message:
I'm aware Windows' Command Prompt doesn't support emojis and therefore is not possible to make it work just like that, but my doubt is if won't exist some workaround or whatever I can do to ensure any Windows/Unix user is going to able to get the folder open by my Lua script without any problem.
I have tried i.e. things like use the codes instead (1246 and U+1F4F0 in this page facing up case) without success. Couldn't I for example simply use some kind of "wildcard" instead? I mean, knowing it's always going to be the very first character in the name. Or, well, any other ideas will be welcomed, cause nothing I'm trying really seems to work...
Of course if it's going to represent any problem I'll simply refuse to use them, but it came in handy for some "first sight" folder distinction and, if possible, I'd like to can count this little visual resource 🙄
This is a Problem about how the string is constructed.
I found only one solution with [[command "path"]] (on Windows 11 and Lua 5.3)...
os.execute([[start ]] .. path .. [["\scripts\menu\📄 My Scripts"]])
-- My Testpath is/was: os.execute([[dir "%localappdata%\nvim\📄 Lua"]])
...the long string ([[]]) will not be interpreted (coercionated) by Lua.
That also have the side effect that you can use single backslashs with that kind of string.
Environment variable expansion (e.g. Windows: %localappdata%) only works inside doublequotes.
Single quotes instead ([[command '%localappdate%\path\']]) will not work (expanded).
os.execute accepts only ANSI-encoded strings (win-1252 in European Windows), but it is unable to encode an emoji.
Hint: you can create .bat-file to do the task for you and invoke it from Lua with os.execute.
I am using the oh-my-zsh theme jtriley. The theme code, reproduced below, displays the entire directory path:
PROMPT="%{$fg_bold[cyan]%}%T%{$fg_bold[green]%} %{$fg_bold[green]%}%d
%{$fg_bold[yellow]%}%% %{$reset_color%}"
I know that there are ways to change the displayed directories, using the prompt_dir() function, following instructions like these. However, when I insert this below the code above, I do not get a change.
How can I alter a theme file where the only code in it is the prompt, and its colors?
For this particular use case, you can just replace %d with %2d in the first line.
However, if you want to do something fancier, you can replace it with \$(prompt_dir) instead, and set the PROMPT_SUBST option. The backslash is important, since that makes it so the function is re-evaluated each time the prompt is displayed, rather than just when $PROMPT is assigned.
I'm using Gvim in windows.
Normally, when we type some character then press Ctrl-n, vim will show some tag, but those tags just includes words which have been pre-typed in the current file.
Now, I need it working in a new language, and show the tag which has been defined in other files.
So, I create a new \\.ctags for this new language, and generate tags file by exuberant-ctags.
I can choose a function in current file, then press Ctrl-] to jump to the function definition, but this function was define in the other files. It is working very well.
I don't know how to make it show the tags which are generated by ctags when I type some character.
Please help me. Thanks very much.
My English is poor, I hope you can understand what I said.
CTRL-N is just the default completion (which completes from a variety of sources, including the open buffers and also the tags database). There are many more specialized completions (all starting with CTRL-X), among them tags completion, triggered via CTRL-X CTRL-], see :help i_CTRL-X_CTRL-]. If you've correctly configured the 'tags' option (so your tags database is found) and tags jumps do work, just start using that.
Some languages / filetypes also define a language-specific completion (for language keywords etc.), usually via the 'omnifunc' option and triggered by CTRL-X CTRL-O. You could write such yourself, too.
I want to try the following things in vim insert mode:
to have closing bracket/parenthesis inserted (after the cursor) every time I type the opening one
to have #{} inserted whenever I type # inside "" (optionally, inside %() too)
I know it is possible, but my competence in this part of vim does not even reach the self-starter level.
This script will do the first one (auto inserting the closing bracket and placing the cursor between the brackets.)
lh-brackets helps define brackets related mappings. It also provides a few functions aimed at defining context-sensitive mappings and abbreviations (see Map4TheseContext).
If in ruby %() is associated to a syntax highlighting, Map4TheseContext will also solve your last request. If not, you'll have to play with searchpair() to detect the current context. Let me know if you have troubles to come up with a working solution.
How can I make a shell script that will know where the caret is and grab selected text, so I can wrap the text in something? For example, this is a script from a Textmate bundle:
<${1:p}>$TM_SELECTED_TEXT</${1/\s.*//}>
It grabs the text and wraps it in open/close HTML tags. And it is variable so the second tag is mirrored as you type the first.
I want to make a script like this but outside of Textmate so I can use it in TextEXpander.
Does this make sense ? :)
$TM_SELECTED_TEXT is not an environment variable provided to scripts running outside of TextMate -- it is a variable that is set by TextMate and provided to scripts that are run as part of its snippet system. It is not even provided to the Shell bundles "Run Script" command.
What you want to do may be doable through other shell facilities (e.g., sed) so the functionality can be replicated by a shell script -- it depends on your usage scenario.