Remaping leader with :execute cannot run two commands at once

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite
1












I have following command:



map <Leader>ntd :execute ":tabe! ". g:path ."l_todo.txt"<CR><ESC> :execute ":vsplit! ".g:path ."u_todo.txt"<CR>


Both tabe! and vsplit! commands works fine as separate remapings. I want to join those commands into one remaping.



How to make the above command to work?










share|improve this question



















  • 2




    You should try using only once execute call with a pipe | to chain the commands like nnoremap <leader>a :execute "tabnew | vsplit"<CR>. I didn't test it but I think your command should be map <Leader>ntd :execute "tabe! ". g:path ."l_todo.txt | vsplit! ".g:path ."u_todo.txt"<CR> (Also use nore and a mode in your map command i.e. nnoremap, this is a good practice and can save you a lot of debugging`)
    – statox♦
    8 hours ago















up vote
2
down vote

favorite
1












I have following command:



map <Leader>ntd :execute ":tabe! ". g:path ."l_todo.txt"<CR><ESC> :execute ":vsplit! ".g:path ."u_todo.txt"<CR>


Both tabe! and vsplit! commands works fine as separate remapings. I want to join those commands into one remaping.



How to make the above command to work?










share|improve this question



















  • 2




    You should try using only once execute call with a pipe | to chain the commands like nnoremap <leader>a :execute "tabnew | vsplit"<CR>. I didn't test it but I think your command should be map <Leader>ntd :execute "tabe! ". g:path ."l_todo.txt | vsplit! ".g:path ."u_todo.txt"<CR> (Also use nore and a mode in your map command i.e. nnoremap, this is a good practice and can save you a lot of debugging`)
    – statox♦
    8 hours ago













up vote
2
down vote

favorite
1









up vote
2
down vote

favorite
1






1





I have following command:



map <Leader>ntd :execute ":tabe! ". g:path ."l_todo.txt"<CR><ESC> :execute ":vsplit! ".g:path ."u_todo.txt"<CR>


Both tabe! and vsplit! commands works fine as separate remapings. I want to join those commands into one remaping.



How to make the above command to work?










share|improve this question















I have following command:



map <Leader>ntd :execute ":tabe! ". g:path ."l_todo.txt"<CR><ESC> :execute ":vsplit! ".g:path ."u_todo.txt"<CR>


Both tabe! and vsplit! commands works fine as separate remapings. I want to join those commands into one remaping.



How to make the above command to work?







key-bindings






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 8 hours ago









statox♦

24.4k660125




24.4k660125










asked 8 hours ago









lluke

434




434







  • 2




    You should try using only once execute call with a pipe | to chain the commands like nnoremap <leader>a :execute "tabnew | vsplit"<CR>. I didn't test it but I think your command should be map <Leader>ntd :execute "tabe! ". g:path ."l_todo.txt | vsplit! ".g:path ."u_todo.txt"<CR> (Also use nore and a mode in your map command i.e. nnoremap, this is a good practice and can save you a lot of debugging`)
    – statox♦
    8 hours ago













  • 2




    You should try using only once execute call with a pipe | to chain the commands like nnoremap <leader>a :execute "tabnew | vsplit"<CR>. I didn't test it but I think your command should be map <Leader>ntd :execute "tabe! ". g:path ."l_todo.txt | vsplit! ".g:path ."u_todo.txt"<CR> (Also use nore and a mode in your map command i.e. nnoremap, this is a good practice and can save you a lot of debugging`)
    – statox♦
    8 hours ago








2




2




You should try using only once execute call with a pipe | to chain the commands like nnoremap <leader>a :execute "tabnew | vsplit"<CR>. I didn't test it but I think your command should be map <Leader>ntd :execute "tabe! ". g:path ."l_todo.txt | vsplit! ".g:path ."u_todo.txt"<CR> (Also use nore and a mode in your map command i.e. nnoremap, this is a good practice and can save you a lot of debugging`)
– statox♦
8 hours ago





You should try using only once execute call with a pipe | to chain the commands like nnoremap <leader>a :execute "tabnew | vsplit"<CR>. I didn't test it but I think your command should be map <Leader>ntd :execute "tabe! ". g:path ."l_todo.txt | vsplit! ".g:path ."u_todo.txt"<CR> (Also use nore and a mode in your map command i.e. nnoremap, this is a good practice and can save you a lot of debugging`)
– statox♦
8 hours ago











1 Answer
1






active

oldest

votes

















up vote
4
down vote













You don't need to use two different execute() calls, you can simply call both commands in execute() like this:



