To use Vim/Python like a calculator one option is executing the following command in gVim:
:pyf[ile] P:\Computer Applications\Python\pi.py
I intend on storing all my py files in the directory P:\Computer Applications\Python\. Can I add something to my _vimrc file so that in the future gVim knows where all my python files are stored and all I need to write is the following?
:pyf[ile] pi.py
Why not use a mapping?
nnoremap <Leader>p :pyf P:\Computer Applications\Python\
You can then press \p (in normal mode) to get the :pyf P:\Computer Applications\Python\ prefix.
References
Mapleader
:noremap
Normal mode
Related
In my ~/.vimrc, I have an if statement that is supposed to determine if the open file is a python file or not:
if (&ft=='python')
map <F9> <esc>:w<enter>:!python3 '%'<enter>
endif
The goal is to bind the python execution command to F9 if the file is a python file, but when I press F9 in a .py file nothing happens. I took out the if statement, and it worked. What is going wrong?
Try something like that instead:
au FileType python map <buffer> <F9> <esc>:w<bar>!python3 '%'<cr>
Your .vimrc config file runs only once on startup. So if you put an if test at this time, it won't work, because no python file is then currently edited.
But you can use .vimrc to setup an automatic behaviour : something that vim will do each time it will encounter a special condition. The condition can be in your case : "A new file is being editing, and its file type is 'python'". See :h :au
In cases like yours, it's useful to add the <buffer> parameter in the :map command : it limits the scope of your mapping to the current buffer only : so when you will press F9 on a non-python buffer, the mapping will not be trigerred.
EDIT:
In my first answer, I also deleted the <esc> in your command, but maybe I'm wrong about this, because it can cause problems in visual mode, so I put it again. You have to test it in visual mode, I didn't do it.
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')`
The Netrw plugin has a special <s-cr> keybinding to "squeeze" the current directory (hence fold it). As we all know <s-cr> or shift-enter has troubles with terminals and it do not properly work (in my case too, it just open a file like <cr>). So I remap it as follows: nmap <leader>f <s-cr>. After pressing \f I get these errors:
Are there any solutions? Maybe there is explicit command to map (eg :netrw-squeeze)?
If I have a file with a shebang line (e.g. #!/bin/bash) open in Vim and the file has execute permissions (i.e. chmod +x) I know I can type this to execute it without leaving the editor:
:! %:p
: for command mode
! to run a shell command
% to refer to the file in the current buffer
:p to use the full path of the current file
Is there a shorter shortcut for this frequent task?
e.g. there is a ZZ shortcut for :wq, etc.
:!%:p
,without the spaces, is shorter.
If you want an even shorter shortcut, you can create a custom mapping:
nnoremap <F9> :!%:p
or the more "mnemonic":
nnoremap <leader>r :!%:p
If you haven't set permissions you can run:
:! sh %
None of the previous answers work if your filename/directory path has spaces in it. Simple fix.
:!"%:p"
After you've executed that once, a short :!! will repeat it.
When starting vi, specify file path explicitly, like this "vi ./blablabla"
vi ./yourscript.pl
Then start with !%
The other variant is to invoke the vi command like this
!./%
You can add a key mapping to your .vimrc
map <F5> :!%
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