How do I write a custom command in Atom? - atom-editor

I want to write a command for Atom that composes two or more pre-existing commands, like "Select Line" and then "Cut". How do I do that?

You can add the following code to your init.coffee file:
atom.commands.add 'atom-text-editor', 'custom:cut-line', ->
editor = atom.workspace.getActiveTextEditor()
editor.selectLinesContainingCursors()
editor.cutSelectedText()
You can get the code to execute from the source by searching for strings in the command palette. And once you have a command created, you can map a key to it by editing your keymap.cson file:
'atom-text-editor':
'alt-cmd-z': 'custom:cut-line'

Related

How to bind a key combination to a command?

I have recently run into the following use case in LightTable:
I want to execute a command (for instance, duplicate a line)
I press "Ctrl-Space" and a list of commands shows up
I select the command I need
I want to repeat this command often, but there is no keybinding for this command
I go to user.keymap where I am supposed to add a line like [:editor "alt-shift-w" :editor.watch.unwatch]
But I have no idea what is the :editor.watch.unwatch-like key for the command I have just found. All I know is the displayed name of this command: Editor: duplicate line
Is there a way to add this keybinding without digging up the documentation and finding the key?
If you add the beginning of a line like this to your user.keymap:
[:editor "alt-shift-w"
and position the keyboard cursor at the end of the line you should be able to type duplicate line and the relevant command should be listed in a popup autocomplete menu.

Run bash command for currently selected line

I'd like to be able to hit a shortcut like cmd-shift-r and have that automatically run a bash command, say, mix test test/turtle/api/v3_test.exs:72.
In other words: mix test {FILE_ACTIVE}:{FILE_ACTIVE_LINE_NUMBER}
What is the best way to accomplish this? Is there an atom package that takes care of this or something I could write quickly myself?
Thanks!
There is a fairly low-fi way to get this to work using your init.coffee load this by going to File → Init Script... or by choosing Application: Open Your Init Script from the Command Palette.
Add the following to the bottom of your init.coffee:
atom.commands.add 'atom-text-editor',
'custom:execute-this-test': ->
if editor = atom.workspace.getActiveTextEditor()
row = editor.getCursors()[0].getBufferRow() + 1
path = editor.buffer.file.path
{spawn} = require 'child_process'
mix = spawn 'mix', ['test', "#{path}:#{row}"]
mix.stderr.on 'data', (data) ->
atom.notifications.addError data.toString()
mix.stdout.on 'data', (data) ->
atom.notifications.addInfo data.toString()
mix.on 'exit', (code) ->
atom.notifications.addInfo "Exited with Code #{code}"
Save the file with Ctrl-S and reload the text editor with Ctrl-Alt-R.
Once Atom has restarted, find the line in the file you want to execute the code against and open the Command Palette with Ctrl-Shift-P and search for Execute This Test.
You may want to tweak the code to be reduce the amount of output it generates, however the above is probably enough to get you started. If you wanted to optimize the workflow even further you can create a Keybinding.

Evaluating buffer until the cursor

I am trying to create a key binding for "Evaluate buffer till here" in Emacs & ESS, which is situated in ESS => ESS Eval menu. Most of the commands in that menu are listed in help files (http://ess.r-project.org/Manual/ess.html, and in Emacs options), but this particular one is not. If I place following code in .emacs file:
(eval-after-load "ess-mode" '(define-key ess-mode-map (kbd "C-.") 'ess-eval-buffer-till-here))
I get a following message when trying to use the binding: Symbol´s function definition is void: ess-eval-buffer-till-here. Obviously I am calling for wrong name. What is the right name for this command and how can I see all of the commands for ESS?
So it's a menu item? Type C-hk and then select that item.
(Menus are implemented as keymaps, so this is just the normal describe-key functionality.)
You can also see the non-interactive call form of the last command with C-xESCESC or C-xM-:. It's easy to figure out the command name once you have that. (thanks event_jr)
For listing all commands, most modes will list all their key bindings in their docstring, so you can use C-hm to describe the modes in use in the buffer.
As there may be commands without bindings, you could also use M-x apropos-command to list them all (most likely specifying ^ess as a pattern, if it uses that as a consistent name space).

how to insert the filename before inserting the file into another file in vim

Is there a way we can insert the filename before we insert the file content into another file
i know how to insert a file into another -- :r
But how to insert the filename which we are inserting before
Thanks
If you have the filename in a register (e.g. the system clipboard), you can insert that with <C-r>+. This also works with Vim variables: <C-r>=g:filename<CR>.
But usually, you'll just type the :r filename; then, the only shortcut to avoid typing it again is the filename completion (:help i_CTRL-X_CTRL-F): In insert mode, type the beginning of the file name, then try to complete: filen<C-x><C-f>.
You can avoid the typing if you do it in reverse order: As the :read command sets the alternate file, you can insert that through the special register #:
:read filename
'[OI just added <C-r>#
You can write a small function to insert filename before reading the content:
fun! ReadwithFn(fname)
let fn = expand(a:fname)
if !filereadable(fn)
echohl Error
echom fn." cannot be read!"
echohl NONE
else
execute "put ='".fn."'"
execute 'read '.fn
endif
endf
add define your own command:
command! -nargs=1 R call ReadwithFn(<q-args>)
if you source above codes, you could type
:R fileToRead
the function will first insert the path of the file, then comes the content. If the file not readable (doesn't exist for example) there is an error msg.
It is just basic script, you can tweak a bit.

How to use a template in vim

This is really a newbie question - but basically, how do I enable a template for certain filetypes.
Basically, I just want the template to insert a header of sorts, that is with some functions that I find useful, and libraries loaded etc.
I interpret
:help template
the way that I should place this in my vimrc
au BufNewFile,BufRead ~/.vim/skeleton.R
Running a R script then shows that something could happen, but apparently does not:
--- Auto-Commands ---
This may be because a template consists of commands (and there are no such in skeleton.R) - and in this case I just want it to insert a text header (which skelton.R consist of).
Sorry if this question is mind boggeling stupid ;-/
The command that you've suggested is not going to work: what this will do is run no Vim command whenever you open ~/.vim/skeleton.R
A crude way of achieving what you want would be to use:
:au BufNewFile *.R r ~/.vim/skeleton.R
This will read (:r) your file whenever a new *.R file is created. You want to avoid having BufRead in the autocmd, or it will read the skeleton file into your working file every time you open the file!
There are many plugins that add a lot more control to this process. Being the author and therefore completely biased, I'd recommend this one, but there are plenty of others listed here.
Shameless plug:
They all work in a relatively similar way, but to explain my script:
You install the plugin as described on the linked page and then create some templates in ~/.vim/templates. These templates should have the same extension as the 'target' file, so if it's a template for .R files, call it something like skeleton.R. In your .vimrc, add something like this:
let g:file_template_default = {}
let g:file_template_default['R'] = 'skeleton'
Then create your new .R file (with a filename, so save it if it's new) and enter:
:LoadFileTemplate
You can also skip the .vimrc editing and just do:
:LoadFileTemplate skeleton
See the website for more details.
Assume that your skeletons are in your ~/.vim/templates/ directory, you can put this
snippet in your vimrc file.
augroup templates
au!
" read in templates files
autocmd BufNewFile *.* silent! execute '0r ~/.vim/templates/skeleton.'.expand("<afile>:e")
augroup END
Some explanation,
BufNewFile . = each time we edit a new file
silent! execute = execute silently, no error messages if failed
0r = read file and insert content at top (0) in the new file
expand(":e") = get extension of current filename
see also http://vim.wikia.com/wiki/Use_eval_to_create_dynamic_templates
*fixed missing dot in file path
Create a templates subdirectory in your ~/.vim folder
$ mkdir -p ~/.vim/templates
Create a new file in subdirectory called R.skeleton and put in the header and/or other stuff you want to automagically load upon creating a new ".R " file.
$ vim ~/.vim/templates/R.skeleton
Then, add the following to your ~/.vimrc file, which may have been suggested in a way by "guest"
autocmd BufNewFile * silent! 0r $HOME/.vim/templates/%:e.skeleton
Have a look at my github repository for some more details and other options.
It's just a trick I used to use .
It's cheap but If you ain't know nothing about vim and it's commands it's easy to handle.
make a directory like this :
~/.vim/templates/barney.cpp
and as you konw barney.cpp should be your template code .
then add a function like ForUncleBarney() to end of your .vimrc file located in ~/.vimrc
it should be like
function ForBarneyStinson()
:read ~/.vim/templates/barney.cpp
endfunction
then just use this command in vim
:call ForBarneyStinson()
then you see your template
as an example I already have two templates for .cpp files
:call ForBarney()
:call ACM()
sorry said too much,
Coding's awesome ! :)
Also take a look at https://github.com/aperezdc/vim-template.git.
I use it and have contributed some patches to it and would argue its relatively full featured.
What about using the snipmate plugin? See here
There exist many template-file expanders -- you'll also find there explanations on how to implement a rudimentary template-file expander.
For my part, I'm maintaining the fork of muTemplate. For a simple start, just drop a {ft}.template file into {rtp}/template/. If you want to use any (viml) variable or expression, just do. You can even put vim code (and now even functions) into the template-file if you wish. Several smart decisions are already implemented for C++ and vim files.

Resources