I use ack.vim, and have below keymap in my .vimrc,
nnoremap ;f :Ack! ^\"
to search for tags in my note files, which are lines that begin with double quote followed by non-space characters.
which works fine, since my note files lies in a common directory, say ~/notes.
Now, say at a git repo, eg, ~/code/some_repo, I want below keymap at that directory,
nnoremap ;f :Ack! ^\/\/\ *\"
I could manually set the keymap if work at given directory, but it soon become tedious.
So I wonder, how can I set keymap base on working directory when I start vim.
-- hopefully vimscript solution, with possible aid of bash command.
Have a look at https://stackoverflow.com/a/456889/15934
The solutions exposed can solve your question. Either by defining buffer specific mappings (:h :map-<buffer>, or by defining buffer variables that you could use in your mappings (:h b:var).
As #Luc suggested, I could use autocmd base on file name with BufEnter and BufCreate. Which works fine.
Then I ask myself, why stick to command only??
Just utilize vim builtin functions and simple vimscript statement!!
So, let's break my requirement to different parts.
get current working directory. -- getcwd().
pattern match on that. -- =~. What does =~ mean in VimScript?
do keymap base on match or not.
below is what's in my vimrc then,
"git, see also " grep
if getcwd() =~ "/code/repoA/"
nnoremap ;f :Ack! ^\/\/\ *\"
nnoremap ,a :!cd %:p:h && git add %<CR>
else
nnoremap ;f :Ack! ^\"
endif
Related
I am trying to personalize my ~/.vimrc file.
Here is what I want: when the file name opened with vi is titi45.tex, and when I press <space> in normal mode, I want that the command make toto45
is executed. And if the file opened with vi is called titi65.tex, I want that
make toto65 is executed, and so on.
I tried to use a command
au FileType tex nmap <space> :w<CR>:!make<CR><CR>
in my .vimrc but I don't know how to match the file name and use the number.
Can you help me?
Mathieu
You are looking for :make %<. BTW why don't you compile within vim? Avoid :!make. Prefer :make, and check the help related to the quickfix mode (:h quickfix).
Your mapping would then be:
nnoremap <buffer> <silent> <space> :update<cr>:make %<<cr>
(the <buffer> part is very important, it makes sure your mapping won't leak to other filetypes; the other are not critical here, but good practices)
EDIT: Sorry I missed the exact requirement.
Then, you'll have to transform the target name. You'll to play with substitute() and :exe. But your substitution won't be a simple substitution. It looks like a rotating substitution. Solutions for this kind of substitution have been described over there: How do I substitute from a list of strings in VIM?
And IIIRC, there exist a plugin that does similar things.
In your case, I guess I would use a dictionary to define how names are substituted:
let k_subs = { 'toto': 'titi', 'titi': 'toto' }
nnoremap <buffer> <silent> <space> :update<cr>:exe 'make '.substitute(expand('%'), join(keys(k_subs), '\|'), '\=k_subs[submatch(0)]', '')cr>
NB: I haven't tested it.
If you want to get rid of the extension, it'd better be done in expand() argument.
Hum...
finally i use an additionnal script
#!/bin/bash
maRegex='source_(enonce|corrige)([0-9]+)$'
if [[ "${1}" =~ $maRegex ]]
then
commande="make enonce${BASH_REMATCH[2]}"
else
commande="make plouf"
fi
echo commande de compilation lancée: $commande
$commande
This script in launched by vimrc.
I got the following hotkey mapping:
nnoremap <leader>f Unite file -path=~/Workspace
This works great, however, I want to make it so that path equals the current folder I'm in (which will be seperate from working directory).
Anyone know how I can make this happen? :S
You can use the expand() function to use %:p:h in places where a file name is not expected (these expansions are for file-name arguments, not others like what it appears to happen with your command)
:echo expand('%:p:h')
But you can't map that directly. It is a command that needs to be built "on-the-fly", so you can use :execute to build and execute an evaluated expression:
nnoremap <leader>f :exec "Unite file -path=" . expand('%:p:h')
How about using:
:UniteWithBufferDir file
or
:UniteWithCurrentDir file
(depending on what you want)
Unite allows dynamic argument by using backtick, as documented in the Unite help doc:
You don't have to use |:execute| for dynamic arguments.
You can use evaluation cmdline by ``.
Note: In the evaluation, The special characters(spaces, "\" and ":")
are escaped automatically.
>
:Unite -buffer-name=search%`bufnr('%')` line:forward:wrap<CR>
So in your case, the mapping will be:
:nnoremap <leader>f :Unite file -path=`expand('%:p:h')`
I am trying to make a keymap that will call latexmk when .tex is available (it would be better if .tex is the currently open and active buffer)
I tried :
:nnoremap <Leader>lw :if filereadable('*.tex')<cr>up!<cr>:!latexmk -pdf<cr>endif<cr>
when trying to make latexmk -pdf run, but unfortunately, its just prompting those line in the window, and doing nothing like:
~
:if filereadable('*.tex')
: up!
: :call Tex_RunLaTeX()
: endif
Press ENTER or type command to continue
kindly help.
(it will be great, as told, if this can be done when .tex is the currently open and active buffer.)
NB: this question and its variant has been asked here several time here eg this and this, hence sorry for the repetation. I have failed to solve my problem with those.
You need to do 3 things:
fix your mapping to run the command properly
create a mapping local to a specific buffer by using the <buffer> option for nnoremap.
load the mappings for just a specific filetype.
First fix the mapping by using executing the command as single ex command by using <bar> and removing :'s & <cr>'s. We also remove the filereadable portion because we just wrote the file.
nnoremap <buffer> :up!<bar>!latexmk -pdf<cr>
or you can use an expressing mapping like FDinoff suggested.
Note the <buffer> option on the mapping. This makes the mapping only available to the current buffer not every buffer.
Now we need to make sure this mapping only works for tex filetypes. This can be done via an autocommand in your .vimrc like so:
autocmd FileType tex nnoremap <buffer> :up!<bar>!latexmk -pdf<cr>
The other way option is by creating a filetype plugin. (see :h ftplugin for more details)
A simple example is do create a file named, ~/.vim/ftplugin/text.vim and place your mappings inside like so:
nnoremap <buffer> :up!<bar>!latexmk -pdf<cr>
I personally lean more towards the ftplugin approach but having a everything in your .vimrc file can be nice.
I feel like this could be done with an autocmd.
The autocmd only loads the mapping when the file is a tex file.
autocmd FileType tex nnoremap <leader>lw :up! \| !latexmk -pdf<CR>
If you want to do this filereadable('*.tex') which just checks to see if a file in the directory is a tex file. You could use the expr mapping from the first link. In the else part of the expression we just put an empty string so the mapping will do nothing.
nnoremap <expr> <leader>lw filereadable('*.txt') ? ':up! \| !latexmk -pdf<CR>' : ''
I'm trying to add a key mapping so that it opens up command and populate it with :e /path/to/current/file
I can get the current directory using :pwd but I'm having a trouble to use it in the mapping
I think it will be along the lines of setting pwd to a variable and use that variable as such:
noremap <C-q> <C-o>:e *pwdvariable*<Space>
Should I create a function to perform this?
I guess you need
nnoremap <C-q> <C-\><C-n>:e <C-r>=fnameescape(expand('%:p:h'))<CR>
%:p:h will get the full path of the current file (without trailing slash). Read more in :help filename-modifiers.
Not exactly, what you have asked for, but maybe more helpful: Vim tip 64: Set working directory to the current file: In short, add the following line to .vimrc:
autocmd BufEnter * silent! lcd %:p:h
Interesting for you would be also Easy edit of files in the same directory.
The current gf command will open *.pdf files as ascii text. I want the pdf file opened with external tools (like okular, foxitreader, etc.). I tried to use autocmd to achieve it like this:
au BufReadCmd *.pdf silent !FoxitReader % & "open file under cursor with FoxitReader
au BufEnter *.pdf <Ctrl-O> "since we do not really open the file, go back to the previous buffer
However, the second autocmd failed to work as expected. I could not figure out a way to execute <Ctrl-o> command in a autocmd way.
Could anyone give me a hint on how to <Ctrl-O> in autocmd, or just directly suggest a better way to open pdf files with gf?
Thanks.
That's because what follows an autocmd is an ex command (the ones beginning
with a colon). To simulate the execution of a normal mode command, use the
:normal command. The problem is that you can't pass a <C-O> (and not
<Ctrl-O>) directly to :normal, it will be taken as literal characters (<,
then C, then r) which is not a very meaningful normal command. You have two
options:
1.Insert a literal ^O Character
Use controlvcontrolo to get one:
au BufEnter *.pdf normal! ^O
2.Use :execute to Build Your Command
This way you can get a more readable result with the escaped sequence:
au BufEnter *.pdf exe "normal! \<c-o>"
Anyway, this is not the most appropriate command. <C-O> just jumps to the
previous location in the jump list, so your buffer remains opened. I would do
something like:
au BufEnter *.pdf bdelete
Instead. Still I have another solution for you.
Create another command with a map, say gO. Then use your PDF reader
directly, or a utility like open if you're in MacOS X or Darwin (not sure if
other Unix systems have it, and how it's called). It's just like double clicking
the icon of the file passed as argument, so it will open your default PDF reader
or any other application configured to open any file by default, like images or
so.
:nnoremap gO :!open <cfile><CR>
This <cfile> will be expanded to the file under the cursor. So if you want to
open the file in Vim, use gf. If you want to open it with the default
application, use gO.
If you don't have this command or prefer a PDF-only solution, create a map to
your preferred command:
:nnoremap gO :!FoxitReader <cfile> &<CR>
If the default app is acceptable, then simply using :!open % in command mode works. You can always map this to a suitable leader combination in your vim config file etc.
If you want something that works with normal mode, then you could try something like the following (i use this too for opening HTML files), and modify to your own needs:
if has('win32') || has ('win64')
autocmd FileType html nmap <Leader>g :silent ! start firefox "%"<cr>
elseif has('mac')
autocmd FileType html nmap <Leader>g :!open "%"<cr><cr>
endif