nnoremap <Leader>ntd :execute "tabe! ". g:path . "l_todo.txt | vsplit! " . g:path . "u_todo.txt"<CR>


Note the escaped piped character (|) it is escaped so that vim understands that it is part of the command fed to execute.



Otherwise, when you source your vimrc, Vim would try to execute first nnoremap <Leader>ntd :execute ":tabe! ". g:path . "l_todo.txt followed by vsplit! " . g:path . "u_todo.txt"<CR> which is not what you want.




You could also improve the readability of your code like this:



let firstCommand="tabe! ". getcwd() . "/l_todo.txt"
let secondCommand="vsplit! " . getcwd() . "/u_todo.txt"
nnoremap <Leader>ntd :execute firstCommand . "|" . secondCommand<CR>


Here we separate the different part of the string give to execute() to see the two actions executed.



We also use :h getcwd() to get the current working directory so that we don't need a global variable to get the path.




Several other points about your code:



  • You don't need the : in the commands you feed to execute()

  • Always use the non recurcive version of map (i.e. noremap) unless you know what you're doing

  • Always specify a mode to map (see :h map-modes)





share|improve this answer


















  • 1




    With your refactoring, I believe changing g:path mid-session wont be reflected in the mapping. Id make first and second functions instead.
    – D. Ben Knoble
    4 hours ago






  • 1




    @D.BenKnoble You're perfectly right, I guess one could also use :h getcwd() instead of g:path depending on what the variable is meant to contain.
    – statox♦
    4 hours ago










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "599"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fvi.stackexchange.com%2fquestions%2f17477%2fremaping-leader-with-execute-cannot-run-two-commands-at-once%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
4
down vote













You don't need to use two different execute() calls, you can simply call both commands in execute() like this:



nnoremap <Leader>ntd :execute "tabe! ". g:path . "l_todo.txt | vsplit! " . g:path . "u_todo.txt"<CR>


Note the escaped piped character (|) it is escaped so that vim understands that it is part of the command fed to execute.



Otherwise, when you source your vimrc, Vim would try to execute first nnoremap <Leader>ntd :execute ":tabe! ". g:path . "l_todo.txt followed by vsplit! " . g:path . "u_todo.txt"<CR> which is not what you want.




You could also improve the readability of your code like this:



let firstCommand="tabe! ". getcwd() . "/l_todo.txt"
let secondCommand="vsplit! " . getcwd() . "/u_todo.txt"
nnoremap <Leader>ntd :execute firstCommand . "|" . secondCommand<CR>


Here we separate the different part of the string give to execute() to see the two actions executed.



We also use :h getcwd() to get the current working directory so that we don't need a global variable to get the path.




Several other points about your code:



  • You don't need the : in the commands you feed to execute()

  • Always use the non recurcive version of map (i.e. noremap) unless you know what you're doing

  • Always specify a mode to map (see :h map-modes)





share|improve this answer


















  • 1




    With your refactoring, I believe changing g:path mid-session wont be reflected in the mapping. Id make first and second functions instead.
    – D. Ben Knoble
    4 hours ago






  • 1




    @D.BenKnoble You're perfectly right, I guess one could also use :h getcwd() instead of g:path depending on what the variable is meant to contain.
    – statox♦
    4 hours ago














up vote
4
down vote













You don't need to use two different execute() calls, you can simply call both commands in execute() like this:



nnoremap <Leader>ntd :execute "tabe! ". g:path . "l_todo.txt | vsplit! " . g:path . "u_todo.txt"<CR>


Note the escaped piped character (|) it is escaped so that vim understands that it is part of the command fed to execute.



Otherwise, when you source your vimrc, Vim would try to execute first nnoremap <Leader>ntd :execute ":tabe! ". g:path . "l_todo.txt followed by vsplit! " . g:path . "u_todo.txt"<CR> which is not what you want.




You could also improve the readability of your code like this:



let firstCommand="tabe! ". getcwd() . "/l_todo.txt"
let secondCommand="vsplit! " . getcwd() . "/u_todo.txt"
nnoremap <Leader>ntd :execute firstCommand . "|" . secondCommand<CR>


Here we separate the different part of the string give to execute() to see the two actions executed.



We also use :h getcwd() to get the current working directory so that we don't need a global variable to get the path.




Several other points about your code:



  • You don't need the : in the commands you feed to execute()

  • Always use the non recurcive version of map (i.e. noremap) unless you know what you're doing

  • Always specify a mode to map (see :h map-modes)





share|improve this answer


















  • 1




    With your refactoring, I believe changing g:path mid-session wont be reflected in the mapping. Id make first and second functions instead.
    – D. Ben Knoble
    4 hours ago






  • 1




    @D.BenKnoble You're perfectly right, I guess one could also use :h getcwd() instead of g:path depending on what the variable is meant to contain.
    – statox♦
    4 hours ago












up vote
4
down vote










up vote
4
down vote









You don't need to use two different execute() calls, you can simply call both commands in execute() like this:



nnoremap <Leader>ntd :execute "tabe! ". g:path . "l_todo.txt | vsplit! " . g:path . "u_todo.txt"<CR>


Note the escaped piped character (|) it is escaped so that vim understands that it is part of the command fed to execute.



Otherwise, when you source your vimrc, Vim would try to execute first nnoremap <Leader>ntd :execute ":tabe! ". g:path . "l_todo.txt followed by vsplit! " . g:path . "u_todo.txt"<CR> which is not what you want.




You could also improve the readability of your code like this:



let firstCommand="tabe! ". getcwd() . "/l_todo.txt"
let secondCommand="vsplit! " . getcwd() . "/u_todo.txt"
nnoremap <Leader>ntd :execute firstCommand . "|" . secondCommand<CR>


Here we separate the different part of the string give to execute() to see the two actions executed.



We also use :h getcwd() to get the current working directory so that we don't need a global variable to get the path.




Several other points about your code:



  • You don't need the : in the commands you feed to execute()

  • Always use the non recurcive version of map (i.e. noremap) unless you know what you're doing

  • Always specify a mode to map (see :h map-modes)





share|improve this answer














You don't need to use two different execute() calls, you can simply call both commands in execute() like this:



nnoremap <Leader>ntd :execute "tabe! ". g:path . "l_todo.txt | vsplit! " . g:path . "u_todo.txt"<CR>


Note the escaped piped character (|) it is escaped so that vim understands that it is part of the command fed to execute.



Otherwise, when you source your vimrc, Vim would try to execute first nnoremap <Leader>ntd :execute ":tabe! ". g:path . "l_todo.txt followed by vsplit! " . g:path . "u_todo.txt"<CR> which is not what you want.




You could also improve the readability of your code like this:



let firstCommand="tabe! ". getcwd() . "/l_todo.txt"
let secondCommand="vsplit! " . getcwd() . "/u_todo.txt"
nnoremap <Leader>ntd :execute firstCommand . "|" . secondCommand<CR>


Here we separate the different part of the string give to execute() to see the two actions executed.



We also use :h getcwd() to get the current working directory so that we don't need a global variable to get the path.




Several other points about your code:



  • You don't need the : in the commands you feed to execute()

  • Always use the non recurcive version of map (i.e. noremap) unless you know what you're doing

  • Always specify a mode to map (see :h map-modes)






share|improve this answer














share|improve this answer



share|improve this answer








edited 3 hours ago

























answered 5 hours ago









statox♦

24.4k660125




24.4k660125







  • 1




    With your refactoring, I believe changing g:path mid-session wont be reflected in the mapping. Id make first and second functions instead.
    – D. Ben Knoble
    4 hours ago






  • 1




    @D.BenKnoble You're perfectly right, I guess one could also use :h getcwd() instead of g:path depending on what the variable is meant to contain.
    – statox♦
    4 hours ago












  • 1




    With your refactoring, I believe changing g:path mid-session wont be reflected in the mapping. Id make first and second functions instead.
    – D. Ben Knoble
    4 hours ago






  • 1




    @D.BenKnoble You're perfectly right, I guess one could also use :h getcwd() instead of g:path depending on what the variable is meant to contain.
    – statox♦
    4 hours ago







1




1




With your refactoring, I believe changing g:path mid-session wont be reflected in the mapping. Id make first and second functions instead.
– D. Ben Knoble
4 hours ago




With your refactoring, I believe changing g:path mid-session wont be reflected in the mapping. Id make first and second functions instead.
– D. Ben Knoble
4 hours ago




1




1




@D.BenKnoble You're perfectly right, I guess one could also use :h getcwd() instead of g:path depending on what the variable is meant to contain.
– statox♦
4 hours ago




@D.BenKnoble You're perfectly right, I guess one could also use :h getcwd() instead of g:path depending on what the variable is meant to contain.
– statox♦
4 hours ago

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fvi.stackexchange.com%2fquestions%2f17477%2fremaping-leader-with-execute-cannot-run-two-commands-at-once%23new-answer', 'question_page');

);

Post as a guest













































































Comments

Popular posts from this blog

Long meetings (6-7 hours a day): Being “babysat” by supervisor

Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

Confectionery